Skip to content

Commit

Permalink
Push context management down into the protocol instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo committed Sep 23, 2016
1 parent dfe8e80 commit 4a350b2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
13 changes: 2 additions & 11 deletions labrad/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from concurrent.futures import Future
from twisted.internet import defer, reactor

from labrad import auth, constants as C, support, thread, types as T
from labrad import constants as C, support, thread, types as T
from labrad.wrappers import getConnection


Expand All @@ -32,12 +32,6 @@ def __init__(self, cxn, spawn_kw=None):
self._spawn_kw = spawn_kw
self._connected = threading.Event()
self._connected.set()
self._nextContext = 1

# This connection can be shared between multiple threads with
# separate Client objects. Access to mutable state needs to
# be protected by a lock.
self._lock = threading.Lock()

# Setup a coroutine that will clear the connected flag when the
# connection is lost. We launch this but do not wait for the result of
Expand Down Expand Up @@ -68,10 +62,7 @@ def spawn(self):

def context(self):
"""Create a new context for use with this connection"""
with self._lock:
ctx = 0, self._nextContext
self._nextContext += 1
return ctx
return _call_future(self.cxn.context).result()

def sendRequest(self, target, records, *args, **kw):
return _call_future(self.cxn.sendRequest, target, records, *args, **kw)
Expand Down
7 changes: 7 additions & 0 deletions labrad/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class LabradProtocol(protocol.Protocol):

def __init__(self):
self.disconnected = False
self._next_context = 1
self._nextRequest = 1
self.pool = set()
self.requests = {}
Expand Down Expand Up @@ -65,6 +66,12 @@ def set_address(self, host, port):
self.host = host
self.port = port

def context(self):
"""Create a new communication context for this connection."""
context = (0, self._next_context)
self._next_context += 1
return context

# network events
def connectionMade(self):
# set the SO_KEEPALIVE option on all connections
Expand Down
5 changes: 1 addition & 4 deletions labrad/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ def __init__(self, prot):
self._cache = {}
self._cxn = prot
self._mgr = manager.AsyncManager(self._cxn)
self._next_context = 1
self._refreshLock = defer.DeferredLock()

_staticAttrs = ['servers', 'refresh', 'context']
Expand Down Expand Up @@ -548,9 +547,7 @@ def _delServer(self, name):

def context(self):
"""Create a new communication context for this connection."""
context = (0, self._next_context)
self._next_context += 1
return context
return self._cxn.context()

def __getitem__(self, key):
return self.servers[key]
Expand Down

0 comments on commit 4a350b2

Please sign in to comment.