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()
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()
Exemple #3
0
def client():
    clientConn = Connection()
    clientConn.open(12000, ('127.0.0.1',12001))
    t = time.clock()
    while (time.clock() - t < 2): pass
    clientConn.terminate()