def _conn_center(self):
        def __retry(conn):
            print('retry connect center')

            self.center_conn = None
            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'))
                pubkey = open('pubkey.pem','r').read()
                TOJAuth(pubkey)

                self._idendesc = info['idendesc']
                self._link = info['worker_link']
                Proxy(self._link,TOJAuth.instance,self._idendesc,self._conn_link)

                self.center_conn = SocketConnection(info['center_link'],stream,
                                                    self.center_addr,
                                                    self.pend_filestream,
                                                    self.del_pend_filestream)
                self.center_conn.add_close_callback(__retry)
                Proxy.instance.add_conn(self.center_conn)

                #self._init_blobclient()

                #Proxy.instance.register_call('test/','get_client_list',self._test_get_client_list)
                Proxy.instance.register_call('test/','test_dst',self._test_dst)
                #Proxy.instance.register_filter('test/',self._test_filter)

                try:
                    mod.load('Notice','notice',self._idendesc,self._get_link)
                    mod.load('UserMg','user',self._idendesc,self._get_link)
                    mod.load('SquareMg','square',self._idendesc,self._get_link)
                    mod.load('ProblemMg','problem',self._idendesc,self._get_link)
                    mod.load('Mail','mail',self._idendesc,self._get_link)

                except Exception as e:
                    print(e)

                if self._link == '/backend/2/':
                    self._test_call(None)

            sock_ip,sock_port = self.sock_addr
            netio.send_pack(stream,bytes(json.dumps({
                'conntype':'main',
                'linkclass':'backend',
                'sock_ip':sock_ip,
                'sock_port':sock_port,
                'ws_ip':'210.70.137.215',
                'ws_port':self.ws_port
            }),'utf-8'))
            netio.recv_pack(stream,___recv_info_cb)

        stream = SocketStream(socket.socket(socket.AF_INET,socket.SOCK_STREAM,0))
        stream.set_close_callback(__retry)
        stream.connect(self.center_addr,__send_worker_info)
Esempio n. 2
0
    def _conn_link(self, link):
        def __handle_pend(conn):
            try:
                retids = self._pend_mainconn_linkmap.pop(worker_link)

            except KeyError:
                return

            for retid in retids:
                imc. async .ret(retid, conn)

        def __conn_cb():
            conn = Proxy.instance.get_conn(worker_link)
            if conn != None:
                __handle_pend(conn)
                main_stream.set_close_callback(None)
                main_stream.close()

            else:
                sock_ip, sock_port = self.sock_addr
                netio.send_pack(
                    main_stream,
                    bytes(
                        json.dumps({
                            'conntype': 'main',
                            'link': self._link,
                            'sock_ip': sock_ip,
                            'sock_port': sock_port
                        }), 'utf-8'))
                netio.recv_pack(main_stream, __recv_cb)

        def __recv_cb(data):
            stat = json.loads(data.decode('utf-8'))
            if stat == True:
                conn = SocketConnection(worker_link, main_stream, sock_addr,
                                        self.pend_filestream,
                                        self.del_pend_filestream)
                Proxy.instance.add_conn(conn)
                __handle_pend(conn)

            else:
                main_stream.set_close_callback(None)
                main_stream.close()

        if self.center_conn == None:
            return None

        with TOJAuth.change_current_iden(self._idendesc):
            stat, ret = Proxy.instance.call(self.center_conn.link + 'core/',
                                            'lookup_link', 65536, link)

        if stat == False or ret == None:
            return None

        else:
            worker_link = ret['worker_link']

            conn = Proxy.instance.get_conn(worker_link)
            if conn != None:
                return conn

            elif worker_link in self._pend_mainconn_linkmap:
                self._pend_mainconn_linkmap[worker_link].append(
                    imc. async .get_retid())
                return imc. async .switch_top()

            else:
                self._pend_mainconn_linkmap[worker_link] = [
                    imc. async .get_retid()
                ]

                sock_addr = (ret['sock_ip'], ret['sock_port'])

                main_stream = SocketStream(
                    socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0))
                main_stream.set_close_callback(
                    lambda conn: __handle_pend(None))
                main_stream.connect(sock_addr, __conn_cb)

                return imc. async .switch_top()
