From 69a96fe65114dbdcc1449715b815489c410dd228 Mon Sep 17 00:00:00 2001 From: Adriana Draghici Date: Mon, 15 Feb 2010 17:26:13 +0200 Subject: [PATCH] server - implemented stop client, not working --- bt_comm/Util.py | 4 ++++ bt_comm/server/Client.py | 10 +++++---- bt_comm/server/Server_NO_DAEMON.py | 33 ++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/bt_comm/Util.py b/bt_comm/Util.py index 0fc72a7..1db4fd0 100644 --- a/bt_comm/Util.py +++ b/bt_comm/Util.py @@ -1,6 +1,10 @@ #!/usr/bin/env python + +#PORT = 1002 +#HOST = "127.0.0.1" + """ Message types: client -> server """ START_MSG = "1"; diff --git a/bt_comm/server/Client.py b/bt_comm/server/Client.py index e8c9003..939d6f2 100644 --- a/bt_comm/server/Client.py +++ b/bt_comm/server/Client.py @@ -3,8 +3,7 @@ import pickle from Util import * MSGLEN = 1024 HOST = "127.0.0.1" -PORT = 10001 - +PORT = 10005 class MySocket: def __init__(self, sock=None): if sock is None: @@ -78,6 +77,9 @@ if __name__ == "__main__": } response = s.send_command(START_MSG, start_data) - print response - + + print response + pid = (response.split())[1] + print "pid = ", pid + response = s.send_command(STOP_MSG, response) diff --git a/bt_comm/server/Server_NO_DAEMON.py b/bt_comm/server/Server_NO_DAEMON.py index ea83ef6..8375649 100644 --- a/bt_comm/server/Server_NO_DAEMON.py +++ b/bt_comm/server/Server_NO_DAEMON.py @@ -6,9 +6,8 @@ from daemon import Daemon from Util import * from BitTorrentClientRun import * -PORT = 10001 +PORT = 10005 HOST = "127.0.0.1" - BUFFER_SIZE = 4096 states = {} # keeps track of what kind of message was previously receveid on a socket. @@ -43,8 +42,10 @@ def start_bt_client(bt_client_data): bt_client_data[TORRENT]) btcr.start() - btcr.run_client(btcr.simple_run_command) - + pid = btcr.run_client(btcr.simple_run_command) + return pid + + return -1 """Simple test btcr = BitTorrentClientRun("/usr/bin/transmissioncli", @@ -60,10 +61,18 @@ def start_bt_client(bt_client_data): btcr.start() btcr.run_client(btcr.simple_run_command) """ + +def stop_bt_client(pid): + os.kill(pid, signal.SIGKILL) # kill generates zombies + os.wait() + print "killed ", pid + def doServer(): - #os.spawnvp(os.P_NOWAIT,"/usr/bin/transmission",[]) serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + print HOST + print PORT serversocket.bind((HOST, PORT)); serversocket.listen(10) #max 10 requests @@ -93,22 +102,24 @@ def doServer(): if states[clientsock] == WAITING_START_DATA: bt_client_data = recv_pickled_data(clientsock) - start_bt_client(bt_client_data) + client_pid = start_bt_client(bt_client_data) + clientsock.send(ACK_MSG +" "+ str(client_pid)) elif states[clientsock] == WAITING_STOP_DATA: - config = recv_pickled_data(clientsock) + client_pid = recv_pickled_data(clientsock) + stop_bt_client(client_pid) + clientsock.send(ACK_MSG) elif states[clientsock] == WAITING_STATUS_DATA: config = recv_pickled_data(clientsock) + clientsock.send(ACK_MSG) states[clientsock] = WAITING_MSG_TYPE - # clientsock.recv(BUFFER_SIZE) # recv_pickled_data(clientsock) - clientsock.send(ACK_MSG) - print "am trimis ack" - clientsock.close() + + clientsock.close() if __name__ == "__main__": doServer() -- 2.20.1