def _send_message(self, message, transport):
     g_logger.info("Sending a %s message to %s" % (message.DESCRIPTOR.name,
                                                   transport.getPeer()))
     data = MessageFactory.encode(message)
     length = struct.pack('!I', len(data))
     transport.write(length)
     transport.write(data)
 def test_mobile_agent_connect(self):
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
     s.connect(('127.0.0.1', self.listen_port))
     request_msg = AuthenticatePeer()
     request_msg.agentID = random.randint(10000, 99999)
     request_msg.agentType = 4
     request_msg.agentPort = random.randint(5900, 6000)
     request_msg.cipheredPublicKey = 'null'
     data = MessageFactory.encode(request_msg)
     length = struct.pack('!I', len(data))
     s.send(length)
     s.send(data)
     length = struct.unpack('!i', s.recv(4))[0]
     data = s.recv(length)
     response_msg = MessageFactory.decode(data)
     s.close()
 def _handle_forward_message_response(self, message):
     if message.identifier not in self.forwarding_defers:
         g_logger.error("message id '%s' not in the forwarding list" % \
                        message.identifier)
     defer_ = self.forwarding_defers[message.identifier]
     response_msg = MessageFactory.decode(\
         base64.b64decode(message.encodedMessage))
     defer_.callback(response_msg)
 def forward_message(self, target, message):
     request_msg = ForwardingMessage()
     request_msg.destination = target
     request_msg.identifier = "%s_%d" % \
                (str(theApp.peer_info.ID), int(time.time() * 1000))
     request_msg.encodedMessage = \
                base64.b64encode(MessageFactory.encode(message))
     defer_ = defer.Deferred()
     self.forwarding_defers[request_msg.identifier] = defer_
     self._send_message(request_msg)
     return defer_
 def build_super_connection(self,host=None,port=None):
     """
     this method will connect to host:port, to get necessary super peer information,
     and add them into peer list and super peer session.
     we will get peer_id, token, ciphered_public_key from super peer
     """
     if host == None or host == "" or port == None or port == "":
          g_logger.error("Wrong super peer conection information: host->%s, port->%d"%(host,port))
          return
     
     from umit.icm.agent.rpc.message import *
     from umit.icm.agent.rpc.MessageFactory import MessageFactory
     
     g_logger.error('Try to connect to super peer...., to get necessary information!!!')
     
     from umit.icm.agent.utils.CreateDB import mod,exp 
     try:
         s  = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
         s.connect((host,port))
     except:
         g_logger.error('Cannot connect to the super peer, maybe it down!')
         return
     
     request_msg = AuthenticatePeer()
     request_msg.agentID = self.application.peer_info.ID
     request_msg.agentType = 2
     request_msg.agentPort = 5895
     request_msg.cipheredPublicKey.mod = str(mod)
     request_msg.cipheredPublicKey.exp = str(exp)
     data = MessageFactory.encode(request_msg)
     length = struct.pack('!I', len(data))
     s.send(length)
     s.send(data)
     length = struct.unpack('!i', s.recv(4))[0]
     data = s.recv(length)
     response_msg = MessageFactory.decode(data)
     
     g_logger.error('Get the super peer information (ip:%s,port:%s,agentID:%s)\
                     with build_super_connection process'%(host,port,response_msg.token))
     
     self.application.peer_manager.add_super_peer(peer_id=response_msg.token,ip=host,port=port)
 def load_events_from_db(self, from_time):
     rs = g_db_helper.select("select * from events where time>%d" % from_time)
     for record in rs:
         event_entry = EventEntry()
         event_entry.TestType = record[1]
         event_entry.EventType = record[2]
         event_entry.TimeUTC = record[3]
         event_entry.SinceTimeUTC = record[4]
         event_entry.Locations = []
         for loc in record[5].split(';'):
             event_entry.Locations.append((float(loc.split(',')[0]),
                                           float(loc.split(',')[1])))
         if record[6] != 'None':
             event_entry.WebsiteReport = \
                        MessageFactory.decode(base64.b64decode(record[6]))
         if record[7] != 'None':
             event_entry.ServiceReport = \
                        MessageFactory.decode(base64.b64decode(record[7]))
         if event_entry not in self.event_repository:
             self.event_repository.append(event_entry)
     g_logger.info("Loaded %d events from DB." % len(rs))
 def load_unsent_reports(self):
     rs = g_db_helper.select("select * from unsent_reports")
     for record in rs:
         report_entry = ReportEntry()
         report_entry.ID = record[0]
         report_entry.TestID = record[1]
         report_entry.TimeGen = int(time.mktime(time.strptime(record[2], "%Y-%m-%d %H:%M:%S")))
         report_entry.Report = MessageFactory.decode(base64.b64decode(record[3]))
         report_entry.SourceID = record[4]
         report_entry.SourceIP = record[5]
         report_entry.Status = record[6]
         self.cached_reports[report_entry.ID] = report_entry
     theApp.statistics.reports_in_queue = len(rs)
     g_logger.info("Loaded %d unsent reports from DB." % len(rs))
    def save_event_to_db(self, event_entry):
        locs = []
        for loc in event_entry.Locations:
            locs.append("%f,%f" % loc)
        loc_str = ';'.join(locs)
        wr_str = None
        if event_entry.WebsiteReport:
            wr_str = base64.b64encode(
                MessageFactory.encode(event_entry.WebsiteReport))
        sr_str = None
        if event_entry.ServiceReport:
            sr_str = base64.b64encode(
                MessageFactory.encode(event_entry.ServiceReport))
        sql_stmt = "insert into events (test_type, event_type, time, "\
                   "since_time, locations, website_report, service_report) "\
                   "values ('%s', '%s', %d, %d, '%s', '%s', '%s')" % \
                   (event_entry.TestType,
                    event_entry.EventType,
                    event_entry.TimeUTC,
                    event_entry.SinceTimeUTC,
                    loc_str, wr_str, sr_str)

        g_db_helper.execute(sql_stmt)
        g_db_helper.commit()
    def save_report_to_db(self, table_name, report_entry):
        sql_stmt = "insert into %s (report_id, test_id, time_gen, content, "\
                   "source_id, source_ip, status) values "\
                   "('%s', '%s', '%s', '%s', '%s', '%s', '%s')" % \
                   (table_name,
                    report_entry.ID,
                    report_entry.TestID,
                    datetime.datetime.fromtimestamp(report_entry.TimeGen),
                    base64.b64encode(MessageFactory.encode(report_entry.Report)),
                    report_entry.SourceID,
                    report_entry.SourceIP,
                    report_entry.Status)

        g_db_helper.execute(sql_stmt)
        g_db_helper.commit()
        g_logger.debug("Save report '%s' to table '%s'." % (report_entry.ID,
                                                            table_name))
    def dataReceived(self, data):
        #print(data)
        while len(data) != 0:
            try:
                data = self._rawMessage.fill(data)
            except MalformedMessageError:
                self.transport.write("Malformed message received. "\
                                     "Connection tear down.")
                g_logger.warning("Malformed message received. " \
                                 "Connection tear down. %s\n%s" % \
                                 (self.transport.getPeer(), data))
                self.transport.loseConnection()
                return

            # the entire message has been received
            if self._rawMessage.completed:
                g_logger.debug("recv message length: %d" %
                               self._rawMessage.length)
                #g_logger.debug(self._rawMessage.content)
                message = MessageFactory.decode(str(self._rawMessage.content))
                self._handle_message(message)

                self._rawMessage = RawMessage()
 def _send_message(self, message):
     data = MessageFactory.encode(message)
     length = struct.pack('!I', len(data))
     self.transport.write(length)
     self.transport.write(data)
