more changes
authorMarius Sandu-Popa <sandupopamarius@gmail.com>
Fri, 29 Jan 2010 15:21:13 +0000 (17:21 +0200)
committerMarius Sandu-Popa <sandupopamarius@gmail.com>
Fri, 29 Jan 2010 15:21:29 +0000 (17:21 +0200)
bt_comm/client/Client.py
bt_comm/client/SSHCommander.py

index df47042..b171e92 100644 (file)
@@ -1,22 +1,26 @@
 import sys, socket
 import pickle
+import paramiko
+import os
 from Util import *
-MSGLEN = 1024
-HOST = "127.0.0.1"
-PORT = 10001
-
-class MySocket:
-       def __init__(self, sock=None):
-               if sock is None:
-                       self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-               else:
-                       self.sock = sock
+from XMLParser import *
+paramiko.util.log_to_file('/tmp/paramiko.log')
 
-       def connect(self, host, port):
-               self.sock.connect((host, port))
-               print "connected to %s %s"%(host, port)
+MSGLEN = 1024
 
-       def send_msg(self, msg):
+class Commander:
+       def __init__(self):
+               self.sshc = paramiko.SSHClient()
+               self.sshc.load_system_host_keys()
+               self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+               
+       def start_daemon(hostname, username, password, comm):
+               self.sshc.connect(hostname=hostname,  username=username, password=password)
+               stdin, stdout, stderr = self.sshc.exec_command(comm)
+               self.sshc.close()
+       
+       
+       def send(self, msg):
                totalsent = 0
                while totalsent < len(msg):
                        sent = self.sock.send(msg[totalsent:])
@@ -25,41 +29,32 @@ class MySocket:
                        totalsent = totalsent + sent
        
        def send_command(self, msg_type, config_data):
-               self.send_msg(msg_type)
+               self.send(msg_type)
                response = self.recv_msg()
                if response == ACK_MSG:
-                       print "aici"
-                       
-                       self.send_dict(config_data)
-                       print "am trimis"
+                       self.send_msg(pickle.dumps(config_data))
                        response = self.recv_msg()
                
                return response
 
-       def recv_msg(self):
-               msg = ''
-               chunk = self.sock.recv(MSGLEN)
-               if chunk == '':
-                               raise RuntimeError,     "socket connection broken"
-               msg = msg + chunk
-               print msg
-               return msg
+       #~ def recv_msg(self):
+       #~ msg = ''
+       #~ chunk = self.sock.recv(MSGLEN)
+       #~ if chunk == '':
+                       #~ raise RuntimeError,  "socket connection broken"
+       #~ msg = msg + chunk
+       #~ print msg
+       #~ return msg
 
-       # send a pickled dictionary
-       def send_dict(self, data):
-               dumped_data = pickle.dumps(data)        
-               self.send_msg(dumped_data)
+class TestCommander:
+       def __init__(self):
+               nodes = Nodes("nodes.xml");
+               swarm = Swarm("swarm.xml");
 
 if __name__ == "__main__":
 
        s = MySocket()
        s.connect(HOST, PORT)
-       # test 1
-       #s.send_msg("hello")
-       # test 2
-       #dd = {'a':1, 'b':2}
-       #s.send_dict(dd)
-       #s.recv_msg()
 
        # test config
        config_data = [{CLIENT:"tribler", FILE:"Tribler/Tools/cmdline.py",
index 716a065..7a06ad1 100644 (file)
@@ -6,6 +6,8 @@ class SSHCommander:
        def __init__(self, hostname, username, password):
                self.sshclient = paramiko.SSHClient()
                self.sshclient.load_system_host_keys()
+       
+       def connect(self):
                self.sshclient.connect(hostname=hostname,  username=username, password=password)
 
        def emitCommand(self, comm):