Ejemplo n.º 1
0
    def respid(self, id):
        """ response id setter method """
        if(id is None):
            id = str(getuid())

        if(self.__respid != id):
            self.msg = None
            self.msgparts = None
            self.__respid = id
Ejemplo n.º 2
0
    def __reset(self,
                msg=None,
                msgparts=None,
                lineseparator='\r\n',
                hdrs_dict=None,
                body=None,
                reqline=None,
                reqid=None):
        """ internal function to reset the object to its initial state"""
        if(reqid is None):
            reqid = str(getuid())

        if(reqline is None):
            reqline = get_request_line()

        return reqid, reqline
Ejemplo n.º 3
0
    def __reset(self,
                msg=None,
                msgparts=None,
                lineseparator=None,
                respcode=None,
                reason=None,
                hdrs_dict=None,
                body=None,
                respline=None,
                respid=None):
        """ internal function to reset the object to its initial state"""
        if(respid is None):
            respid = str(getuid())

        if(respline is None):
            respline = get_response_line()

        return respid, respline
Ejemplo n.º 4
0

if __name__ == '__main__':
    from com.goffersoft.utils.uid import getuid
    from com.goffersoft.msg.msgfac import get_req_msg_validation_handler
    from com.goffersoft.msg.msgfac import print_registry
    import logging

    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger()
    init()
    print_registry(logger)
    print_registry()

    mreq = MsgReqRaw(body='Hello')
    mreq.fromhdr = Address(addrparts=(('a', getuid())))

    msgstr = str(mreq)

    msg_handler = get_req_msg_validation_handler(msgstr)

    msg, reason = msg_handler(msgstr, MsgType.REQ)

    if(reason is not None):
        print(reason)

    print(msg.debug())

    msgstr1 = str(msg)

    print('1-->%r' % msgstr)
Ejemplo n.º 5
0

if __name__ == '__main__':
    from com.goffersoft.utils.uid import getuid
    from com.goffersoft.msg.msgfac import get_req_msg_validation_handler
    from com.goffersoft.msg.msgfac import print_registry
    import logging

    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger()
    init()
    print_registry(logger)
    print_registry()

    mreq = MsgReqRaw(body='Hello')
    mreq.fromhdr = Address(addrparts=(('a', getuid())))

    msgstr = str(mreq)

    msg_handler = get_req_msg_validation_handler(msgstr)

    msg, reason = msg_handler(msgstr, MsgType.REQ)

    if (reason is not None):
        print(reason)

    print(msg.debug())

    msgstr1 = str(msg)

    print('1-->%r' % msgstr)
