예제 #1
0
def main():
    core = AsyncCore()
    client_hid = core.new_connect('127.0.0.1', 8888, HEADER_WORDLSB)
    if client_hid < 0:
        print 'can not connect to localhost:8888'
        return -1

    while True:
        core.wait(0.1)
        while True:
            event, hid, tag, data = core.read()
            if event == None:
                break

            if event == ASYNC_EVT_NEW:
                print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'new hid=%xh' % hid
            elif event == ASYNC_EVT_LEAVE:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh' % hid
            elif event == ASYNC_EVT_ESTAB:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'estab hid=%xh' % hid
                core.send(hid, 'START')
            elif event == ASYNC_EVT_DATA:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid, 'data', data
예제 #2
0
def main():
    core = AsyncCore()
    hid_listen = core.new_listen('127.0.0.1', 8888, HEADER_LINESPLIT)
    if hid_listen < 0:
        print 'can not listen on port 8888'
        return -1

    print 'listen on localhost:8888 hid=%xh' % hid_listen

    clients = set()
    while True:
        core.wait(0.1)
        while True:
            event, hid, tag, data = core.read()
            if event == None:
                break

            if event == ASYNC_EVT_NEW:
                print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'new hid=%xh' % hid
                if core.get_mode(hid) == ASYNC_MODE_IN:
                    clients.add(hid)
                    _, port, ip = core.parse_remote(data)
                    print time.strftime(
                        '[%Y-%m-%d %H:%M:%S]'
                    ), 'accept hid=%xh' % hid, 'from %s:%d' % (ip, port)
            elif event == ASYNC_EVT_LEAVE:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh' % hid
                clients.remove(hid)
            elif event == ASYNC_EVT_DATA:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid, 'data', data
                core.send(hid, data)

    return 0
예제 #3
0
def main():
    core = AsyncCore()
    client_hid = core.new_connect('127.0.0.1', 8888, HEADER_WORDLSB)
    if client_hid < 0:
        print 'can not connect to localhost:8888'
        return -1

    begin_send = False
    index = 1
    next_time = time.time() + 1
    while True:
        core.wait(0.1)
        while True:
            event, hid, tag, data = core.read()
            if event == None:
                break

            if event == ASYNC_EVT_NEW:
                print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'new hid=%xh' % hid
            elif event == ASYNC_EVT_LEAVE:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh' % hid
            elif event == ASYNC_EVT_ESTAB:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'estab hid=%xh' % hid
                begin_send = True
            elif event == ASYNC_EVT_DATA:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid, 'data', data

        t = time.time()
        if t >= next_time:
            next_time = t + 1
            if begin_send:
                index += 1
                if index % 2 == 0:
                    data = ('hello', 'sayHello', None)
                    data = msgpack.packb(data)
                    core.send(client_hid, data)
                else:
                    data = ('counter', 'incCount', (5, ))
                    data = msgpack.packb(data)
                    core.send(client_hid, data)
예제 #4
0
def main():
    core = AsyncCore()
    hid_listen = core.new_listen('127.0.0.1', 8888, HEADER_WORDLSB)
    if hid_listen < 0:
        print 'can not listen on port 8888'
        return -1

    print 'listen on localhost:8888 hid=%xh' % hid_listen

    index = 0
    clients = set()
    timeslap = time.time()
    while True:
        core.wait(0.1)
        while True:
            event, hid, tag, data = core.read()
            if event == None:
                break

            if event == ASYNC_EVT_NEW:
                print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'new hid=%xh' % hid
                if core.get_mode(hid) == ASYNC_MODE_IN:
                    clients.add(hid)
                    print time.strftime(
                        '[%Y-%m-%d %H:%M:%S]'), 'accept hid=%xh' % hid
            elif event == ASYNC_EVT_LEAVE:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh' % hid
                clients.remove(hid)
            elif event == ASYNC_EVT_DATA:
                print time.strftime(
                    '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid, 'data', data

        if clients:
            current = time.time()
            if current > timeslap:
                timeslap = current + 5
                for hid in clients:
                    core.send(hid, 'ECHO\x00%d' % index)
                    index += 1

    return 0
예제 #5
0
	def __init__(self, ip, port):
		self.core = AsyncCore()
		self.listenHid = self.core.new_listen(ip, port, HEADER_WORDLSB)

		print 'listen on localhost:8888 hid=%xh' % self.listenHid
예제 #6
0
 def __init__(self):
     self.core = AsyncCore()
     self.listen_hids = []
     self.client_hids = []
예제 #7
0
 def __init__(self):
     self.core = AsyncCore()
     self.client_hid = None