示例#1
0
文件: conn.py 项目: demonxjj/TCE
    def dispatchMsg(self, m):
        from communicator import RpcCommunicator
        server = RpcCommunicator.instance().currentServer()
        if m.calltype & RpcMessage.CALL:

            #rpc调用请求
            if self.adapter:
                # print 'containedIf:',self.adapter.containedIf(m.ifidx)
                if self.adapter.containedIf(m.ifidx):
                    # print 'adapter.dispatchMsg',m
                    self.adapter.dispatchMsg(m)
                    return
            # redirect to next

            #用户未认证,不能转发到下级节点
            route = server.routes.get(m.ifidx)  #查找接口路由信息
            if route:
                if self.ep.type in ('socket', 'websocket'):  # 内向外的rpc请求
                    #-- 鉴别用户是否已经通过认证  userid=0 非法用户编号
                    server = RpcCommunicator.instance().currentServer()
                    if server.getPropertyValue('userid_check',
                                               'false') == 'true':  #判别舒服需要用户检查
                        if not self.userid:  #用户id为0,未认证连接,取消转发
                            print 'userid checked (is null),unauthorized skip redirect..'
                            return
                if self.userid:
                    m.extra.props['__user_id__'] = str(self.userid)
                else:
                    print 'user_id is null, __user_id__ not apply.'
                #开始查找路由表,定位
                inout = route.getRouteInoutPair(RpcIfRouteDetail.CALL,
                                                self.ep.id)
                if inout:  #路由输出ep
                    peer_ep = inout.out
                    # if peer_ep.type !='mq':
                    # 	log_error('out-ep must be mq-type ,please check config of server: %s '%server.name)
                    # 	return
                    m.call_id |= 1 << 15  #最高位1表示此消息包是转发消息包
                    #print 'redirect CALL msg,call_id',hex(m.call_id)
                    peer_ep.sendMessage(m)
                    #系统开始,用于已经将rpc调用回路设置好了 EasyMqConnection.setLoopbackMq()

        if m.calltype & RpcMessage.RETURN:
            #print 'call_id:',hex(m.call_id),m.extra.props
            #最高位为0表示此RETURN为本地调用产生的RETURN
            #routes表项记录为空,表示非转发服务,直接本地处理
            if not m.call_id >> 15 or not server.routes:
                self.doReturnMsg(m)
                return
            #分派到其他ep
            # type_,id = (m.call_id>>8)&0x7f, m.call_id&0xff
            route = server.routes.get(m.ifidx)
            #print 'RETURN:',route
            if route:
                inout = route.getRouteInoutPair(RpcIfRouteDetail.RETURN,
                                                self.ep.id)
                if inout:  #路由输出ep
                    peer_ep = inout.out
                    print 'redirect RETURN ..'
                    peer_ep.sendMessage(m)
示例#2
0
	def onDataRecved(self,d):
		from communicator import RpcCommunicator
		r = False
		r,err = self.queue.dataQueueIn(d)
		if not r:
			print r,err ,'close socket..'
			self.close() #数据解码错误,直接关闭连接
			return False
		msglist=[]
		msglist = self.queue.getMessageList()
		if len(msglist) == 0:
			return True

		for d in msglist:
			m = RpcMessage.unmarshall(d)
			if not m:
				log_error('decode mq-msg error!')
				continue
			m.conn = self
			# RpcCommunicator.instance().dispatchMsg(m)
			m.user_id = self.userid
			# self.dispatchMsg(m)

			self.recvpkg_num += 1 # 2013.11.25
			listener = RpcCommunicator.instance().getConnectionEventListener()
			if listener:
				r = listener.onDataPacket(self,m)
				if not r: # message item ignored
					print 'eventListener filtered one message..'
					self.close()
					return
			#--- end 2013.11.25 ---
			#print m.extra.props
			# self.dispatchMsg(m)
			RpcCommunicator.instance().dispatchMsg(m)