def service_loop(shutdown_event,
                 ctxt=None,
                 name=None,
                 id=None,
                 discovery_addr='localhost:5555',
                 mgmt_addr='localhost:6666',
                 inproc_addr_list=None,
                 inproc_shutdown_event_list=None,
                 update_interval=10,
                 poll_timeout=5000):
    func_name = service_loop.__name__
    try:
        logger = logging.getLogger()

        if(ctxt is None):
            ctxt = zmq.Context()

        if(id is None):
            id = getuid()

        if(name is None):
            name = 'Service ' + str(random.randint(1, 1000000))

        service_addr = Address(addrparts=(name, str(id)))
        filter = u''

        th = threading.current_thread()
        th.setName(name)
        logger.info('%s : %s : %s : %s : Starting Service Loop...'
                    '(discover_addr=%s, mgmt_addr=%s, '
                    'update_interval=%d, poll_timeout=%d)',
                    func_name,
                    th.getName(),
                    th.ident, id,
                    discovery_addr,
                    mgmt_addr,
                    update_interval,
                    poll_timeout)

        discovery_socket = ctxt.socket(zmq.SUB)
        discovery_socket.connect('tcp://' + discovery_addr)
        discovery_socket.setsockopt_string(zmq.SUBSCRIBE, filter)
        service_socket = ctxt.socket(zmq.DEALER)
        service_socket.setsockopt(zmq.IDENTITY, str(id).encode())
        service_socket.connect('tcp://' + mgmt_addr)
        id = getuid()

        poller = zmq.Poller()
        poller.register(service_socket, zmq.POLLIN)
        poller.register(discovery_socket, zmq.POLLIN)

        inproc_socket_list = []
        inproc_req_dict = None
        num_inproc_socks = None
        if(inproc_addr_list is not None):
            num_inproc_socks = len(inproc_addr_list)
            inproc_req_dict = {}
            inproc_socket_list = []
            for addr in inproc_addr_list:
                inproc_socket = ctxt.socket(zmq.PAIR)
                inproc_socket.connect('inproc://' + str(addr))
                inproc_socket_list.append(inproc_socket)
                poller.register(inproc_socket, zmq.POLLIN)

        init = False
        hello = False
        num_polls = 0
        reqid = None
        service_mgr_addr = None

        while shutdown_event.is_set() is False:
            try:
                i = 0
                if(inproc_addr_list is not None and
                   inproc_shutdown_event_list is not None):
                    for event in inproc_shutdown_event_list:
                        if(event.is_set() is True):
                            poller.unregister(inproc_socket_list[i])
                            inproc_socket_list[i] = None
                            num_inproc_socks -= 1
                        i += 1
                    if(num_inproc_socks <= 0):
                        break
                socks = dict(poller.poll(poll_timeout))
            except KeyboardInterrupt:
                break

            if(discovery_socket in socks):
                rcvd_msg = discovery_socket.recv()
                rcvd_msg = rcvd_msg.decode()
                msg, reason = verify_reqmsg(rcvd_msg)
                if(reason is not None):
                    logger.info('%s : discovery socket : Invalid Msg=%r',
                                func_name, reason)
                    logger.info('%s : Msg=%r', func_name, rcvd_msg)
                else:
                    if(service_mgr_addr is None):
                        init = True
                    service_mgr_addr = msg.fromhdr

            if(service_socket in socks):
                rcvd_msg = service_socket.recv()
                rcvd_msg = rcvd_msg.decode()
                msg, reason = verify_respmsg(rcvd_msg)
                if(reason is not None):
                    logger.info('%s : service_socket : Invalid Msg=%r',
                                func_name, reason)
                    logger.info('%s : Msg=%r', func_name, rcvd_msg)
                else:
                    if((msg.fromhdr.addrparts[1] ==
                       service_mgr_addr.addrparts[1]) and
                       (msg.tohdr.addrparts[1] ==
                           service_addr.addrparts[1])):
                        if(reqid == msg.respid):
                            reqid = None
                            init = False
                            hello = False
                        elif(msg.respid in inproc_req_dict):
                            i = inproc_req_dict[msg.respid]
                            del inproc_req_dict[msg.respid]
                            if(inproc_socket_list[i] is not None):
                                msg = MsgRespRaw(respid=msg.respid,
                                                 respcode=msg.respcode,
                                                 reason=msg.reason,
                                                 body=msg.body)
                                msg.fromhdr = service_addr
                                msg.tohdr = inproc_addr_list[i]
                                inproc_socket_list[i].send(msg.tobytes())
                        else:
                            logger.info('%s : service socket : '
                                        'no matching request '
                                        'for this response, Msg=%r',
                                        func_name, rcvd_msg)
                    else:
                        if(msg.fromhdr.addrparts[1] !=
                           (service_mgr_addr.addrparts[1])):
                            service_mgr_addr = None
                        logger.info('%s : service socket : '
                                    'Message Not Authentic, Msg=%s',
                                    func_name, msg.debug())
                        reqid = None
                        init = False
                        hello = False

            i = 0
            for inproc_sock in inproc_socket_list:
                if(inproc_sock in socks):
                    rcvd_msg = inproc_sock.recv()
                    rcvd_msg = rcvd_msg.decode()
                    msg, reason = verify_reqmsg(rcvd_msg)
                    if(reason is not None):
                        logger.info('%s : inproc socket(%s) :  Invalid Msg : %r',
                                    func_name,
                                    inproc_addr_list[i],
                                    reason)
                        logger.info('%s : Msg=%r', func_name, rcvd_msg)
                    elif(service_mgr_addr is None):
                        if(inproc_socket_list[i] is not None):
                            msg = MsgRespRaw(respid=msg.reqid,
                                             respcode=RawRCType.RC_503,
                                             reason=RawRCDescrType.RC_503 +
                                             ' : Service Mgr Not Initialized')
                            msg.fromhdr = service_addr
                            msg.tohdr = inproc_addr_list[i]
                            inproc_socket.send(msg.tobytes())
                    else:
                        msg = MsgReqRaw(body=msg.body)
                        msg.fromhdr = service_addr
                        msg.tohdr = service_mgr_addr
                        inproc_req_dict[msg.reqid] = i
                        service_socket.send(msg.tobytes())
                i += 1

            if(service_mgr_addr is not None):
                num_polls += 1
                if(num_polls % 10 == 0):
                    hello = True

            if(init is True):
                if(reqid is None):
                    msg = MsgReqRaw(body=STMsgType.INIT)
                    msg.fromhdr = service_addr
                    msg.tohdr = service_mgr_addr
                    reqid = msg.reqid
                    service_socket.send(msg.tobytes())
            elif(hello is True):
                if(reqid is None):
                    msg = MsgReqRaw(body=STMsgType.HELLO)
                    msg.fromhdr = service_addr
                    msg.tohdr = service_mgr_addr
                    reqid = msg.reqid
                    service_socket.send(msg.tobytes())

            i = 0

        logger.info('%s : Shutting Down Service Loop...', func_name)
    except:
        logger.exception('%s : Shutting Down Service Loop...', func_name)
