vadimtsushko avatar vadimtsushko committed 3bb9459

11

Comments (0)

Files changed (23)

 tests/allTests.dart
 tests/CursorTest.dart
 tests/DbCommandTest.dart
+lib/objectory/WebsocketClient.dart
+tests/objectory/allObjectoryTests.dart
 tests/objectory/PersistentObjectTest.dart
+lib/objectory/objectory_server.dart
 lib/bson/bson.dart
 examples/queries.dart
 third_party/dart-crypto-lib/test/vm/tests.dart
 third_party/log4dart/LogLib.dart
 tests/DbTest.dart
 tests/bson/BsonObjectIdTest.dart
+tests/objectory/ObjectoryImplWsTest.dart
 examples/simpleUpdate.dart
 third_party/dart-crypto-lib/src/md5.dart
+lib/objectory/ObjectoryLib_ws.dart
 tests/bson/BsonImplTest.dart
 tests/bson/ClientTest/ClientTest.dart
 tests/objectory/ObjectoryBaseImplTest.dart
 third_party/testing/unittest/unittest_dom.dart
 examples/raw_queries.dart
 lib/networking/logger.dart
+tests/objectory/AsyncException.dart
 third_party/testing/unittest/unittest_vm.dart
 lib/mongo.dart
 tests/objectory/ObjectoryImplVmTest.dart

lib/objectory/Objectory.dart

 typedef PersistentObject FactoryMethod();
 class ClassSchema{
+  static final SimpleProperty = 0;
+  static final InternalObject = 1;
+  static final ExternalObject = 2;
   String className;
   FactoryMethod factoryMethod;
   Set<String> properties;
   Map<String,String> components;
-  Map<String,String> links;  
+  Map<String,String> links;
   ClassSchema(this.className,this.factoryMethod,List<String> propertyList,    
     [this.components,this.links]){
     properties = new Set<String>.from(propertyList);
     if (links !== null){
       properties.addAll(links.getKeys());
     }
-
+  }    
+  void property(String propertyName, String className, [int type = SimpleProperty]){
+    
   }
-    
 }
 interface Objectory{  
   void registerClass(ClassSchema schema);
   Future<bool> open([String database, String url]);
   Future<bool> dropDb();
   ClassSchema getSchema(String className);
+  void close();
 }
 abstract class ObjectoryBaseImpl implements Objectory{
   Map<String,ClassSchema> schemata;

lib/objectory/ObjectoryDirectConnectionImpl.dart

   Future<bool> dropDb(){
     db.drop();
   }
+  void close(){
+    db.close();
+  }
 }

lib/objectory/ObjectoryWebSocketImpl.dart

     if (_objectory === null){
       _objectory = new ObjectoryWebSocketImpl._internal();
     }