示例#3
0
    def onDataRecved(self, d):
        r = False
        r, err = self.queue.dataQueueIn(d)
        if not r:
            print r, err, 'close socket..'
            self.close()  #数据解码错误,直接关闭连接
            return False
        msglist = []
        msglist = self.queue.getMessageList()
        if len(msglist) == 0:
            return True

        for d in msglist:
            m = RpcMessage.unmarshall(d)
            if not m:
                log_error('decode mq-msg error!')
                continue

            m.conn = self
            m.user_id = self.userid

            #-- begin 2013.11.25 ---
            #当接收到第一个pkg时,判别__token__是否携带
            self.recvpkg_num += 1  # 2013.11.25
            listener = RpcCommunicator.instance().getConnectionEventListener()
            if listener:
                r = listener.onDataPacket(self, m)
                if not r:  # message item ignored
                    print 'eventListener filtered one message..'
                    self.close()
                    return
            #--- end 2013.11.25 ---
            #print m.extra.props
            # self.dispatchMsg(m)
            RpcCommunicator.instance().dispatchMsg(m)
示例#4
0
文件: conn.py 项目: demonxjj/TCE
    def sendMessage(self, m):
        from communicator import RpcCommunicator
        if m.calltype & RpcMessage.CALL:
            # if not m.extra.props.get('__user_id__'):
            # 	m.extra.props['__user_id__'] = str(m.user_id)

            #2013.11.25 following line commented
            #			m.call_id |= RpcCommunicator.instance().currentServer().getId()

            if m.calltype & RpcMessage.ONEWAY == 0:
                RpcCommunicator.instance().enqueueMsg(m)  #置入等待队列
        r = False
        r = self.sendDetail(m)
        if not r:
            if m.calltype & RpcMessage.CALL:  #发送失败,删除等待队列中的消息
                if m.calltype & RpcMessage.ONEWAY == 0:
                    RpcCommunicator.instance().dequeueMsg(m.sequence)
        return r
示例#5
0
文件: conn.py 项目: demonxjj/TCE
 def doReturnMsg(self, m2):
     from communicator import RpcCommunicator
     #		print 'doReturnMsg',m2,m2.sequence,repr(m2)
     m1 = RpcCommunicator.instance().dequeueMsg(m2.sequence)
     if m1:
         if m1. async:  #异步通知
             m1.asyncparser(m1, m2)  #m2.paramstream,m2.async,m2.prx)
         else:
             #				m1.mtx.notify(m2)       # 传递到等待返回结果的对象线程
             m1.mtx.set(m2)
示例#6
0
    def __thread_recv(self):
        from communicator import RpcCommunicator
        # print 'qpid-mq recv thread start..'
        while not self.exitflag:
            try:
                #				if not self.conn:
                #					print 'try open mq:',self.ep.name
                #					broker = "%s:%s"%(self.ep.host,self.ep.port)
                #					self.conn = Connection( broker,reconnect= True,tcp_nodelay=True)
                #					self.conn.open()
                #					self.ssn = self.conn.session()
                #					self.rcv = self.ssn.receiver(self.ep.addr)
                #					self.rcv.capacity = 4000
                m = self.rcv.fetch()
                # print '.'*10
                d = m.content
                # print 'mq recv:',repr(d)
                print 'recved 1 msg from MQ..'
                self.ssn.acknowledge(sync=True)
                m = RpcMessage.unmarshall(d)
                if not m:
                    log_error('decode mq-msg error!')
                    continue
                m.conn = self

                value = m.extra.props.get(RpcMessageTraits.MAX_MSG_LINGER_TIME,
                                          '0')
                linger_time = int(value)
                if linger_time and time.time() > linger_time:
                    # drop it
                    continue  #过期接收的消息直接丢弃

                # self.dispatchMsg(m)
                RpcCommunicator.instance().dispatchMsg(m)
            except:
                log_error(traceback.format_exc())
                gevent.sleep(1)

        if self.adapter:
            self.adapter.stopmtx.set()
        # log_info("qpid-mq thread exiting..")
        return False
示例#7
0
文件: conn_qpid.py 项目: adoggie/TCE
	def thread_recv(self):
		from communicator import RpcCommunicator
		# print 'qpid-mq recv thread start..'
		while not self.exitflag:
			try:
