# 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 socket
import struct
import sys
import time
from umit.icm.agent.rpc.message import *
from umit.icm.agent.rpc.MessageFactory import MessageFactory


sys.path.insert(0, ROOT_DIR)


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'

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)

# 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: tools/Diagnoser.py
import os
import socket
import struct
import sys
import time
from umit.icm.agent.rpc.message import *
from umit.icm.agent.rpc.MessageFactory import MessageFactory


sys.path.insert(0, ROOT_DIR)


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'

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)