def service_mgr_loop(shutdown_event,
                     ctxt=None,
                     name='service mgr',
                     id=None,
                     discovery_addr='*:5555',
                     mgmt_addr='*:6666',
                     discovery_interval=5,
                     poll_timeout=5000):
    func_name = service_mgr_loop.__name__
    try:
        logger = logging.getLogger()
        registry = {}

        if(ctxt is None):
            ctxt = zmq.Context()

        if(id is None):
            id = getuid()

        service_mgr_addr = Address(addrparts=(name, str(id)))

        discovery_socket = ctxt.socket(zmq.PUB)
        mgmt_socket = ctxt.socket(zmq.ROUTER)
        discovery_socket.bind('tcp://' + str(discovery_addr))
        mgmt_socket.bind('tcp://' + str(mgmt_addr))

        th = threading.current_thread()
        th.setName(name)
        logger.info('%s : %s : %s : %s : Starting Service Manager...'
                    '(discover_addr=%s, mgmt_addr=%s'
                    'discover_interval=%d, poll_timeout=%d)',
                    func_name,
                    th.getName(),
                    th.ident, id,
                    discovery_addr,
                    mgmt_addr,
                    discovery_interval,
                    poll_timeout)

        housekeeping_shutdown_event = threading.Event()

        service_mgr_housekeeping(service_mgr_addr,
                                 discovery_socket,
                                 housekeeping_shutdown_event,
                                 discovery_interval,
                                 True)

        poller = zmq.Poller()
        poller.register(mgmt_socket, zmq.POLLIN)

        while shutdown_event.is_set() is False:
            try:
                socks = dict(poller.poll(poll_timeout))

                if(mgmt_socket in socks):
                    msgparts = mgmt_socket.recv_multipart()
                    numparts = len(msgparts)
                    i = 1
                    msg = ''
                    while(i < numparts):
                        msg += msgparts[i].decode()
                        i += 1

                    retval, out_msg = service_mgr_process_msg(service_mgr_addr,
                                                              msgparts[0],
                                                              registry,
                                                              msg)

                    if(retval is False):
                        break

                    if(out_msg is not None):
                        out_msgparts = (msgparts[0], out_msg.tobytes())
                        mgmt_socket.send_multipart(out_msgparts)

                service_mgr_update_registry(registry)
            except KeyboardInterrupt:
                logger.exception('%s : Service Manager Exception...',
                                 func_name)
                break

        logger.info('%s : Shutting Down Service Manager...',
                    func_name)
        housekeeping_shutdown_event.set()
    except:
        logger.exception('%s : Shutting Down Service Manager...',
                         func_name)
        housekeeping_shutdown_event.set()
