imalse / core / nodes / client.py

"""This file defines the Client Command Meta Description"""

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

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):
        """Initialize"""
        if desc == None:
            desc = ClientDescription()
            
        CMD.__init__(self, desc)
        self.sock = None

    @property
    def srv_addr(self):
        """Returns the server address.""" 
        return self.desc.server_address
    @property
    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))
            self.trigger(event)
        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."""
        self.logger.info("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"""
        self.logger.info("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."""
        self.logger.info("recv_disconn_req")
        self.node.close_sock(sock)

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

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

    def start(self):
        """
        
        This method overrides the one in CMD. Start by requesting a connection.
        
        """
        self.request_connect()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.