From 6661ac399def1693e39d53f86cbb9e3078cc3e51 Mon Sep 17 00:00:00 2001 From: Adriana Draghici Date: Thu, 15 Apr 2010 11:50:24 +0000 Subject: [PATCH] autorun: Server modifications for the revised protcol. --- autorun/Util.py | 9 ++- autorun/server/Client.py | 33 +++++++---- autorun/server/Server.py | 88 ++++++++++++------------------ autorun/server/Server_NO_DAEMON.py | 59 +++++++------------- 4 files changed, 82 insertions(+), 107 deletions(-) diff --git a/autorun/Util.py b/autorun/Util.py index 9624d81..d254d4b 100644 --- a/autorun/Util.py +++ b/autorun/Util.py @@ -1,12 +1,10 @@ - #!/usr/bin/env python SERVER_DIR = "/home/p2p/cs-p2p-next/autorun/server/" SERVER_FILE = "Server.py" SERVER_TYPE = "python" SERVER_PORT = 10004 -#SERVER_HOST = "127.0.0.1" -SERVER_HOST = "172.16.20.3" +SERVER_HOST = "127.0.0.1" """ Message types: client -> server """ @@ -14,8 +12,8 @@ START_MSG = "1"; STOP_MSG = "2"; STATUS_MSG = "3"; -ACK_MSG = "ACK" # send by server to the client after receiving a message if everything was ok -ERROR_MSG = "ERROR " # send by server to the client if the parameters were incorrect +ACK_MSG = "1" # send by server to the client after receiving a message if everything was ok +ERROR_MSG = "0" # send by server to the client if the parameters were incorrect # or the client could not be started etc #xml tags from swarm.xml @@ -33,4 +31,5 @@ TORRENT = "torrent_file" #clients TRIBLER = "tribler" TRANSMISSION = "transmission" +HRKTORRENT = "hrktorrent" diff --git a/autorun/server/Client.py b/autorun/server/Client.py index 943bdc3..0e08616 100644 --- a/autorun/server/Client.py +++ b/autorun/server/Client.py @@ -22,15 +22,19 @@ class MySocket: raise RuntimeError, "socket connection broken" totalsent = totalsent + sent + def send_pickled_data (self, data): + dumped_data = pickle.dumps(data) + totalsent = 0 + while totalsent < len(dumped_data): + sent = self.sock.send(dumped_data[totalsent:]) + if sent == 0: + raise RuntimeError, "socket connection broken" + totalsent = totalsent + sent + def send_command(self, msg_type, config_data): - self.send_msg(msg_type) + self.send_pickled_data((msg_type, config_data)) print "am trimis ", msg_type - response = self.recv_msg() - if response == ACK_MSG: - self.send_dict(config_data) - print "am trimis" - response = self.recv_msg() - + response = self.recv_pickled_data() return response def recv_msg(self): @@ -39,9 +43,16 @@ class MySocket: if chunk == '': raise RuntimeError, "socket connection broken" msg = msg + chunk - print "am primit ", msg return msg + def recv_pickled_data(self): + # while chunk: + # chunk = clientsock.recv(BUFFER_SIZE) + # data += chunk + data = self.recv_msg() + dd = pickle.loads(data) + return dd + # send a pickled dictionary def send_dict(self, data): dumped_data = pickle.dumps(data) @@ -85,13 +96,13 @@ def test_all_commands(torrent_file): } - response = s.send_command(START_MSG, start_data_transmission) + response = s.send_command(START_MSG, start_data_tribler) print response - pid = (response.split(" "))[1] + pid = response[1] print pid s = MySocket() s.connect(SERVER_HOST, SERVER_PORT) - time.sleep(100) + time.sleep(20) response = s.send_command(STOP_MSG, pid) print response diff --git a/autorun/server/Server.py b/autorun/server/Server.py index cf4b6c2..66cbb11 100644 --- a/autorun/server/Server.py +++ b/autorun/server/Server.py @@ -10,24 +10,19 @@ from BitTorrentClientRun import * from TransmissionRun import * from TriblerRun import * -DEBUG = True class MyDaemon(Daemon): ip = "" port = 0 BUFFER_SIZE = 4096 + DEBUG = True - states = {} # keeps track of what kind of message was previously receveid on a socket. processes = {} # keeps lists of file descriptors for each process - WAITING_MSG_TYPE = 0 - WAITING_START_DATA = 3 - WAITING_STOP_DATA = 4 - WAITING_STATUS_DATA = 5 - def __init__(self, pidfile, ip='', port = 0, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): + def __init__(self, pidfile, ip='', stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): Daemon.__init__(self, pidfile, stdin, stdout, stderr) self.ip = ip - self.port = port + self.port = SERVER_PORT def set_address(self, ip): self.ip = ip @@ -47,6 +42,14 @@ class MyDaemon(Daemon): data = clientsock.recv(self.BUFFER_SIZE) dd = pickle.loads(data) return dd + def send_pickled_data (self, clientsock, data): + dumped_data = pickle.dumps(data) + totalsent = 0 + while totalsent < len(dumped_data): + sent = clientsock.send(dumped_data[totalsent:]) + if sent == 0: + raise RuntimeError, "socket connection broken" + totalsent = totalsent + sent """ Starts a process for a BitTorrent client and returns its pid. @@ -73,7 +76,7 @@ class MyDaemon(Daemon): [pid, log_fd, output_fd] = btcr.run_client(btcr.simple_run_command) self.processes[pid] = (log_fd, output_fd) print self.processes[pid] - if(DEBUG): + if(self.DEBUG): print "Server: started client with pid = ", pid return pid @@ -98,70 +101,49 @@ class MyDaemon(Daemon): self.processes[int_pid][0].close() self.processes[int_pid][1].close() del self.processes[int_pid] - if(DEBUG): + if(self.DEBUG): print "Server: killed process with pid = ", pid def doServer(self, ip, port): serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - if(DEBUG): - print "Server: host ip = %s, port = %d"%(ip,port) - serversocket.bind((ip,port)); + if(self.DEBUG): + print "Server: host ip = %s, port = %d"%(SERVER_HOST,SERVER_PORT) + serversocket.bind((SERVER_HOST, SERVER_PORT)); serversocket.listen(10) #max 10 requests - clientsocks = [] while(1): - if(DEBUG): + if(self.DEBUG): print "Server: accepting connections" (clientsock, address) = serversocket.accept(); - if(DEBUG): + if(self.DEBUG): print "Server: accepted connection from ", address - if clientsock not in self.states: - self.states[clientsock] = self.WAITING_MSG_TYPE; + msg = self.recv_pickled_data(clientsock) + if(self.DEBUG): + print "Server: received message:\n", msg + - if self.states[clientsock] == self.WAITING_MSG_TYPE: - print self.states[clientsock] - msg = clientsock.recv(self.BUFFER_SIZE) - if(DEBUG): - print "Server: received message:\n", msg - msg_types = { - START_MSG: self.WAITING_START_DATA, - STOP_MSG: self.WAITING_STOP_DATA, - STATUS_MSG: self.WAITING_STATUS_DATA - } - if msg not in msg_types: - clientsock.send(ERROR_MSG +"wrong message type " + msg) - else: - self.states[clientsock] = msg_types[msg] - clientsock.send(ACK_MSG) - #else: - print self.states[clientsock] - - if self.states[clientsock] == self.WAITING_START_DATA: - bt_client_data = self.recv_pickled_data(clientsock) - if(DEBUG): - print "Server: received message:\n", msg + if msg[0] == START_MSG: + bt_client_data = msg[1] client_pid = self.start_bt_client(bt_client_data) - clientsock.send(ACK_MSG +" "+ str(client_pid)) + self.send_pickled_data(clientsock, (ACK_MSG, client_pid)) - elif self.states[clientsock] == self.WAITING_STOP_DATA: - client_pid = self.recv_pickled_data(clientsock) - if(DEBUG): + elif msg[0] == STOP_MSG: + client_pid = msg[1] + if(self.DEBUG): print "Server: received message:\n", msg self.stop_bt_client(client_pid) - clientsock.send(ACK_MSG) - - elif self.states[clientsock] == self.WAITING_STATUS_DATA: - config = self.recv_pickled_data(clientsock) - clientsock.send(ACK_MSG) + self.send_pickled_data(clientsock, (ACK_MSG,"")) + + else: - self.states[clientsock] = self.WAITING_MSG_TYPE + self.send_pickled_data(clientsock,( ERROR_MSG,"Error: wrong message type ")) - # clientsock.recv(self.BUFFER_SIZE) + # clientsock.recv(BUFFER_SIZE) # recv_pickled_data(clientsock) clientsock.close() + if __name__ == "__main__": if len(sys.argv) >= 2: @@ -172,7 +154,7 @@ if __name__ == "__main__": # sys.exit(2) #daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], int(sys.argv[3]), stdout = '/home/p2p/out', stderr = 'home/p2p/err') - daemon = MyDaemon('/tmp/daemon-example.pid', SERVER_HOST, SERVER_PORT, stdout = '/home/p2p/out', stderr = 'home/p2p/err') + daemon = MyDaemon('/tmp/daemon-example.pid', SERVER_HOST, stdout = '/home/p2p/out', stderr = '/home/p2p/err') daemon.start() elif 'stop' == sys.argv[1]: daemon = MyDaemon('/tmp/daemon-example.pid') diff --git a/autorun/server/Server_NO_DAEMON.py b/autorun/server/Server_NO_DAEMON.py index 2745331..fde9001 100644 --- a/autorun/server/Server_NO_DAEMON.py +++ b/autorun/server/Server_NO_DAEMON.py @@ -11,12 +11,7 @@ from TransmissionRun import * from TriblerRun import * BUFFER_SIZE = 4096 -states = {} # keeps track of what kind of message was previously receveid on a socket. processes = {} -WAITING_MSG_TYPE = 0 -WAITING_START_DATA = 3 -WAITING_STOP_DATA = 4 -WAITING_STATUS_DATA = 5 DEBUG = True @@ -28,6 +23,14 @@ def recv_pickled_data(clientsock): dd = pickle.loads(data) return dd +def send_pickled_data (clientsock, data): + dumped_data = pickle.dumps(data) + totalsent = 0 + while totalsent < len(dumped_data): + sent = clientsock.send(dumped_data[totalsent:]) + if sent == 0: + raise RuntimeError, "socket connection broken" + totalsent = totalsent + sent def start_bt_client(bt_client_data): @@ -95,46 +98,26 @@ def doServer(): if(DEBUG): print "Server: accepted connection from ", address - if clientsock not in states: - states[clientsock] = WAITING_MSG_TYPE; + msg = recv_pickled_data(clientsock) + if(DEBUG): + print "Server: received message:\n", msg + - if states[clientsock] == WAITING_MSG_TYPE: - print states[clientsock] - msg = clientsock.recv(BUFFER_SIZE) - if(DEBUG): - print "Server: received message:\n", msg - msg_types = { - START_MSG: WAITING_START_DATA, - STOP_MSG: WAITING_STOP_DATA, - STATUS_MSG: WAITING_STATUS_DATA - } - if msg not in msg_types: - clientsock.send(ERROR_MSG +"wrong message type " + msg) - else: - states[clientsock] = msg_types[msg] - clientsock.send(ACK_MSG) - #else: - print states[clientsock] - - if states[clientsock] == WAITING_START_DATA: - bt_client_data = recv_pickled_data(clientsock) - if(DEBUG): - print "Server: received message:\n", msg + if msg[0] == START_MSG: + bt_client_data = msg[1] client_pid = start_bt_client(bt_client_data) - clientsock.send(ACK_MSG +" "+ str(client_pid)) + send_pickled_data(clientsock, (ACK_MSG, client_pid)) - elif states[clientsock] == WAITING_STOP_DATA: - client_pid = recv_pickled_data(clientsock) + elif msg[0] == STOP_MSG: + client_pid = msg[1] if(DEBUG): print "Server: received message:\n", msg stop_bt_client(client_pid) - clientsock.send(ACK_MSG) - - elif states[clientsock] == WAITING_STATUS_DATA: - config = recv_pickled_data(clientsock) - clientsock.send(ACK_MSG) + send_pickled_data(clientsock, (ACK_MSG,"")) + + else: - states[clientsock] = WAITING_MSG_TYPE + send_pickled_data(clientsock,( ERROR_MSG,"Error: wrong message type ")) # clientsock.recv(BUFFER_SIZE) # recv_pickled_data(clientsock) -- 2.20.1