From: Adriana Draghici Date: Fri, 5 Mar 2010 09:48:45 +0000 (+0200) Subject: Server: minor changes X-Git-Tag: getopt_long~147 X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=6c2949f1e5cb7f7d7014c7aa4849d4e6d3def4ba;p=cs-p2p-next.git Server: minor changes --- diff --git a/bt_comm/Util.py b/bt_comm/Util.py index 1db4fd0..d5da785 100644 --- a/bt_comm/Util.py +++ b/bt_comm/Util.py @@ -1,9 +1,8 @@ #!/usr/bin/env python - -#PORT = 1002 -#HOST = "127.0.0.1" +SERVER_PORT = 10004 +SERVER_HOST = "127.0.0.1" """ Message types: client -> server """ @@ -30,3 +29,4 @@ TORRENT = "torrent_file" #clients TRIBLER = "tribler" TRANSMISSION = "transmission" + diff --git a/bt_comm/server/Client.py b/bt_comm/server/Client.py index 11a2560..918e81e 100644 --- a/bt_comm/server/Client.py +++ b/bt_comm/server/Client.py @@ -2,8 +2,7 @@ import sys, socket import pickle from Util import * MSGLEN = 1024 -HOST = "127.0.0.1" -PORT = 10005 +DEBUG = True class MySocket: def __init__(self, sock=None): if sock is None: @@ -51,7 +50,7 @@ class MySocket: # basic test that starts and stops a BT Client def test_all_commands(): s = MySocket() - s.connect(HOST, PORT) + s.connect(SERVER_HOST, SERVER_PORT) start_data = {CLIENT: TRANSMISSION, PORT:10150, TORRENT: "/home/adriana/p2p/p2p-meta/scrubs.torrent", DL_DIR: "/home/adriana/p2p/p2p-dld/transmission", @@ -64,9 +63,8 @@ def test_all_commands(): print response pid = (response.split(" "))[1] print pid - #pid = 8065 s = MySocket() - s.connect(HOST, PORT) + s.connect(SERVER_HOST, SERVER_PORT) response = s.send_command(STOP_MSG, pid) print response diff --git a/bt_comm/server/Server_NO_DAEMON.py b/bt_comm/server/Server_NO_DAEMON.py index 2b33020..18d9461 100644 --- a/bt_comm/server/Server_NO_DAEMON.py +++ b/bt_comm/server/Server_NO_DAEMON.py @@ -5,10 +5,9 @@ import pickle import signal from daemon import Daemon from Util import * +from Util import SERVER_HOST, SERVER_PORT from BitTorrentClientRun import * -PORT = 10005 -HOST = "127.0.0.1" BUFFER_SIZE = 4096 states = {} # keeps track of what kind of message was previously receveid on a socket. @@ -18,7 +17,7 @@ WAITING_START_DATA = 3 WAITING_STOP_DATA = 4 WAITING_STATUS_DATA = 5 - +DEBUG = True def recv_pickled_data(clientsock): # while chunk: @@ -28,7 +27,6 @@ def recv_pickled_data(clientsock): dd = pickle.loads(data) return dd -#TODO def start_bt_client(bt_client_data): @@ -48,52 +46,57 @@ def start_bt_client(bt_client_data): btcr.start() pid = btcr.run_client(btcr.simple_run_command) - #stop_bt_client(pid) + if(DEBUG): + print "Server: started client with pid = ", pid return pid - return -1 - """Simple test - - btcr = BitTorrentClientRun("/usr/bin/transmissioncli", - "$base_path --download-dir $download_dir --port $port $torrent_file > $output_dir/$output_file", - "TR_DEBUG=2 $base_path/cli/transmissioncli --download-dir $download_dir --port $port $torrent_file 2> $log_dir/$log_file > $output_dir/$output_file") - - btcr.config_run("/home/adriana/p2p/p2p-dld/transmission", - "/home/adriana/p2p/p2p-log/transmission", - "scrubs.out", "/home/adriana/p2p/", - "transmission-scrubs.log", 10150, - "/home/adriana/p2p/p2p-meta/scrubs.torrent") - - btcr.start() - btcr.run_client(btcr.simple_run_command) - """ + + return -1 + """Simple test + + btcr = BitTorrentClientRun("/usr/bin/transmissioncli", + "$base_path --download-dir $download_dir --port $port $torrent_file > $output_dir/$output_file", + "TR_DEBUG=2 $base_path/cli/transmissioncli --download-dir $download_dir --port $port $torrent_file 2> $log_dir/$log_file > $output_dir/$output_file") + + btcr.config_run("/home/adriana/p2p/p2p-dld/transmission", + "/home/adriana/p2p/p2p-log/transmission", + "scrubs.out", "/home/adriana/p2p/", + "transmission-scrubs.log", 10150, + "/home/adriana/p2p/p2p-meta/scrubs.torrent") + + btcr.start() + btcr.run_client(btcr.simple_run_command) + """ def stop_bt_client(pid): - print "la kill pid=", pid os.kill(int(pid), signal.SIGKILL) # kill generates zombies os.wait() - print "killed ", pid + if(DEBUG): + print "Server: killed process with pid = ", pid def doServer(): serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - print HOST - print PORT - serversocket.bind((HOST, PORT)); + if(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): - print >> sys.stderr, "inainte de accept" + if(DEBUG): + print "Server: accepting connections" (clientsock, address) = serversocket.accept(); - print >>sys.stderr, "aici" + if(DEBUG): + print "Server: accepted connection from ", address + if clientsock not in states: states[clientsock] = WAITING_MSG_TYPE; if states[clientsock] == WAITING_MSG_TYPE: print states[clientsock] msg = clientsock.recv(BUFFER_SIZE) - print >>sys.stderr, "Server: Received:\n", msg - print >>sys.stderr, "******** END MESSAGE ********" + if(DEBUG): + print "Server: received message:\n", msg msg_types = { START_MSG: WAITING_START_DATA, STOP_MSG: WAITING_STOP_DATA, @@ -109,12 +112,15 @@ def doServer(): if states[clientsock] == WAITING_START_DATA: bt_client_data = recv_pickled_data(clientsock) + if(DEBUG): + print "Server: received message:\n", msg client_pid = start_bt_client(bt_client_data) clientsock.send(ACK_MSG +" "+ str(client_pid)) elif states[clientsock] == WAITING_STOP_DATA: - print >>sys.stderr,"la WAITING_STOP_DATA" client_pid = recv_pickled_data(clientsock) + if(DEBUG): + print "Server: received message:\n", msg stop_bt_client(client_pid) clientsock.send(ACK_MSG) @@ -127,9 +133,6 @@ def doServer(): # clientsock.recv(BUFFER_SIZE) # recv_pickled_data(clientsock) clientsock.close() - #clientsocks.append(clientsock) - #for i in range(0, len(clientsocks)): - # clientsocks[i].close() if __name__ == "__main__": doServer()