#				if not self.conn:
#					print 'try open mq:',self.ep.name
#					broker = "%s:%s"%(self.ep.host,self.ep.port)
#					self.conn = Connection( broker,reconnect= True,tcp_nodelay=True)
#					self.conn.open()
#					self.ssn = self.conn.session()
#					self.rcv = self.ssn.receiver(self.ep.addr)
#					self.rcv.capacity = 4000
				m = self.rcv.fetch()
				# print '.'*10
				d = m.content
				# print 'mq recv:',repr(d)
				print 'recved 1 msg from MQ..'
				self.ssn.acknowledge(sync=False)
				m = RpcMessage.unmarshall(d)
				if not m:
					log_error('decode mq-msg error!')
					continue
				m.conn = self

				value = m.extra.props.get(RpcMessageTraits.MAX_MSG_LINGER_TIME,'0')
				linger_time = int(value)
				if linger_time and time.time() > linger_time:
					# drop it
					continue	#过期接收的消息直接丢弃


				# self.dispatchMsg(m)
				RpcCommunicator.instance().dispatchMsg(m)
			except:
				log_error(traceback.format_exc())
				gevent.sleep(1)

		if self.adapter:
			self.adapter.stopmtx.set()
		# log_info("qpid-mq thread exiting..")
		return False
示例#8
0
    def _service(self, sock, address):
        #print ' new client socket come in :',str(address)
        conn = RpcConnectionSocket(self, self.ep, sock)
        self.addConnection(conn)

        server = RpcCommunicator.instance().currentServer()
        if server.getPropertyValue('userid_check', 'false') == 'false':
            conn.setUserId(str(self.generateSeq()))
            # print 'setUserid:',conn.userid
        conn.recv()
        self.removeConnection(conn)
示例#9
0
	def _service(self,sock,address):
		#print ' new client socket come in :',str(address)
		conn = RpcConnectionSocket(self,self.ep,sock)
		self.addConnection(conn)

		server = RpcCommunicator.instance().currentServer()
		if server.getPropertyValue('userid_check','false') == 'false':
			conn.setUserId( str(self.generateSeq()) )
			# print 'setUserid:',conn.userid
		conn.recv()
		self.removeConnection(conn)
示例#10
0
文件: log.py 项目: demonxjj/TCE
def log_print(m, what):
    from communicator import RpcCommunicator
    logger = RpcCommunicator.instance().getLogger()
    if not logger: return
    if what == LOG_DEBUG:
        logger.debug(m)
    elif what == LOG_WARN:
        logger.warning(m)
    elif what == LOG_INFO:
        logger.trace(m)
    elif what == LOG_ERROR:
        logger.error(m)
示例#11
0
文件: log.py 项目: adoggie/TCE
def log_print(m,what):
	from communicator import RpcCommunicator
	logger= RpcCommunicator.instance().getLogger()
	if not logger:  return
	if what == LOG_DEBUG:
		logger.debug(m)
	elif what == LOG_WARN:
		logger.warning(m)
	elif what == LOG_INFO:
		logger.trace(m)
	elif what == LOG_ERROR:
		logger.error(m)
示例#12
0
	def _service(self,environ, start_response):
		from communicator import RpcCommunicator
		print ' new client websocket come in :'#,str(address)
		sock = environ.get("wsgi.websocket")
		if sock is None:
			return self._http_handler(environ, start_response)
		conn = RpcConnectionWebSocket(self,self.ep,sock)
		self.addConnection(conn)

		server = RpcCommunicator.instance().currentServer()
		if server.getPropertyValue('userid_check','false') == 'false':
			conn.setUserId( str(self.generateSeq()) )
		conn.recv()

		self.removeConnection(conn)
示例#13
0
    def sendDetail(self, m):
        try:
            #self.mtx.acquire()
            if not self.sock:  # and  not self.adapter:
                # import urlparse
                dest = (self.ep.host, self.ep.port)
                # scheme='tcp'
                # if isinstance(self.address,str):
                # 	cps = urlparse.urlparse(self.address)
                # 	scheme = cps.scheme.lower()
                # 	host,port = cps.netloc.split(':')
                # 	dest = (host,int(port))

                self.sock = gevent.socket.create_connection(dest, )
                # if scheme =='ssl':
                if self.ep.ssl:
                    import ssl
                    self.sock = gevent.ssl.wrap_socket(self.sock, )

                gevent.spawn(self.recv)
                #send token when connect on
                if self.token:
                    m.extra.setPropertyValue('__token__', self.token)
                # 2014.7.1 scott
                device_id = RpcCommunicator.instance().getProperties().get(
                    'device_id')
                if device_id:
                    m.extra.setPropertyValue('__device_id__', device_id)

            d = NetMetaPacket(msg=m).marshall(
                compress=self.getCompressionType())
            # print 'sendDetail socket',self.sock,repr(d)
            self.sock.sendall(d)
            return True
        except:
            log_error(traceback.format_exc())
            if self.sock:
                self.sock.close()
            self.sock = None
            return False
        finally:
            #self.mtx.release()
            pass