-    return _objectory;
+    return _objectory;      
   }
 }
 class ObjectoryWebSocketImpl extends ObjectorySingleton{
-  ObjectoryWebSocketImpl._internal():super._internal();
+  static final IDLE_STATUS = 0;
+  static final GET_CONNECTION_ID = 1;
+  static final FIND_ONE = 2;
+  static final FIND = 3;
+  int messageProcessingStatus = IDLE_STATUS;  
   WebSocket webSocket;
-  String socketId; 
+  int socketId;  
   Completer onMessageCompleter;
+  ObjectoryWebSocketImpl._internal():super._internal();  
   Future<bool> open([String database, String url]){
     Completer completer = new Completer();    
     if (database !== null){
     if (url === null){
       throw "Url paramater is mandatory in WebSocket objectory implementation";
     }
+    onMessageCompleter = new Completer();    
     webSocket = new WebSocket(url);
-    onMessageCompleter = new Completer();
+    
     webSocket.onopen = (){
-      webSocket.send("Objectory opened");    
-      completer.complete(true);          
-      webSocket.onmessage = processMessage;
-    };        
-    return completer.future;    
+//      messageProcessingStatus =  GET_CONNECTION_ID;
+//      webSocket.onmessage = processMessage;      
+//      webSocket.send("Objectory opened");    
+      print("in client webSocket.onopen");
+      completer.complete(true);//  
+    };
+  //return Futures.wait([completer.future,onMessageCompleter.future]);
+  //  completer.complete(true);
+    return completer.future;
   }
   Future get onMessageFuture() => onMessageCompleter.future;
   void processMessage(message){
     var data = message.data;
-    print("in process message $data");
-    if (data is String){
-      socketId = data;
+    print("in process message $data $messageProcessingStatus");
+
+    if (messageProcessingStatus == GET_CONNECTION_ID){      
+      socketId = Math.parseInt(data);
       print("socketId  $socketId");
     }
-    else{    
+    else {    
       Binary buffer = new Binary.from(data);    
       BsonMap command = new BsonMap(null);
       command.unpackValue(buffer);
       print(command.data);    
     }
+    messageProcessingStatus = IDLE_STATUS;
     onMessageCompleter.complete(true);
   }  
-  Binary sendMessage(String command, Map obj, [String collecion]){
-    Map map = {};
-    map["command"] = command;
-    map["object"] = obj;
-    map["socketId"] = socketId;
-    BsonMap bmap = new BsonMap(map);
-    Binary message = new Binary(bmap.byteLength());
-    bmap.packValue(message);
+  Binary sendMessage(String command, Map obj, [String collection]){    
+    Map header = {};    
+    if (obj ===  null){
+      obj = {};
+    }
+    header["command"] = command;        
+    header["collection"] = collection;    
+    BsonMap bHeader = new BsonMap(header);    
+    BsonMap bObj = new BsonMap(obj);        
+    Binary message = new Binary(bHeader.byteLength()+bObj.byteLength());    
+    bHeader.packValue(message);
+    bObj.packValue(message);    
     webSocket.send(message.byteList);        
   }
   void save(RootPersistentObject persistentObject){    
        persistentObject.id = new ObjectId();
        persistentObject.map["_id"] = persistentObject.id;
     }
+    print("sendMessage($command,${persistentObject.map},${persistentObject.type})");
     sendMessage(command,persistentObject.map,persistentObject.type);
   }
   void remove(RootPersistentObject persistentObject){
     }
     sendMessage("remove",{"_id":persistentObject.id},persistentObject.type);
   }
+  Future<bool> dropDb(){
+    sendMessage("dropDb",{"_id":persistentObject.id},persistentObject.type);
+  }
+  Future<PersistentObject> findOne(String className,[Map selector]){
+    onMessageCompleter = new Completer<PersistentObject>();
+    sendMessage("findOne",selector,className);
+    messageProcessingStatus = FIND_ONE;
+    return onMessageCompleter.Future;  
+  }
+  future<List<PersistentObject>> find(String className,[Map selector]){
+    Completer completer = new Completer();
+    List<PersistentObject> result = new List<PersistentObject>();
+    db.collection(className)
+      .find(selector)
+      .each((map){
+        RootPersistentObject obj = objectory.map2Object(className,map);
+        result.add(obj);
+      }).then((_) => completer.complete(result));
+    return completer.future;  
+  }
+  void close(){
+    print("Closing objectory");
+    webSocket.close(null,"Normal closing, socketId: $socketId");
+  }
 /*  Future<List<PersistentObject>> find(String className,[Map selector]){
     Completer completer = new Completer();
     List<PersistentObject> result = new List<PersistentObject>();
       }).then((_) => completer.complete(result));
     return completer.future;  
   }
-  Future<PersistentObject> findOne(String className,[Map selector]){
-    Completer completer = new Completer();    
-    db.collection(className)
-      .findOne(selector)
-      .then((map){              
-        PersistentObject obj = objectory.map2Object(className,map);
-        completer.complete(obj);
-      });
-    return completer.future;  
-  }
   
-  Future<bool> dropDb(){
-    db.drop();
-  }
 */  
 }

lib/objectory/PersistentObject.dart

         return this[property];
       }
       else {       
+        print("Not registered property $property on for class $type");
         super.noSuchMethod(function_name, args);
       }        
     }    

lib/objectory/objectory_server.dart

 #import("../mongo.dart");
 #import("../bson/bson.dart");
 
-Map<String, String> contentTypes = const {
-  "html": "text/html; charset=UTF-8",
-  "dart": "application/dart",
-  "js": "application/javascript", 
-};
+final PORT = 8080;
+final DATABASE = 'OBJECTORY_WS_TEST';
+final IPADDRESS = '127.0.0.1';
 