def cmd_proc_loop(shutdown_event,
                  ctxt=None,
                  name='cmd proc',
                  id=None,
                  discovery_addr='localhost:5555',
                  mgmt_addr='localhost:6666',
                  discovery_interval=5,
                  poll_timeout=5000):
    func_name = cmd_proc_loop.__name__
    try:
        usage = """
           l : List Available Servers
           s <uuid> : Stop a Server
           e : Exit
        """
        logger = logging.getLogger()

        if(ctxt is None):
            ctxt = zmq.Context()

        if(id is None):
            id = getuid()

        cmd_proc_addr = Address(addrparts=(name, str(id)))

        th = threading.current_thread()
        th.setName(name)
        logger.info('%s : %s : %s : %s : Starting Cmd Proc Loop...'
                    '(discover_addr=%s, mgmt_addr=%s'
                    'discover_interval=%d, poll_timeout=%d)',
                    func_name,
                    th.getName(),
                    th.ident, id,
                    discovery_addr,
                    mgmt_addr,
                    discovery_interval,
                    poll_timeout)

        cmd_socket = ctxt.socket(zmq.PAIR)
        cmd_socket.bind('inproc://' + str(cmd_proc_addr))

        inproc_addr_list = (cmd_proc_addr,)
        inproc_event_list = (threading.Event(),)

        service_thread, service_shutdown_event = \
            init_service(ctxt,
                         'cmd proc service',
                         id,
                         discovery_addr,
                         mgmt_addr,
                         inproc_addr_list,
                         inproc_event_list,
                         discovery_interval,
                         poll_timeout,
                         '../../../../conf/logconf_servicecmd.json')

        print_menu = True
        valid_cmds = ('l', 's', 'e')
        while shutdown_event.is_set() is False:
            try:
                if(print_menu is True):
                    print_usage(usage)

                cmd = nonBlockingRawInputUsingSelect()
                if(cmd == ''):
                    print_menu = False
                    continue
                elif(cmd not in valid_cmds):
                    print_menu = True
                    logger.info('%s : <%s> not a valid cmd...try again',
                                func_name, cmd)
                    continue
                elif(cmd == 'e'):
                    inproc_event_list[0].set()
                    break
                else:
                    print_menu = True

                msg = MsgReqRaw(body=STMsgType.MGMT+'\r\n'+cmd)
                msg.fromhdr = cmd_proc_addr
                msg.tohdr = cmd_proc_addr
                cmd_socket.send(msg.tobytes())
                if(cmd == 'l'):
                    rcvd_msg = cmd_socket.recv()
                    rcvd_msg = rcvd_msg.decode()
                    msg, reason =\
                        verify_respmsg(rcvd_msg)
                    if(reason is not None):
                        logger.info('%s : Invalid Msg=%s',
                                    func_name, reason)
                        logger.debug('%s : Msg=%r', func_name, rcvd_msg)
                    elif(msg.tohdr == cmd_proc_addr):
                        print('List of services : ')
                        print('respcode=%s, reason=%s' %
                              (msg.respcode, msg.reason))
                        print('%s' % msg.body)
                    else:
                        logger.info('%s : Cmd Loop : Message Not Authentic,'
                                    'Msg=%s', func_name, msg.debug())
            except KeyboardInterrupt:
                break

        logger.info('%s : Shutting Down Cmd Processor...', func_name)
        service_shutdown_event.set()
        service_thread.join()
    except:
        logger.exception('%s : Shutting Down Cmd Processor...', func_name)
        service_shutdown_event.set()
        service_thread.join()