示例#14
0
	def sendDetail(self,m):
		try:
			#self.mtx.acquire()
			if not self.sock:# and  not self.adapter:
				# import urlparse
				dest = (self.ep.host,self.ep.port)
				# scheme='tcp'
				# if isinstance(self.address,str):
				# 	cps = urlparse.urlparse(self.address)
				# 	scheme = cps.scheme.lower()
				# 	host,port = cps.netloc.split(':')
				# 	dest = (host,int(port))

				self.sock = gevent.socket.create_connection(dest,)
				# if scheme =='ssl':
				if self.ep.ssl:
					import ssl
					self.sock = gevent.ssl.wrap_socket(self.sock,)

				gevent.spawn(self.recv)
				#send token when connect on
				if self.token:
					m.extra.setPropertyValue('__token__',self.token)
				# 2014.7.1 scott
				device_id = RpcCommunicator.instance().getProperties().get('device_id')
				if device_id:
					m.extra.setPropertyValue('__device_id__',device_id)

			d = NetMetaPacket(msg=m).marshall(compress=self.getCompressionType())
			# print 'sendDetail socket',self.sock,repr(d)
			self.sock.sendall(d)
			return True
		except:
			log_error(traceback.format_exc())
			if self.sock:
				self.sock.close()
			self.sock = None
			return False
		finally:
			#self.mtx.release()
			pass
示例#15
0
	def recv(self):
		from communicator import  RpcCommunicator
		listener = RpcCommunicator.instance().getConnectionEventListener()
		if listener:
			listener.onConnected(self)

		while True:
			try:
				d = self.sock.receive()
				if not d:
					break
				self.onDataRecved( str(d) )
			except:
				# traceback.print_exc()
				break
		print 'websocket lost!'
		self.sock = None
		if self.cb_disconnect:
			self.cb_disconnect(self)
		if listener:
			listener.onDisconnected(self)
示例#16
0
    def recv(self):
        #		f = self.sock.makefile()
        #		print 'socket made..'
        listener = RpcCommunicator.instance().getConnectionEventListener()
        #print 'on connected:',listener
        if listener:
            listener.onConnected(self)

        while True:
            try:
                d = self.sock.recv(1000)
                # print 'socket recved:',repr(d)
                if not d:
                    break
                self.onDataRecved(d)
            except:
                # traceback.print_exc()
                break
        print 'socket lost!'
        self.sock = None
        if self.cb_disconnect:
            self.cb_disconnect(self)
        if listener:
            listener.onDisconnected(self)
示例#17
0
	def recv(self):
#		f = self.sock.makefile()
#		print 'socket made..'
		listener = RpcCommunicator.instance().getConnectionEventListener()
		#print 'on connected:',listener
		if listener:
			listener.onConnected(self)

		while True:
			try:
				d = self.sock.recv(1000)
				# print 'socket recved:',repr(d)
				if not d:
					break
				self.onDataRecved(d)
			except:
				# traceback.print_exc()
				break
		print 'socket lost!'
		self.sock = None
		if self.cb_disconnect:
			self.cb_disconnect(self)
		if listener:
			listener.onDisconnected(self)
示例#18
0
文件: tce.py 项目: adoggie/TCE
def getUniqueSequence():
	from communicator import RpcCommunicator
	return RpcCommunicator.instance().generateSeq()
示例#19
0
def currentServer():
    from communicator import RpcCommunicator
    return RpcCommunicator.instance().currentServer()
示例#20
0
def waitForShutdown():
    from communicator import RpcCommunicator
    RpcCommunicator.instance().waitForShutdown()
示例#21
0
文件: tce.py 项目: adoggie/TCE
def waitForShutdown():
	from communicator import RpcCommunicator
	RpcCommunicator.instance().waitForShutdown()
示例#22
0
文件: tce.py 项目: adoggie/TCE
def currentServer():
	from communicator import RpcCommunicator
	return RpcCommunicator.instance().currentServer()
示例#23
0
def getUniqueSequence():
    from communicator import RpcCommunicator
    return RpcCommunicator.instance().generateSeq()