-List<WebSocketConnection> connections;
-void processMessage(message){
-  print("in process message $message");
-  if (message is String){
-    print(message);    
+
+class ObjectoryServer {
+  Db db;
+    ObjectoryServer(){
+      db = new Db(DATABASE);
+      db.open().then((v){
+      WebSocketHandler wsHandler = new WebSocketHandler();
+      wsHandler.onOpen = (WebSocketConnection conn) {
+        conn.onError = (e) => print("onError: $e");
+        conn.onClosed = (a,b) => print("onClosed: $a, $b");  
+        conn.onMessage = (m) => processMessage(m,conn);    
+      };
+      HttpServer server = new HttpServer();
+      server.addRequestHandler((_) => true, wsHandler.onRequest);   
+      server.listen(IPADDRESS, PORT);  
+    });  
   }
-  else{    
-    Binary buffer = new Binary.from(message);    
-    BsonMap command = new BsonMap(null);
-    command.unpackValue(buffer);
-    print(command.data);    
+  void processMessage(message, WebSocketConnection connection){
+    if (message is String){
+      print(message);    
+    }
+    else{    
+      Binary buffer = new Binary.from(message);    
+      BsonMap header = new BsonMap(null);
+      header.unpackValue(buffer);
+      print("Header: ${header.value}");    
+      BsonMap obj = new BsonMap(null);
+      obj.unpackValue(buffer);
+      print("Obj: ${obj.value}");
+      String command = header.value["command"];
+      processMongoCommand(header.value["command"], header.value["collection"], obj.value, connection);
+    }    
+  }
+  void processMongoCommand(String command,String collection, Map obj, WebSocketConnection connection){
+    if (command == "insert"){
+      db.collection(collection).insert(obj);
+    }
+    if (command == "remove"){
+      db.collection(collection).remove(obj);
+    }
+    if (command == "dropDb"){
+      db.drop();
+    }  
+    if (command == "update"){
+      db.collection(collection).update(obj);
+    }
+    if (command == "findOne"){
+      db.collection(collection).findOne(obj).then((val)=>sendObject(connection,val));
+    }
+  }
+  sendObject(WebSocketConnection connection, Map val){
   }
 }
 void main() {
-  connections = new List();
-  
-  WebSocketHandler wsHandler = new WebSocketHandler();
-  wsHandler.onOpen = (WebSocketConnection conn) {
-    print("Connection opened. Connections total: ${connections.length}");
-    connections.add(conn);    
-    conn.onClosed = (a, b) => removeConnection(conn);
-    conn.onError = (_) => removeConnection(conn);
-    conn.send("234234234");    
-    conn.onMessage = processMessage;
-    
-  };
-
-  HttpServer server = new HttpServer();
-  server.addRequestHandler((_) => true, wsHandler.onRequest);   
-
-  server.listen("127.0.0.1", 8080);  
+  var server = new ObjectoryServer();
 }
-void removeConnection(WebSocketConnection conn) {
-  int index = connections.indexOf(conn);
-  if (index > -1) {
-    connections.removeRange(index, 1);
-  }
-}

lib/objectory/start_objectory.cmd

+dart --enable_type_checks --enable_asserts objectory_server.dart

tests/ConnectionTest.dart

 #import("../lib/bson/bson.dart");
 #import("dart:io");
 #import('dart:builtin');
-#import('../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../dart/dart-sdk/lib/unittest/unittest.dart');
 testPing(){
   Connection conn = new Connection();
   conn.connect();

tests/CursorTest.dart

 #import("../lib/mongo.dart");
 #import("dart:io");
 #import('dart:builtin');
-#import('../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../dart/dart-sdk/lib/unittest/unittest.dart');
 testCursorCreation(){
   Db db = new Db('mongo-dart-test');
   DbCollection collection = db.collection('student');

tests/DbCollectionTest.dart

 #import("../lib/mongo.dart");
 #import("dart:io");
 #import("dart:builtin");
-#import('../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../dart/dart-sdk/lib/unittest/unittest.dart');
 testCollectionCreation(){
   Db db = new Db('db');
   DbCollection collection = db.collection('student');

tests/DbCommandTest.dart

 #library("dbcommand");
 #import("../lib/mongo.dart");
-#import('../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../dart/dart-sdk/lib/unittest/unittest.dart');
 testDbCommandCreation(){
   Db db = new Db('mongo-dart-test');
   DbCommand dbCommand = new DbCommand(db,"student",0,0,1,{},{});

tests/DbTest.dart

 #library("dbtest");
 #import("../lib/mongo.dart");
 #import('dart:builtin');
-#import('../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../dart/dart-sdk/lib/unittest/unittest.dart');
 testDatabaseName(){
   Db db = new Db('mongo-dart-test');
   String dbName;

tests/SelectorBuilderTest.dart

 #import("../lib/bson/bson.dart");
 #import("dart:io");
 #import("dart:builtin");
-#import('../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../dart/dart-sdk/lib/unittest/unittest.dart');
 testSelectorBuilderCreation(){
   SelectorBuilder selector = query();
   expect(selector is Map && selector.isEmpty()).isTrue();

tests/bson/BSonTypesTest.dart

 #library("BsonTypesTest");
-#import('../../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../../dart/dart-sdk/lib/unittest/unittest.dart');
 #import("../../lib/bson/bson.dart");
 typeTest(){
   Expect.isTrue(bsonObjectFrom(1234) is BsonInt);

tests/bson/BsonBinaryTest.dart

 #library("bsonbinarytest");
-#import('../../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../../dart/dart-sdk/lib/unittest/unittest.dart');
 #import("../../lib/bson/bson.dart");
 testUint8ListNegativeWrite(){
   Uint8List bl = new Uint8List(4);

tests/bson/BsonImplTest.dart

 #library("bsonimpltest");
-#import('../../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../../dart/dart-sdk/lib/unittest/unittest.dart');
 #import("../../lib/bson/bson.dart");
 testSerializeDeserialize(){
   var bson = new BSON();

tests/bson/BsonObjectIdTest.dart

 #library("BsonObjectId");
-#import('../../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../../dart/dart-sdk/lib/unittest/unittest.dart');
 #import("../../lib/bson/bson.dart");
 testObjectId(){
   var id1 = new ObjectId();

tests/objectory/.children

+allObjectoryTests.dart
+ObjectoryImplVmTest.dart
+ObjectoryImplWsTest.dart
+PersistentObjectTest.dart

tests/objectory/.project

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>objectory</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>com.google.dart.tools.core.dartBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>com.google.dart.tools.core.dartNature</nature>
+	</natures>
+</projectDescription>

tests/objectory/ObjectoryImplVmTest.dart

 #library("ObjectoryVM");
 #import("../../lib/objectory/ObjectoryLib_vm.dart");
-#import('../../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../../dart/dart-sdk/lib/unittest/unittest.dart');
 #source("DomainModel.dart");
 #source("../../lib/helpers/SelectorBuilder.dart");
 #source("../../lib/helpers/MapProxy.dart");
+List futures;
 Future<bool> setUpObjectory(){
   var res = new Completer();
 //  objectory.clearFactories();
   return res.future;
 }
 void testInsertionAndUpdate(){
+  Completer completer = new Completer();
+  futures.add(completer.future);  
   Author author = new Author();  
   author.name = 'Dan';
   author.age = 3;
     expect(coll.length).equals(1);
     Author authFromMongo = coll[0];
     expect(authFromMongo.age).equals(4);    
+    completer.complete(true);
     callbackDone();
   });
 }
   expect(address.cityName).equals("44444");   
 }
 testCompoundObject(){
+  Completer completer = new Completer();
+  futures.add(completer.future);
   Person person = new Person();  
   person.address.cityName = 'Tyumen';
   person.address.streetName = 'Elm';  
   person.firstName = 'Dick';
-  objectory.save(person); ;
+  objectory.save(person);
   objectory.findOne('Person',query().id(person.id)).then((savedPerson){
     expect(savedPerson.firstName).equals('Dick');
     expect(savedPerson.address.streetName).equals('Elm');
     expect(savedPerson.address.cityName).equals('Tyumen');
+    completer.complete(true);
     callbackDone();
   });
 }
 testObjectWithLinks(){
+  Completer completer = new Completer();
+  futures.add(completer.future);  
   Person father = new Person();  
   father.firstName = 'Father';
   objectory.save(father);
     sonFromObjectory.fetchLinks().then((_){
       expect(sonFromObjectory.father.firstName).equals("Father");
       expect(sonFromObjectory.mother).equals(null);
+      completer.complete(true);      
       callbackDone();
     });    
   });
 }
 main(){  
+  futures = new List();  
   setUpObjectory().then((_) {    
-    group("ObjectoryVM", ()  {
+    group("ObjectoryVM", () {      
       asyncTest("testCompoundObject",1,testCompoundObject);   
       asyncTest("testInsertionAndUpdate",1,testInsertionAndUpdate);         
       asyncTest("testObjectWithLinks",1,testObjectWithLinks);               
       test("testNewInstanceMethod",testNewInstanceMethod);   
-      test("testMap2ObjectMethod",testMap2ObjectMethod);       
+      test("testMap2ObjectMethod",testMap2ObjectMethod);      
+      //Futures.wait(futures).then((_)=>objectory.close());
     });  
   });    
 }

tests/objectory/ObjectoryImplWsTest.dart

 #library("ObjectoryWS");
 #import("../../lib/objectory/ObjectoryLib_ws.dart");
 #import("../../lib/bson/bson.dart");
-#import('../../third_party/testing/unittest/unittest_vm.dart');
+//#import('../../../../dart/dart-sdk/lib/unittest/unittest.dart');
 #source("DomainModel.dart");
 Future<bool> setUpObjectory(){
   var res = new Completer();
 //  objectory.clearFactories();
-  objectory.open(url:"ws://localhost:8080").then((_){    
+  objectory.open(url:"ws://localhost:8080").then((_){
+//  new Future.immediate(true).then((_){    
 //    objectory.dropDb();
     print("I'm here");
-    registerClasses();
-    res.complete(true);
+//    registerClasses();
+//    res.complete(true);
   });    
   return res.future;
 }
-void testInsertionAndUpdate(){
+/*void testInsertionAndUpdate(){
   Author author = new Author();  
   author.name = 'Dan';
   author.age = 3;
       expect(sonFromObjectory.mother).equals(null);
       callbackDone();
     });    
-  });
+  });  
 }
+*/
 main(){  
   setUpObjectory().then((_) {    
-    objectory.onMessageFuture.then((_){
-      print("Now here ${objectory.socketId}");
-      objectory.save(new Person());    
-      Person person = new Person();
-      person.id = new ObjectId();
-      objectory.remove(person);
-//    print("End ${objectory.webSocket.bufferedAmount}");
-      objectory.webSocket.send("closing");
-      objectory.webSocket.close(123,"Normal closing");      
-      return;
-      });
+//    objectory.onMessageFuture.then((_){
+//      print("Now here ${objectory.socketId}");      
+//      Person person = new Person();
+//      print(person);
+//      person.firstName1 = "asdfasdf";
+//      print(person);
+//      person.fistName = 'Vadim';
+//      objectory.save(person);                
+      1.foo();
+      print("Before closing");
+      objectory.close();      
+//      return;
+//      });
     return;
-    group("ObjectoryVM", ()  {
+  /* 
+   group("ObjectoryVM", ()  {
       asyncTest("testCompoundObject",1,testCompoundObject);   
       asyncTest("testInsertionAndUpdate",1,testInsertionAndUpdate);         
       asyncTest("testObjectWithLinks",1,testObjectWithLinks);               
       test("testNewInstanceMethod",testNewInstanceMethod);   
       test("testMap2ObjectMethod",testMap2ObjectMethod);       
     });  
+*/    
   });    
 }

tests/objectory/PersistentObjectTest.dart

 #library("PersistenObjectTests");
 #import("../../lib/objectory/ObjectoryLib_vm.dart");
 #import("../../lib/bson/bson.dart");
-#import('../../third_party/testing/unittest/unittest_vm.dart');
+#import('../../../../dart/dart-sdk/lib/unittest/unittest.dart');
 #source("DomainModel.dart");
 testAuthorCreation(){
   Author author = new Author();

tests/objectory/allObjectoryTests.dart

 #import("ObjectoryImplVmTest.dart",prefix:"impl");
 #import("PersistentObjectTest.dart",prefix:"persistentObject");
 main(){  
-  impl.main();
+//  impl.main();
   persistentObject.main();
+  impl.main();  
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.