# Path: umit/icm/agent/rpc/MessageFactory.py
# class MessageFactory(object):
#     """"""
#     _reader = BinaryReader()
#     _writer = BinaryWriter()
# 
#     #----------------------------------------------------------------------
#     #def __init__(self):
#         #"""Constructor"""
# 
#     @classmethod
#     def create(cls, msg_type):
#         return message_creator[msg_type]()
# 
#     @classmethod
#     def encode(cls, message):
#         cls._writer.reset()
#         msg_type = message.DESCRIPTOR.name
#         msg_type_id = message_type_to_id[msg_type]
#         msg_str = message.SerializeToString()
#         cls._writer.writeInt32(msg_type_id)
#         cls._writer.writeFixedLengthString(msg_str, len(msg_str))
#         return cls._writer.getString()
# 
#     @classmethod
#     def decode(cls, str_):
#         cls._reader.setString(str_)
#         msg_type_id = cls._reader.readInt32()
#         msg_type = message_id_to_type[msg_type_id]
#         message = cls.create(msg_type)
#         msg_str = cls._reader.readFixedLengthString(len(str_)-4)
#         message.ParseFromString(msg_str)
#         return message
# 
#     @classmethod
#     def encrypt(cls, str_):
#         pass
# 
#     @classmethod
#     def decrypt(cls, str_):
#         pass


import os
import random
import socket
import unittest
from twisted.internet import reactor
from umit.icm.agent.rpc.message import *
from umit.icm.agent.rpc.MessageFactory import MessageFactory
        from umit.icm.agent.rpc.AgentService import AgentFactory

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

    def test_desktop_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 = 3
        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 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'

# Path: umit/icm/agent/rpc/MessageFactory.py
# class MessageFactory(object):
#     """"""
#     _reader = BinaryReader()
#     _writer = BinaryWriter()
# 
#     #----------------------------------------------------------------------
#     #def __init__(self):
#         #"""Constructor"""
# 
#     @classmethod
#     def create(cls, msg_type):
#         return message_creator[msg_type]()
# 
#     @classmethod
#     def encode(cls, message):
#         cls._writer.reset()
#         msg_type = message.DESCRIPTOR.name
#         msg_type_id = message_type_to_id[msg_type]
#         msg_str = message.SerializeToString()
#         cls._writer.writeInt32(msg_type_id)
#         cls._writer.writeFixedLengthString(msg_str, len(msg_str))
#         return cls._writer.getString()
# 
#     @classmethod
#     def decode(cls, str_):
#         cls._reader.setString(str_)
#         msg_type_id = cls._reader.readInt32()
#         msg_type = message_id_to_type[msg_type_id]
#         message = cls.create(msg_type)
#         msg_str = cls._reader.readFixedLengthString(len(str_)-4)
#         message.ParseFromString(msg_str)
#         return message
# 
#     @classmethod
#     def encrypt(cls, str_):
#         pass
# 
#     @classmethod
#     def decrypt(cls, str_):
#         pass


# Path: umit/icm/agent/unittest/TestCases.py
import os
import random
import socket
import unittest
from twisted.internet import reactor
from umit.icm.agent.rpc.message import *
from umit.icm.agent.rpc.MessageFactory import MessageFactory
        from umit.icm.agent.rpc.AgentService import AgentFactory

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

    def test_desktop_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 = 3
        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 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)
