Commits

SlimerDude  committed 40d5449

- Moved the storage of the TCP Socket into an abstract method on Connection so a different subclass may hold it somewhere other than Actor.locals()
- DB is no longer reliant on a Mongo instance, allowing it to be created with different connection objects

  • Participants
  • Parent commits fd995ed

Comments (0)

Files changed (3)

File fan/Connection.fan

 **
 ** Connection
 **
-internal const class Connection
+abstract const class Connection
 {
-  private const Mongo mongo
-  private const Str id
-  private static const Log log := Log.get("mongo")
+  private const Int port
+  private const Str address
+
   private static const AtomicInt idCounter := AtomicInt(0)
   
-  new make(Mongo m)
+  new make(Str address, Int port)
   {
-    this.mongo = m
-    this.id = "MongoConn-" + Int.random.toHex
+    this.address = address
+    this.port    = port
   }
   
   TcpSocket getSocket()
   {
-    TcpSocket? s := Actor.locals[id]
-    if (s == null) {
-      s = TcpSocket()
-      Actor.locals[id] = s
-    }
+    s := socket
     if (!s.isConnected) {
-      s.connect(IpAddr(mongo.address), mongo.port)
+      s.connect(IpAddr(address), port)
       s.in.endian = s.out.endian = Endian.little
     }
     return s
   
   Void close()
   {
+    socket(false)?.close
+  }
+	
+  abstract protected TcpSocket? socket(Bool forceCreate := true)
+}
+
+internal const class ConnectionStoredInLocals : Connection {
+  private const Str id
+
+  new make(Str address, Int port) : super(address, port) {
+    this.id = "MongoConn-" + Int.random.toHex
+  }
+
+  override TcpSocket? socket(Bool forceCreate := true) {
     TcpSocket? s := Actor.locals[id]
-    if (s != null)
-      s.close
+    if (s == null && forceCreate) {
+      s = TcpSocket()
+      Actor.locals[id] = s
+    }
+    return s
   }
 }
-
-
 const class DB 
 {
   const Str name
-  internal const Mongo mongo
   internal const Connection connection
   private static const Int[] invalidNameChars := [' ', '.', '\$', '/', '\\']
   
   new make(Str name, Mongo mongo)
   {
     this.name = validateName(name)
-    this.mongo = mongo
     this.connection = mongo.connection
   }
+
+  new makeFromConnection(Str name, Connection connection)
+  {
+    this.name = validateName(name)
+    this.connection = connection
+  }
   
   private Str validateName(Str name)
   {

File fan/Mongo.fan

   { 
     this.address = address
     this.port    = port
-    this.connection = Connection(this)
+    this.connection = ConnectionStoredInLocals(address, port)
   }
 
   override Void onStart()