def sendCmdToChip(self, args): sck = Gol().sckPool.get(args[0], None) if not sck: return self.sendOut('Chip is not online, socket %s is closed' % args[0]) # if not sck: return self.sendBack('Chip is not online, socket %s is closed'%args[0]) try: self.mode = 'command' self.factory.addMonitor(self) self.tp = sck.sendTPack(int(args[1]), args[2]) _fs = Gol()._formatPack_('TO', sck, self.tp) self.sendOut(msgpack.packb(_fs[:-1]) + '\r\n', True) # self.sendLine(msgpack.packb(_fs[:-1])+'\r\n') except Exception, e: self.sendOut('Command Format Error: <routecode> <body>')
def errorRoutePack(self, failure, tpack): # if Gol().env == 'test': # raise failure try: self.returnDPack(int(failure.getErrorMessage()), None, tpack.id) except Exception, e: Gol()._log_('MS', self, None, repr(failure.getBriefTraceback().replace('\n', ' ')))
def doTalkingToChip(self, tpack): if not self._mo: raise Exception(401) # tok = uuid.uuid1().hex tok, srv = Gol().getCallTunnel() host, port = srv.split(':') d = self._mo.chip(tpack.body['oth']) d.addCallback(lambda c: self.sendToChip( c, 1003, [c.id, 1, host, int(port), tok, 1], tpack.id)) # d.addCallback(lambda x: self.returnDPack(200, { 'srv' : srv, 'tok' : tok, 'rol' : 0 }, tpack.id)) return d
def recvPack(self, pack): if Gol().stop: return self._lastRecvAt = time.time() if type(pack) == DPack: tp = self.findWaitingTPack(pack.id) if tp: pack.peerToTPack(tp) # else: self.errorRoutePack(500, pack) else: raise Exception(400) Gol()._log_('FR', self, pack) # -*- Log -*- # d = defer.Deferred() d.addCallback(self.loadSession) d.addCallback(self.loadMo) d.addCallback(lambda x: Gol().route(self.__class__.__name__, pack. routeCode)(self, pack)) d.addCallbacks(self.finishRoutePack, lambda x: self.errorRoutePack(x, pack)) d.callback(pack)
def loadSession(self, pack): # Load Session by received package sid # If socket already has session: Check current session id with package sid # If socket has no session and package has sid: Load session from redis by package sid # if socket has no session and package has no sid: Create a new session for this new socket d = None if self._session != None: if pack.sid != self._session.id: raise Exception(401) else: d = Session.findById(pack.sid) if len( pack.sid) == 32 else Session.create({'ts': int(time.time())}) d.addCallback(lambda s: self.setSession(s)) d.addCallback( lambda s: Gol().addSck(self, s.id, self.factory._sckType)) return d
def doTalking(self, tpack): if not self._mo: raise Exception(401) tok = uuid.uuid1().hex srv = Gol().getVoiceTunnel() d = User.findByLogin(tpack.body["oth"]) d.addCallback(lambda u: self.notiToUser( u, 4101, { 'oth': self._mo.info['login'], 'hol': tpack.body['hol'], 'lnk': tpack.body['lnk'], 'srv': srv, 'tok': '01' + tok })) d.addCallback(lambda x: self.returnDPack(200, { 'srv': srv, 'tok': '00' + tok }, tpack.id)) return d
def makeService(self, options): _srvs = service.MultiService() _channel = socket.gethostname() Gol().init(options["env"]) application = service.Application("simcore") # self.initLog(application, options["env"], options["logfile"]) self.initRedis("%s/%s/redis.ini" % (options["pools"], options["env"])) self.initTurnServerList("%s/%s/turnServer.ini" % (options["pools"], options["env"])) self.initServices(_srvs, _channel) _srvs.setServiceParent = wrap_setServiceParent(_srvs.setServiceParent, options) _srvs.setServiceParent(application) return _srvs
def initServices(self, srvs, chn): from simcore.core.shp import RedisSubFactory from simcore.servers.chipMo import BoxMoFactory, ChipMoFactory from simcore.servers.phoneMo import PhoneMoFactory from simcore.servers.phoneNoti import PhoneNotiFactory from simcore.servers.tools import ToolsFactory _loadServ = [[BoxMoFactory, 8901], [ChipMoFactory, 8902], [PhoneMoFactory, 9901], [PhoneNotiFactory, 9902]] [ internet.TCPServer(srv[1], srv[0](chn)).setServiceParent(srvs) for srv in _loadServ ] tf = ToolsFactory(chn) Gol().setMonitor(tf) internet.TCPServer(9898, tf).setServiceParent(srvs) internet.TCPClient(SrPushdb().host, SrPushdb().port, RedisSubFactory(chn)).setServiceParent(srvs)
def stop(self): Gol().stop = True for sck in Gol().sckPool.values(): sck.closeConnection(True) # self.sendBack('Finish Stop') self.sendOut('Finish Stop')
def initTurnServerList(self, turnini): with open(turnini, 'r') as f: Gol().setCallTunnels([s.strip() for s in f.readlines()])
def connectionMade(self): Gol()._log_('CM', self, None, '>> %s' % self.__class__.__name__) # -*- Log -*- # reactor.callLater(self._heart, self.closeConnection)
def findSck(self, SckId): return Gol().findSck(SckId)
def send(self, pack): Gol()._log_('TO', self, pack) # -*- Log -*- # self.transport.write(pack.dump()) return pack
def connectionLost(self, reason): Gol()._log_('CL', self, None, '<< %s' % self.__class__.__name__) # -*- Log -*- # return Gol().delSck( self._session.id, self.factory._sckType) if self._session else defer.Deferred()
def sockets(self, args): self.sendOut([s._session for s in Gol().sckPool.values()])
def findSck(self, sid, sckType=''): return Gol().findSck(sid, sckType)
def decorator(callback): Gol().addRoute(callback.__module__.replace('simcore.servers.', ''), code, callback) return callback