class ObexServerSimulator(object): def __init__(self, name, port, ioSvc): self._ioSvc = ioSvc self._osName = self.__class__.__name__ + '-' + name self._sockName = self._osName self._sockPort = port self._sockAddr = '127.0.0.1:' + str(self._sockPort) self._os = ObexServer(self._osName, self._sockName, self._sockPort, self._ioSvc) def getUnixSockAddr(self): return self._sockName def getTcpSockAddr(self): return '127.0.0.1:' + str(self._sockPort) def getInjector(self, injName, sockType): if not hasattr(self, '_oc') or not self._oc: self._oc = CreateObject(objxp.ObexClient, injName, self._ioSvc, None) if sockType == 'unix': self._oc.connect(injName, self.getUnixSockAddr(), 0, 0) elif sockType == 'tcp': self._oc.connect(injName, self.getTcpSockAddr(), 0, 0) else: self.assertTrue(False) cs = WaitUntil(self._ioSvc, lambda: self._oc.getSession(injName)) return objxp.DynamicCast_ObexRemoteClientSession(cs) def delInjector(self, injName): if hasattr(self, '_oc'): self._oc.disconnect(injName)
class ObexClientSimulator(object): def __init__(self, name, sockAddr, subscribePath, ioSvc): self._ioSvc = ioSvc self._ccb = CreateObject(objxp.DefaultConnectionCallback) self._scb = CreateObject(objxp.DefaultSubscriptionCallback) self._ocb = CreateObject(objxp.ObexCallbackReceiver, name + 'ocb') self._oc = CreateObject(objxp.ObexClient, name, ioSvc, self._ccb) self._oc.connect(name, sockAddr, 3000, 0) cs = WaitUntil(self._ioSvc, lambda: self._oc.getSession(name)) self._cs = objxp.DynamicCast_ObexRemoteClientSession(cs) self._cs.subscribe(subscribePath, self._scb) self._cs.registerCallback(subscribePath, self._ocb) def callbackObjCount(self): return self._ocb.count() def popCallbackObj(self): if self.callbackObjCount() > 0: cbObj = self._ocb.pop() return cbObj.path(), cbObj.obj() else: return "", None def clearCallbackObj(self): self._ocb.clear() def getObject(self, path): return self._cs.getObject(path)
class ObexRemoteClientTest(TestBase, ObexTestCommon): def setUpCommon(self): EnableObjStats() SetTraceLevel(8) self._iosvc = CreateObject(objxp.BasicIoService) self._scb = CreateObject(objxp.DefaultSubscriptionCallback) self._ccb = CreateObject(objxp.DefaultConnectionCallback) self._ocb = CreateObject(objxp.ObexCallbackReceiver, "ocb") self._os = CreateObject(objxp.ObexServer, "os", "os", 0, self._iosvc) self._oc = CreateObject(objxp.ObexClient, "oc", self._iosvc, self._ccb) self._sessionKey = "noncohab" self._oc.connect(self._sessionKey, "os", 3000, 0) self._clientSession = WaitUntil( self._iosvc, lambda: self._oc.getSession(self._sessionKey)) self.assertTrue(self._clientSession) def tearDownCommon(self): self._oc.disconnect(self._sessionKey) del self._clientSession del self._oc del self._os del self._ocb del self._ccb del self._scb del self._iosvc logging.info("ObjStats: " + GetObjStats()) self.assertFalse(bool(json.loads(GetObjStats())))