imalse / core / nodes /

Full commit
"""This file defines the Client Command Meta Description"""
import jsonpickle

from core.cmd import CMD
from core.cmd.description import ClientDescription
from core.cmd.command import MsgFromClientToBotCmd

class ClientCMD(CMD):
    This class is a subclass of CMD. It extends the default CMD with commands
    related to a client.
    name = 'client_cmd'
    def __init__(self, desc=None):
        if desc == None:
            desc = ClientDescription()
        CMD.__init__(self, desc)
        self.sock = None

    def srv_addr(self):
        """Returns the server address.""" 
        return self.desc.server_address
    def srv_port(self):
        """Returns the server port.""" 
        return self.desc.server_port

    def request_connect(self):
        """Request a connection to the server."""        
        sock = self.node.create_sock({'type':'server', 'proto':'tcp'})
        self.sock = sock
        if self.node.NODE_TYPE.startswith('real'):
            event = self.node.connect(sock, (self.srv_addr, self.srv_port))
        elif self.node.NODE_TYPE.startswith('sim'):
            event = self.node.connect(sock, (self.srv_addr, self.srv_port))

    def connection_refused(self):
        """Callback when a connection request has been refused by the server.""""connection_refused, try 2 seconds later")
        self.node.sleep(2, self.request_connect)

    def request_time_out(self):
        """Callback when a connection request has timed out""""request_time_out, try 2 seconds later")
        self.node.sleep(2, self.request_connect)

    def recv_disconn_req(self, sock):
        """Callback when a disconnect request from the server is received.""""recv_disconn_req")

    def recv_ack(self):
        """Callback when a 'ack' message is received from the server.""""connection constructed")
        print 'connection constructed'
        self.node.recv(self.sock, 512, self.dispatcher)

    def echo(self, sock, data):
        """Echo a message.""""receive echo message, [%s]" % (data.msg))
        print '-->', data.msg

    def send_to_server(self, msg):
        """Forward data to botmaster"""
        print "sending to server"
        data = MsgFromClientToBotCmd()        
        data.msg = msg
        self.node.send(self.node.server_sock, jsonpickle.encode(data))

    def start(self):
        This method overrides the one in CMD. Start by requesting a connection.