import os
from Util import *
from XMLParser import *
+from TrafficControl import *
paramiko.util.log_to_file('/tmp/paramiko.log')
MSGLEN = 1024
-class Commander:
- def __init__(self):
+class Commander(Thread):
+ def __init__(self, nodes_xml, swarm_xml):
+ self.nodes = Nodes(nodes_xml);
+ self.swarm = Swarm(swarm_xml);
+ self.tc = TrafficControl("openvz");
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)
+ def send_sshcomm(self, hostname, username, port, comm):
+ self.sshc.connect(hostname=hostname, username=username, port=port)
stdin, stdout, stderr = self.sshc.exec_command(comm)
self.sshc.close()
-
+ def send_comm(self, hostname, port, msg_type, config_data):
+ self.sock.connect((hostname, port))
+ self.send(msg_type)
+ response = self.recv_msg()
+ if response == ACK_MSG:
+ self.send_msg(pickle.dumps(config_data))
+ response = self.recv_msg()
+ self.sock.close()
+
+ return response
+
def send(self, msg):
totalsent = 0
while totalsent < len(msg):
if sent == 0:
raise RuntimeError, "socket connection broken"
totalsent = totalsent + sent
+
+ def start_daemon(self, node):
+ comm = "";
+ send_sshcomm(node.public_address, node.username, node.ssh_port, comm):
- def send_command(self, msg_type, config_data):
- self.send(msg_type)
- response = self.recv_msg()
- if response == ACK_MSG:
- self.send_msg(pickle.dumps(config_data))
- response = self.recv_msg()
+ def apply_tc(self, node):
+ si = swarm.getSIByNode(node)
+ tc.config(node.public_address, node.public_port, node.public_iface, \
+ node.private_address, node.private_port, node.private_iface)
+ tc.set_upload_limit(si.upload_limit)
+ tc.set_download_limit(si.downoad_limit)
- return response
-
+ upload_limit_commands = tc.get_upload_limit_commands()
+ download_limit_commands = tc.get_download_limit_commands()
+ flush_commands = tc.get_flush_commands()
+
+ def send_start(self, node):
+
+ #~ config_data = [{CLIENT:"tribler", FILE:"Tribler/Tools/cmdline.py",
+ #~ RUN_TYPE:"script",
+ #~ INTERPRETER:"python", PREFIX:"PYTHONPATH=.",SUFFIX:"",
+ #~ UP_LIMIT_OPTION:"",DL_LIMIT_OPTION:"", PORT_OPTION:"-p",
+ #~ LOG_DIR_OPTION:"-l",DL_DIR_OPTION:"-d"}]
+ response = send_comm(self, node.public_address, node.public_port, START_MSG, config_data):
+
+ def send_stop(self, node):
+ pass
+
+ def send_status(self, node):
+ pass
+
+ def run(self):
+ pass
+
+
#~ def recv_msg(self):
#~ msg = ''
#~ chunk = self.sock.recv(MSGLEN)
class TestCommander:
def __init__(self):
- nodes = Nodes("nodes.xml");
- swarm = Swarm("swarm.xml");
+ nodes = Nodes("../xml/nodes.xml");
+ swarm = Swarm("../xml/swarm.xml");
+ print nodes.getNode("2");
if __name__ == "__main__":
- s = MySocket()
- s.connect(HOST, PORT)
-
- # test config
- config_data = [{CLIENT:"tribler", FILE:"Tribler/Tools/cmdline.py",
- RUN_TYPE:"script",
- INTERPRETER:"python", PREFIX:"PYTHONPATH=.",SUFFIX:"",
- UP_LIMIT_OPTION:"",DL_LIMIT_OPTION:"", PORT_OPTION:"-p",
- LOG_DIR_OPTION:"-l",DL_DIR_OPTION:"-d"}]
+ tc = TestCommander()
+
+
+ #~ # test config
+ #~ config_data = [{CLIENT:"tribler", FILE:"Tribler/Tools/cmdline.py",
+ #~ RUN_TYPE:"script",
+ #~ INTERPRETER:"python", PREFIX:"PYTHONPATH=.",SUFFIX:"",
+ #~ UP_LIMIT_OPTION:"",DL_LIMIT_OPTION:"", PORT_OPTION:"-p",
+ #~ LOG_DIR_OPTION:"-l",DL_DIR_OPTION:"-d"}]
- response = s.send_command(CONFIG_MSG, config_data)
- print response
+ #~ response = s.send_command(CONFIG_MSG, config_data)
+ #~ print response
self.btclient = list[1];
self.upload_limit = list[2];
self.download_limit = list[3];
- self.port = list[4];
- self.download_dir = list[5];
- self.upload_dir = list[6];
+ self.download_dir = list[4];
+ self.log_dir = list[5];
+ self.output_dir = list[6];
def __str__(self):
return '[%s: %s: %s: %s: %s: %s: %s: %s]' \
%(self.id, self.node_id, self.btclient, self.upload_limit, \
- self.download_limit, self.port, self.upload_dir, self.download_dir);
+ self.download_limit, self.download_dir, self.log_dir, self.output_dir);
class Node:
def __init__(self, id, list):
self.id = id;
self.public_address = list[0];
self.public_port = list[1];
- self.private_address = list[2];
- self.private_port = list[3];
- self.ssh_port= list[4];
- self.clients_base_dir = list[5];
+ self.public_iface = list[2];
+ self.private_address = list[3];
+ self.private_port = list[4];
+ self.private_iface = list[5];
+ self.ssh_port = list[6];
+ self.username = list[7];
+ self.listen_port = list[8];
+ self.clients_base_dir = list[9];
def __str__(self):
- return '[%s: %s: %s: %s: %s: %s: %s]' \
- %(self.id, self.public_address, self.public_port, self.private_address, self.private_port, self.ssh_port, self.clients_base_dir);
+ return '[%s: %s: %s: %s: %s: %s %s: %s]' \
+ %(self.id, self.public_address, self.public_port, self.private_address, \
+ self.private_port, self.ssh_port, self.username, self.clients_base_dir);
class Nodes:
def __init__(self, nodes_xml):
tree = etree.parse(swarm_xml)
root = tree.getroot()
self.swarm = {}
- for elem in root:
+ self.torrent_file = root[0].text
+ for elem in root[1:]:
id = elem.get("id")
list = [elem2.text for elem2 in elem[:len(elem)-1]]
self.swarm[id] = SwarmInstance(id, list);
+ print self.swarm[id]
except IOError as e:
print e
return None
-
-
-
-nodes = Nodes("nodes.xml");
-swarm = Swarm("swarm.xml");
-
+
+ def getSIByNode(self, node):
+ for si in self.swarm.values():
+ if si.node_id == node.id:
+ return si
+
+sw = Swarm("swarm.xml");
+print sw.torrent_file
+print sw.getSIByNode("1")
<node id="1">
<public_address>141.85.224.201</public_address>
<public_port>10150</public_port>
+ <public_iface></public_iface>
<private_address>172.30.10.0</private_address>
<private_port>10150</private_port>
+ <private_iface></private_iface>
<ssh_port>10122</ssh_port>
+ <username>gogu</username>
+ <listen_port></listen_port>
<clients>
<client id="tribler">
<base>/home/p2p/p2p-clients/tribler/</base>
<node id="2">
<public_address>141.85.224.202</public_address>
<public_port>10250</public_port>
+ <public_iface></public_iface>
<private_address>172.30.20.0</private_address>
<private_port>10250</private_port>
+ <private_iface></private_iface>
<ssh_port>10222</ssh_port>
+ <username>gicu</username>
+ <listen_port></listen_port>
<clients>
<client id="tribler">
<base>/home/p2p/p2p-clients/tribler/</base>
<?xml version="1.0" encoding="ISO-8859-1"?>
<swarm>
+ <torrent_file>cucu</torrent_file>
<instance id="1">
<node>1</node>
<client>triber</client>
<upload_limit>512</upload_limit>
<download_limit>256</download_limit>
- <port>9999</port>
<download_dir>/this/dir</download_dir>
- <logging_dir>/this/dir</logging_dir>
+ <log_dir></log_dir>
+ <output_dir></output_dir>
<actions>
<action type="start" delay="00:05:00" />
<action type="stop" delay="00:10:00" />
<client>transmission</client>
<upload_limit></upload_limit>
<download_limit></download_limit>
- <port></port>
<download_dir></download_dir>
- <logging_dir></logging_dir>
+ <log_dir></log_dir>
+ <output_dir></output_dir>
<actions>
<action type="start" delay="00:05:00" />
<action type="stop" delay="01:00:00" />
<client>libtorrent</client>
<upload_limit></upload_limit>
<download_limit></download_limit>
- <port></port>
<download_dir></download_dir>
- <logging_dir></logging_dir>
+ <log_dir></log_dir>
+ <output_dir></output_dir>
<actions>
<action type="start" delay="00:00:00" />
<action type="start" delay="end" />