# Path: elevator_cli/io.py
# def output_result(status, result, *args, **kwargs):
#     if result:
#         if status == FAILURE_STATUS:
#             puts(colored.red(str(result)))
#         else:
#             puts(str(result))
# 
# Path: elevator_cli/helpers.py
# def success(datas):
#     return SUCCESS_STATUS, datas
# 
# def fail(type, msg):
#     return FAILURE_STATUS, "Error : " + ', '.join([type.upper(), msg])
# 
# Path: elevator_cli/message.py
# class Request(object):
#     """Handler objects for frontend->backend objects messages"""
#     def __new__(cls, *args, **kwargs):
#         try:
#             content = {
#                 'meta': kwargs.pop('meta', {}),
#                 'uid': kwargs.get('db_uid'),  # uid can eventually be None
#                 'cmd': kwargs.pop('command'),
#                 'args': kwargs.pop('args'),
#             }
#         except KeyError:
#             raise MessageFormatError("Invalid request format : %s" % str(kwargs))
# 
#         return msgpack.packb(content)
# 
# class ResponseHeader(object):
#     def __init__(self, raw_header):
#         header = msgpack.unpackb(raw_header)
# 
#         try:
#             self.status = header.pop('status')
#             self.err_code = header.pop('err_code')
#             self.err_msg = header.pop('err_msg')
#         except KeyError:
#             errors_logger.exception("Invalid response header : %s" %
#                                     header)
#             raise MessageFormatError("Invalid response header")
# 
#         for key, value in header.iteritems():
#             setattr(self, key, value)
# 
# class Response(object):
#     def __init__(self, raw_message, *args, **kwargs):
#         message = msgpack.unpackb(raw_message)
# 
#         try:
#             self.datas = message['datas']
#         except KeyError:
#             errors_logger.exception("Invalid response message : %s" %
#                                     message)
#             raise MessageFormatError("Invalid response message")


import zmq
from elevator.constants import *
from .io import output_result
from .errors import *
from .helpers import success, fail
from .message import Request, ResponseHeader, Response
    def __init__(self, *args, **kwargs):
        self.protocol = kwargs.pop('protocol', 'tcp')
        self.endpoint = kwargs.pop('endpoint', '127.0.0.1:4141')
        self.host = "%s://%s" % (self.protocol, self.endpoint)

        self.context = None
        self.socket = None
        self.timeout = kwargs.pop('timeout', 10000)

        self.db_uid = None
        self.db_name = None

        self.setup_socket()

        if self.ping():
            self.connect()
        else:
            failure_msg = 'No elevator server hanging on {0}://{1}'.format(self.protocol, self.endpoint)
            output_result(FAILURE_STATUS, failure_msg)

    def __del__(self):
        self.socket.close()
        self.context.term()

    def ping(self, *args, **kwargs):
        pings = True
        timeout = kwargs.pop('timeout', 1000)
        orig_timeout = self.timeout
        self.socket.setsockopt(zmq.RCVTIMEO, timeout)


# Path: elevator_cli/io.py
# def output_result(status, result, *args, **kwargs):
#     if result:
#         if status == FAILURE_STATUS:
#             puts(colored.red(str(result)))
#         else:
#             puts(str(result))
# 
# Path: elevator_cli/helpers.py
# def success(datas):
#     return SUCCESS_STATUS, datas
# 
# def fail(type, msg):
#     return FAILURE_STATUS, "Error : " + ', '.join([type.upper(), msg])
# 
# Path: elevator_cli/message.py
# class Request(object):
#     """Handler objects for frontend->backend objects messages"""
#     def __new__(cls, *args, **kwargs):
#         try:
#             content = {
#                 'meta': kwargs.pop('meta', {}),
#                 'uid': kwargs.get('db_uid'),  # uid can eventually be None
#                 'cmd': kwargs.pop('command'),
#                 'args': kwargs.pop('args'),
#             }
#         except KeyError:
#             raise MessageFormatError("Invalid request format : %s" % str(kwargs))
# 
#         return msgpack.packb(content)
# 
# class ResponseHeader(object):
#     def __init__(self, raw_header):
#         header = msgpack.unpackb(raw_header)
# 
#         try:
#             self.status = header.pop('status')
#             self.err_code = header.pop('err_code')
#             self.err_msg = header.pop('err_msg')
#         except KeyError:
#             errors_logger.exception("Invalid response header : %s" %
#                                     header)
#             raise MessageFormatError("Invalid response header")
# 
#         for key, value in header.iteritems():
#             setattr(self, key, value)
# 
# class Response(object):
#     def __init__(self, raw_message, *args, **kwargs):
#         message = msgpack.unpackb(raw_message)
# 
#         try:
#             self.datas = message['datas']
#         except KeyError:
#             errors_logger.exception("Invalid response message : %s" %
#                                     message)
#             raise MessageFormatError("Invalid response message")


# Path: elevator_cli/client.py
import zmq
from elevator.constants import *
from .io import output_result
from .errors import *
from .helpers import success, fail
from .message import Request, ResponseHeader, Response

    def __init__(self, *args, **kwargs):
        self.protocol = kwargs.pop('protocol', 'tcp')
        self.endpoint = kwargs.pop('endpoint', '127.0.0.1:4141')
        self.host = "%s://%s" % (self.protocol, self.endpoint)

        self.context = None
        self.socket = None
        self.timeout = kwargs.pop('timeout', 10000)

        self.db_uid = None
        self.db_name = None

        self.setup_socket()

        if self.ping():
            self.connect()
        else:
            failure_msg = 'No elevator server hanging on {0}://{1}'.format(self.protocol, self.endpoint)
            output_result(FAILURE_STATUS, failure_msg)

    def __del__(self):
        self.socket.close()
        self.context.term()

    def ping(self, *args, **kwargs):
        pings = True
        timeout = kwargs.pop('timeout', 1000)
        orig_timeout = self.timeout
        self.socket.setsockopt(zmq.RCVTIMEO, timeout)


        request = Request(db_uid=None, command="PING", args=[])