aboutsummaryrefslogtreecommitdiffstats
path: root/src/py/backend_server.py
blob: 6081a6a986694a2a6dfb1c18779b338e0d4cd438 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env python

import socket
import json
import datetime

import tornado.iostream
import tornado.ioloop

import netio
import imc.nonblock
from imc.proxy import Proxy,Connection,imc_call,imc_register_call

class BackendWorker():
    def __init__(self,center_addr):
        self.ioloop = tornado.ioloop.IOLoop.current()
        self.center_addr = center_addr

        self.linkclass = 'backend'
        self.linkid = None

    def start(self):
        self._conn_center()

    def _conn_center(self):
        def __retry():
            print('retry connect center')
            self.ioloop.add_timeout(datetime.timedelta(seconds = 5),self._conn_center)

        def __send_worker_info():
            def ___recv_info_cb(data):
                info = json.loads(data.decode('utf-8'))

                self.linkid = info['linkid']
                Proxy(self.linkid)

                self.center_conn = netio.SocketConnection(info['center_linkid'],stream)
                self.center_conn.add_close_callback(lambda conn : __retry())
                Proxy.instance.add_conn(self.center_conn)

                print('/backend/' + self.linkid)


                imc_register_call('','test_dst',self._test_dst)
                self._test_call(None)

            netio.send_pack(stream,bytes(json.dumps({
                'linkclass':self.linkclass,
                'ws_addr':('210.70.137.215',81)
            }),'utf-8'))
            netio.recv_pack(stream,___recv_info_cb)

        stream = tornado.iostream.IOStream(socket.socket(socket.AF_INET,socket.SOCK_STREAM,0))
        stream.set_close_callback(__retry)
        stream.connect(self.center_addr,lambda : __send_worker_info())

    @imc.nonblock.func
    def _test_call(self,param):
        ret = (yield imc_call(None,'/backend/' + self.center_conn.linkid,'test_dst','Hello'))
        print(ret)

    @imc.nonblock.func
    def _test_dst(self,param):
        return 'Hello Too'
       
if __name__ == '__main__':
    backend_worker = BackendWorker(('localhost',5730))
    backend_worker.start()

    tornado.ioloop.IOLoop.instance().start()