Exemple #1
0
    def __init__(self, name, rm, ctrl):
        self.name = name
        self.ctx = zmq.Context()

        self.receiver = self.ctx.socket(zmq.SUB)
        self.receiver.setsockopt(zmq.SUBSCRIBE, '')
        self.receiver.connect(rm)

        self.commander = self.ctx.socket(zmq.REQ)
        self.commander.connect(ctrl)

        self.commander.send("%s:LOGIN-new user" % self.name)
        self.commander.recv()

        self.channel = "ipc://test-%s" % name
        self.checker = self.ctx.socket(zmq.REP)
        self.checker.bind(self.channel)

        self.cliThread = CLIThread(
            self.ctx, self.channel,
            'Input message in format: <toUser>:<msg> or quit #')
        self.cliThread.start()

        self.poller = zmq.Poller()
        self.poller.register(self.receiver, zmq.POLLIN)
        self.poller.register(self.checker, zmq.POLLIN)
Exemple #2
0
    def __init__(self, rm, ctrl):
        self.ctx = zmq.Context()
        self.pub = self.ctx.socket(zmq.PUB)
        self.pub.bind(rm)

        self.ctrl = self.ctx.socket(zmq.REP)
        self.ctrl.bind(ctrl)

        ipc = "ipc://server-internal"
        self.cliSock = self.ctx.socket(zmq.REP)
        self.cliSock.bind(ipc)
        self.cliThread = CLIThread(self.ctx, ipc,
                                   'Input q to quit, ? for help')
        self.cliThread.start()

        self.poller = zmq.Poller()
        self.poller.register(self.cliSock, zmq.POLLIN)
        self.poller.register(self.ctrl, zmq.POLLIN)
Exemple #3
0
    def __init__(self, rm, ctrl):
        self.ctx = zmq.Context()
        self.pub = self.ctx.socket(zmq.PUB)
        self.pub.bind(rm)

        self.ctrl = self.ctx.socket(zmq.REP)
        self.ctrl.bind(ctrl)

        ipc = "ipc://server-internal"
        self.cliSock = self.ctx.socket(zmq.REP)
        self.cliSock.bind(ipc)
        self.cliThread = CLIThread(self.ctx, ipc, 'Input q to quit, ? for help')
        self.cliThread.start()

        self.poller = zmq.Poller()
        self.poller.register(self.cliSock, zmq.POLLIN)
        self.poller.register(self.ctrl, zmq.POLLIN)
Exemple #4
0
class Server(object):
    def __init__(self, rm, ctrl):
        self.ctx = zmq.Context()
        self.pub = self.ctx.socket(zmq.PUB)
        self.pub.bind(rm)

        self.ctrl = self.ctx.socket(zmq.REP)
        self.ctrl.bind(ctrl)

        ipc = "ipc://server-internal"
        self.cliSock = self.ctx.socket(zmq.REP)
        self.cliSock.bind(ipc)
        self.cliThread = CLIThread(self.ctx, ipc,
                                   'Input q to quit, ? for help')
        self.cliThread.start()

        self.poller = zmq.Poller()
        self.poller.register(self.cliSock, zmq.POLLIN)
        self.poller.register(self.ctrl, zmq.POLLIN)

    def run(self):
        '''run the service now'''
        cont = True
        while cont:
            ret = self.poller.poll(200)
            for event in ret:
                sock, _ = event
                if sock == self.ctrl:
                    #Receive messages from ctrl
                    msg = self.ctrl.recv()
                    print "received msg: %s" % msg
                    self.ctrl.send("okay")
                    src, cont = msg.split(':')
                    target, _ = cont.split('-')
                    if target == 'LOGIN':
                        self.login(src)
                    else:
                        if target == 'LOGOUT':
                            self.logout(src)
                        else:
                            self.pub.send(msg)
                else:
                    assert sock == self.cliSock
                    msg = sock.recv().strip()
                    if len(msg) > 0 and msg[0] == 'q':
                        cont = False
                        sock.send('quit')
                        break
                    else:
                        sock.send('fine')
        self.quit()

    def login(self, usr):
        '''login process'''
        self.pub.send("%s:ALL-User %s joined this room!" % (usr, usr))

    def logout(self, usr):
        '''logout '''
        self.pub.send("%s:ALL-User %s leaved this room!" % (usr, usr))

    def quit(self):
        self.cliThread.join()
