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):
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)
}
- 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
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
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.
[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
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:
# 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')
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
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):
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)