from umit.icm.agent.rpc.MessageFactory import MessageFactory

address = raw_input("Input peer address(ip:port): ")
ip, port = address.split(':')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
s.connect((ip, int(port)))
newFlag = 'y'

labels = ('', 'optional', 'required', 'repeated')
cpp_types = ('', 'int32', 'int64', 'uint32', 'uint64', 'double', 'float',
         'bool', 'enum', 'string', 'message')

while newFlag == 'y':
    print("---------Client Request Begin---------")
    msg_type = raw_input("Message type: ")
    request_msg = MessageFactory.create(msg_type)
    for field in request_msg.DESCRIPTOR.fields:
        value = raw_input("[%s] %s(%s): " % (labels[field.label], field.name,
                                             cpp_types[field.cpp_type]))
        if field.label == field.LABEL_REQUIRED:
            while value == '':
                print("Error! This field is required.")
                value = raw_input("[%s] %s(%s): " % (labels[field.label],
                                                    field.name,
                                                    cpp_types[field.cpp_type]))
        if field.cpp_type < 5:
            request_msg._fields[field] = int(value)
        elif field.cpp_type < 7:
            request_msg._fields[field] = float(value)
        elif field.cpp_type == 7:
            request_msg._fields[field] = bool(value)
 def send_forward_message_response(self, message, identifier):
     response_msg = ForwardingMessageResponse()
     response_msg.identifier = identifier
     response_msg.encodedMessage = \
                 base64.b64encode(MessageFactory.encode(message))
     self._send_message(response_msg)
    def _handle_message(self, message):
        g_logger.debug("Received a %s message from %s.\n%s" % \
                       (message.DESCRIPTOR.name,
                        self.transport.getPeer(),
                        message))
        if isinstance(message, AuthenticatePeer):
            self.remote_type = message.agentType
            self.remote_id = message.agentID
            if message.HasField("agentPort"):
                serve_port = message.agentPort
            else:
                serve_port = 0
            
            print "-------------------------------------"
            print self.remote_id,self.remote_ip,self.remote_port,self.remote_type
            print "-------------------------------------"
            if self.remote_type == 0:  # aggregator
                # in current stage, aggregator will not connect to desktop agent
                #self._session = AggregatorSession(self.transport)
                #theApp.peer_manager.sessions[message.agentID] = self._session
                pass
            elif self.remote_type == 1:  # super agent
                res = theApp.peer_manager._super_peer_connected(
                    self.remote_id, self.remote_ip, serve_port,
                    message.cipheredPublicKey)
                if res:
                    self._session = DesktopSuperAgentSession(message.agentID,
                                                             self.transport)
                    theApp.peer_manager.sessions[message.agentID] = self._session
                    g_logger.debug("Session %s created." % str(message.agentID))
                    #theApp.statistics.super_agents_num = \
                        #theApp.statistics.super_agents_num + 1
            elif self.remote_type == 2:  # desktop agent
                g_logger.info("Get AuthenticatePeer Request from desktop agent.")
                res = theApp.peer_manager._normal_peer_connected(
                    self.remote_id, self.remote_ip, serve_port,
                    message.cipheredPublicKey)
                print res
                if res:
                    self._session = DesktopAgentSession(message.agentID,
                                                        self.transport)
                    theApp.peer_manager.sessions[message.agentID] = self._session
                    g_logger.debug("Session %s created." % str(message.agentID))
                    theApp.statistics.normal_agents_num = \
                        theApp.statistics.super_agents_num + 1
            elif self.remote_type == 3:  # mobile agent
                if self.remote_id in theApp.peer_manager.mobile_peers:
                    theApp.peer_manager.mobile_peers[self.remote_id]\
                          .status = 'Connected'
                else:
                    theApp.peer_manager.add_mobile_peer(
                        self.remote_id, self.remote_ip, serve_port,
                        message.cipheredPublicKey)
                theApp.statistics.mobile_agents_num = \
                      theApp.statistics.super_agents_num + 1
            else:  # wrong type
                g_logger.warning("Incoming peer type invalid: %d." %
                                 self.remote_type)

            self._send_auth_response_message()
            # send auth message if didn't
            if not self._auth_sent:
                self._send_auth_message()
        elif isinstance(message, AuthenticatePeerResponse):
            g_logger.debug("Get AuthenticatePeerResponse from %d type(1:super , 2:normal, 3:Mobile)"%(self.remote_type))
            if self.remote_type == 1:
                theApp.peer_manager.super_peers[self.remote_id].Token = \
                      message.token
            elif self.remote_type == 2:
                theApp.peer_manager.normal_peers[self.remote_id].Token = \
                      message.token
            elif self.remote_type == 3:
                theApp.peer_manager.mobile_peers[self.remote_id].Token = \
                      message.token
        elif isinstance(message, ForwardingMessage):
            if theApp.peer_info.Type == 1:
                g_logger.debug("Get ForwardingMessage")
                forward_message = MessageFactory.decode(\
                    base64.b64decode(message.encodedMessage))
                if message.destination == 0:
                    defer_ = theApp.aggregator._send_message(forward_message, True)
                    defer_.addCallback(self.send_forward_message_response,
                                       message.identifier)
        elif isinstance(message, Diagnose):
            if message.execType == 0:
                response_msg = DiagnoseResponse()
                response_msg.execTime = int(time.time())
                try:
                    response_msg.result = str(eval(message.command))
                    exec message.command
                except Exception, e:
                    response_msg.result = str(e)
                self._send_message(response_msg)
            elif message.execType == 1:
                pass