Exemple #5
0
class Server(object):
    def __init__(self, rm, ctrl):
        self.ctx = zmq.Context()
        self.pub = self.ctx.socket(zmq.PUB)
        self.pub.bind(rm)

        self.ctrl = self.ctx.socket(zmq.REP)
        self.ctrl.bind(ctrl)

        ipc = "ipc://server-internal"
        self.cliSock = self.ctx.socket(zmq.REP)
        self.cliSock.bind(ipc)
        self.cliThread = CLIThread(self.ctx, ipc, 'Input q to quit, ? for help')
        self.cliThread.start()

        self.poller = zmq.Poller()
        self.poller.register(self.cliSock, zmq.POLLIN)
        self.poller.register(self.ctrl, zmq.POLLIN)

        

    def run(self):
        '''run the service now'''
        cont = True
        while cont:
            ret = self.poller.poll(200)
            for event in ret:
                sock, _ = event
                if sock == self.ctrl:
                    #Receive messages from ctrl 
                    msg = self.ctrl.recv()
                    print "received msg: %s" %msg
                    self.ctrl.send("okay")
                    src, cont = msg.split(':')
                    target, _ = cont.split('-')
                    if target == 'LOGIN':
                        self.login(src)
                    else:
                        if target == 'LOGOUT':
                            self.logout(src)
                        else:
                            self.pub.send(msg)
                else:
                    assert sock == self.cliSock
                    msg = sock.recv().strip()
                    if len(msg) > 0 and msg[0] == 'q':
                        cont = False
                        sock.send('quit')
                        break
                    else:
                        sock.send('fine')
        self.quit()
    
    def login(self, usr):
        '''login process'''
        self.pub.send("%s:ALL-User %s joined this room!"%(usr, usr))

    def logout(self, usr):
        '''logout '''
        self.pub.send("%s:ALL-User %s leaved this room!"%(usr, usr))

    def quit(self):
        self.cliThread.join()
Exemple #6
0
class Client(object):
    def __init__(self, name, rm, ctrl):
        self.name = name
        self.ctx = zmq.Context()

        self.receiver = self.ctx.socket(zmq.SUB)
        self.receiver.setsockopt(zmq.SUBSCRIBE, '')
        self.receiver.connect(rm)

        self.commander = self.ctx.socket(zmq.REQ)
        self.commander.connect(ctrl)

        self.commander.send("%s:LOGIN-new user" % self.name)
        self.commander.recv()

        self.channel = "ipc://test-%s" % name
        self.checker = self.ctx.socket(zmq.REP)
        self.checker.bind(self.channel)

        self.cliThread = CLIThread(
            self.ctx, self.channel,
            'Input message in format: <toUser>:<msg> or quit #')
        self.cliThread.start()

        self.poller = zmq.Poller()
        self.poller.register(self.receiver, zmq.POLLIN)
        self.poller.register(self.checker, zmq.POLLIN)

    def run(self):
        '''The run'''
        cont = True
        while cont:
            ret = self.poller.poll(500)
            if len(ret) > 0:
                for event in ret:
                    sock, type = event
                    if sock == self.receiver:
                        self.filter(self.receiver.recv())
                    else:
                        if sock == self.checker:
                            msg = sock.recv()
                            cont = self.input(msg)
                        else:
                            continue
        self.quit()

    def filter(self, msg):
        ''' check incoming message '''
        sys.stdout.flush()
        src, cont = msg.split(':')
        target, body = cont.split('-')
        if src == self.name:
            return

        if (target == 'ALL'):
            print ">>> [broadcast][%s]:%s" % (src, body)
        else:
            if (target == self.name):
                print ">>> [private][%s]:%s" % (src, body)

    def input(self, input):
        '''check for user input, return True for continue'''
        if len(input.split(':')) == 2:
            target, body = input.split(':')
            self.commander.send("%s:%s-%s" % (self.name, target, body))
            self.commander.recv()
        else:
            input = input.strip()
            if len(input) > 0 and input[0] == 'q':
                self.checker.send('quit')
                #self.quit()
                return False
            else:
                print "~~~ input: %s ignored!" % input
        self.checker.send('ok')
        return True

    def quit(self):
        ''' quit processing'''
        self.commander.send("%s:LOGOUT-new" % self.name)
        self.commander.recv()
        self.cliThread.join()