class ClientApplication(object): ''' Used for testing the methods that will be available via the RTP-3251 API ''' def __init__(self): pass #Client commands def connect(self, portOfClient=12000, destIp="143.215.129.100", destPort=7000): #Command: connect (only for projects that support bi-directional transfers) #The FTA-client connects to the FTA-server (running at the same IP host). self.clientConnection = Connection() self.clientConnection.open(portOfClient, (destIp, destPort)) def getF(self, fileName): #Command: get F (only for projects that support bi-directional transfers) #The FTA-client downloads file F from the server (if F exists in the same directory as the fta-server executable). fileRequestObj = ['FFFFFFFF', fileName] self.clientConnection.send(pickle.dumps(fileRequestObj)) serialObj = None while(serialObj == None): serialObj = self.clientConnection.receive() fullfilledRequest = pickle.loads(serialObj) if (fullfilledRequest[0] == fileName+"FromServer"): #write the contents (i.e. the second item in the object array f = open(fileName+"FromServer", "w") # file_path = path.relpath("clientFiles/"+fileName) f.write(fullfilledRequest[1]) f.close() print ("Client successfully received", fileName+"FromServer") else: print ("Client received", fullfilledRequest[0], "but was expecting", fileName+"FromServer") def postF(self, fileName): #Command: post F (only for projects that support bi-directional transfers) #The FTA-client uploads file F to the server (if F exists in the same directory as the fta-client executable). f = open(fileName, 'r') obj = [fileName+"AtServer", f.read()] f.close() self.clientConnection.send(pickle.dumps(obj)) serialObj = None while(serialObj == None): serialObj = self.clientConnection.receive() serverReply = pickle.loads(serialObj) if (serverReply[0] == fileName+"AtServer" and serverReply[1] == "confirmed"): print (fileName + " was confirmed") else: print (fileName + " was not confirmed!") def terminate(self): #Command: disconnect (only for projects that support bi-directional transfers) #The FTA-client terminates gracefully from the FTA-server. self.clientConnection.terminate()
def getObjectAt(obj, time): '''return *obj* as it has been at *time*. *time* may be a 'DateTime' object or a time in seconds since epoch (or a serial/transactionid). *obj* and all (direct or indirect) persistent references are as of *time*. Raises 'POSKeyError' when the state cannot be found. ''' c = Connection(obj._p_jar, time) c.open() return c[obj._p_oid]
class ServerApplication(): ''' Used for testing the methods that will be available via the RTP-3251 API ''' def __init__(self, _debug=False): self._debug = _debug pass def openServer(self, portOfServer=12001): self.serverConnection = Connection(self._debug) self.serverConnection.open(portOfServer) def listen(self): while(1): serialObj = None while(serialObj == None): serialObj = self.serverConnection.receive() #stuck until receives a file read or write request requestObj = pickle.loads(serialObj) if (requestObj[0] == "FFFFFFFF"): #client wants file self.replyF(requestObj[1]) else: #client is posting file as ['filename', content] f = open(requestObj[0],'w') f.write(requestObj[1]) f.close() fileConfirmation = [requestObj[0], 'confirmed'] self.serverConnection.send(pickle.dumps(fileConfirmation)) def replyF(self, fileName): #Command: post F (only for projects that support bi-directional transfers) #The FTA-client uploads file F to the server (if F exists in the same directory as the fta-client executable). f = open(fileName, 'r') obj = [fileName+"FromServer", f.read()] f.close() self.serverConnection.send(pickle.dumps(obj)) def terminate(self): #Shuts down FTA-Server gracefully self.serverConnection.terminate()
def connection(): from connection import Connection from session import Session from link import Sender, Receiver, link from protocol import Fragment, Linkage a = Connection(lambda n: Session(n, link)) b = Connection(lambda n: Session(n, link)) a.id = "A" a.tracing = set(["ops", "err"]) b.id = "B" b.tracing = set(["ops", "err"]) def pump(): while a.pending() or b.pending(): b.write(a.read()) a.write(b.read()) s = Session("test-ssn", link) a.add(s) s2 = Session("test-ssn2", link) a.add(s2) a.open(hostname="asdf") b.open() s.begin() s2.begin() l = Sender("qwer", local=Linkage("S", "T")) s.add(l) l.attach() pump() bssn = [x for x in b.incoming.values() if x.name == "test-ssn"][0] bssn.begin() bl = bssn.links["qwer"] bl.attach() bl.flow(10) pump() l.settle(l.send(fragments=Fragment(True, True, 0, 0, "asdf"))) tag = l.send(delivery_tag="blah", fragments=Fragment(True, True, 0, 0, "asdf")) pump() ln = bssn.links["qwer"] x = ln.get() print "INCOMING XFR:", x ln.disposition(x.delivery_tag, "ACCEPTED") xfr = ln.get() print "INCOMING XFR:", xfr ln.disposition(xfr.delivery_tag, "ASDF") print "--" pump() print "--" print "DISPOSITION", l.get_remote(modified=True) l.settle(tag) l.detach() bl.detach() pump() s.end(True) pump() bssn.end(True) s2.end(True) a.close() b.close() pump()
class Connection: def __init__(self, auth=False): self.proto = ProtoConnection(self.session) self.auth = auth if self.auth: self.sasl = SASL(self.proto) else: self.sasl = self.proto self._lock = RLock() self.condition = Condition(self._lock) self.waiter = Waiter(self.condition) self.selector = Selector.default() self.timeout = 120 def tracing(self, *args, **kwargs): self.proto.tracing(*args, **kwargs) self.sasl.tracing(*args, **kwargs) def trace(self, *args, **kwargs): self.proto.trace(*args, **kwargs) @synchronized def connect(self, host, port): sock = socket.socket() sock.connect((host, port)) sock.setblocking(0) self.selector.register(ConnectionSelectable(sock, self, self.tick)) @synchronized def pending(self): return self.sasl.pending() @synchronized def peek(self, n=None): return self.sasl.peek(n) @synchronized def read(self, n=None): return self.sasl.read(n) @synchronized def write(self, bytes): self.sasl.write(bytes) @synchronized def closed(self): self.sasl.closed() @synchronized def error(self, exc): self.sasl.error(exc) @synchronized def tick(self, connection): self.proto.tick() self.sasl.tick() self.waiter.notify() @synchronized def open(self, **kwargs): if not kwargs.get("container_id"): kwargs["container_id"] = str(uuid4()) if "channel_max" not in kwargs: kwargs["channel_max"] = 65535 mechanism = kwargs.pop("mechanism", "ANONYMOUS") username = kwargs.pop("username", None) password = kwargs.pop("password", None) if self.auth: self.sasl.client(mechanism=mechanism, username=username, password=password) self.proto.open(**kwargs) if self.auth: self.wait(lambda: self.sasl.outcome is not None) if self.sasl.outcome != 0: raise Exception("authentication failed: %s" % self.sasl.outcome) def wait(self, predicate, timeout=DEFAULT): if timeout is DEFAULT: timeout = self.timeout self.selector.wakeup() if not self.waiter.wait(predicate, timeout): raise Timeout() @synchronized def session(self): ssn = Session(self) self.proto.add(ssn.proto) return ssn @synchronized def close(self): self.proto.close() self.wait(lambda: self.proto.close_rcvd)
def server(): serverConn = Connection() serverConn.open(12001)
def client(): clientConn = Connection() clientConn.open(12000, ('127.0.0.1',12001)) t = time.clock() while (time.clock() - t < 2): pass clientConn.terminate()