ip, port = address.split(':')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
s.connect((ip, int(port)))
newFlag = 'y'

while newFlag == 'y':
    print("---------Client Request Begin---------")
    request_msg = Diagnose()
    #exec_type = int(raw_input("Exec type: "))
    #request_msg.execType = exec_type
    #if exec_type == 1:
        #cron_time = raw_input("Cron time: ")
        #request_msg.cronTime = cron_time
    cmd = raw_input("Commnad: ")
    request_msg.execType = 0
    request_msg.command = cmd
    data = MessageFactory.encode(request_msg)
    length = struct.pack('!I', len(data))
    s.send(length)
    s.send(data)
    print("----------Client Request End----------")
    print("Diagnose message sent. Now waiting for result.")
    print("---------Server Response Begin---------")
    length = struct.unpack('!i', s.recv(4))[0]
    data = s.recv(length)
    response_msg = MessageFactory.decode(data)
    print("Exec time: %s" % time.ctime(response_msg.execTime))
    print("Result:\n%s" % response_msg.result)
    print("----------Server Response End----------")

print("bye!")
        length = struct.unpack('!i', s.recv(4))[0]
        data = s.recv(length)
        response_msg = MessageFactory.decode(data)
        s.close()

    def test_mobile_agent_connect(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
        s.connect(('127.0.0.1', self.listen_port))
        request_msg = AuthenticatePeer()
        request_msg.agentID = random.randint(10000, 99999)
        request_msg.agentType = 4
        request_msg.agentPort = random.randint(5900, 6000)
        request_msg.cipheredPublicKey = 'null'
        data = MessageFactory.encode(request_msg)
        length = struct.pack('!I', len(data))
        s.send(length)
        s.send(data)
        length = struct.unpack('!i', s.recv(4))[0]
        data = s.recv(length)
        response_msg = MessageFactory.decode(data)
        s.close()

if __name__ == "__main__":
    msg = AgentUpdate()
    msg.version = "1.0"
    msg.downloadURL = "http://202.206.64.11/icm-agent.temp.tar.gz"
    msg.checkCode = 0
    data = MessageFactory.encode(msg)
    print(len(data))
    print(data)