Esempio n. 3
0
    def _conn_center(self):
        def __retry(conn):
            print('retry connect center')

            self.center_conn = None
            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'))
                pubkey = open('pubkey.pem', 'r').read()
                TOJAuth(pubkey)

                self._idendesc = info['idendesc']
                self._link = info['worker_link']
                Proxy(self._link, TOJAuth.instance, self._idendesc,
                      self._conn_link)

                self.center_conn = SocketConnection(info['center_link'],
                                                    stream, self.center_addr,
                                                    self.pend_filestream,
                                                    self.del_pend_filestream)
                self.center_conn.add_close_callback(__retry)
                Proxy.instance.add_conn(self.center_conn)

                #self._init_blobclient()

                #Proxy.instance.register_call('test/','get_client_list',self._test_get_client_list)
                Proxy.instance.register_call('test/', 'test_dst',
                                             self._test_dst)
                #Proxy.instance.register_filter('test/',self._test_filter)

                try:
                    mod.load('Notice', 'notice', self._idendesc,
                             self._get_link)
                    mod.load('UserMg', 'user', self._idendesc, self._get_link)
                    mod.load('SquareMg', 'square', self._idendesc,
                             self._get_link)
                    mod.load('ProblemMg', 'problem', self._idendesc,
                             self._get_link)
                    mod.load('Mail', 'mail', self._idendesc, self._get_link)

                except Exception as e:
                    print(e)

                if self._link == '/backend/2/':
                    self._test_call(None)

            sock_ip, sock_port = self.sock_addr
            netio.send_pack(
                stream,
                bytes(
                    json.dumps({
                        'conntype': 'main',
                        'linkclass': 'backend',
                        'sock_ip': sock_ip,
                        'sock_port': sock_port,
                        'ws_ip': '210.70.137.215',
                        'ws_port': self.ws_port
                    }), 'utf-8'))
            netio.recv_pack(stream, ___recv_info_cb)

        stream = SocketStream(
            socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0))
        stream.set_close_callback(__retry)
        stream.connect(self.center_addr, __send_worker_info)
    def _conn_link(self,link):
        def __handle_pend(conn):
            try:
                retids = self._pend_mainconn_linkmap.pop(worker_link)
            
            except KeyError:
                return

            for retid in retids:
                imc.async.ret(retid,conn)

        def __conn_cb():
            conn = Proxy.instance.get_conn(worker_link)
            if conn != None:
                __handle_pend(conn)
                main_stream.set_close_callback(None)
                main_stream.close()
            
            else:
                sock_ip,sock_port = self.sock_addr
                netio.send_pack(main_stream,bytes(json.dumps({
                    'conntype':'main',
                    'link':self._link,
                    'sock_ip':sock_ip,
                    'sock_port':sock_port
                }),'utf-8'))
                netio.recv_pack(main_stream,__recv_cb)

        def __recv_cb(data):
            stat = json.loads(data.decode('utf-8'))
            if stat == True:
                conn = SocketConnection(worker_link,main_stream,sock_addr,
                                        self.pend_filestream,
                                        self.del_pend_filestream)
                Proxy.instance.add_conn(conn)
                __handle_pend(conn)

            else:
                main_stream.set_close_callback(None)
                main_stream.close()
        
        if self.center_conn == None:
            return None

        with TOJAuth.change_current_iden(self._idendesc):
            stat,ret = Proxy.instance.call(self.center_conn.link + 'core/','lookup_link',65536,link)

        if stat == False or ret == None:
            return None

        else:
            worker_link = ret['worker_link']

            conn = Proxy.instance.get_conn(worker_link)
            if conn != None:
                return conn

            elif worker_link in self._pend_mainconn_linkmap:
                self._pend_mainconn_linkmap[worker_link].append(imc.async.get_retid())
                return imc.async.switch_top()

            else:
                self._pend_mainconn_linkmap[worker_link] = [imc.async.get_retid()]

                sock_addr = (ret['sock_ip'],ret['sock_port'])

                main_stream = SocketStream(socket.socket(socket.AF_INET,socket.SOCK_STREAM,0))
                main_stream.set_close_callback(lambda conn : __handle_pend(None))
                main_stream.connect(sock_addr,__conn_cb)

                return imc.async.switch_top()