###############################################################################
-VERSION = "0.4.0"
+VERSION = "0.6.0"
OPENDOOR = False
CREDENTIALS = {}
WHITELIST = []
+SERVICELIST = []
NETMASK = []
###############################################################################
+###############################################################################
+def createClassName(objectPath):
+ return re.sub('/', '_', objectPath[1:])
+
################################################################################
class DynDBusClass():
def __init__(self, className, globalCtx, localCtx):
- self.className = className
self.xmlCB = XmlCbParser(self)
self.signature = {}
self.class_code = ExecCode(globalCtx, localCtx)
self.class_code.indent_increment = 4
self.class_code.append_stmt("import dbus")
self.class_code.append_stmt("\n")
- self.class_code.append_stmt("class " + self.className + "(dbus.service.Object):")
+ self.class_code.append_stmt("class " + className + "(dbus.service.Object):")
self.class_code.indent()
## Overload of __init__ method
self.def_method("__init__")
- self.add_method("bus, callback=None, objPath='/sample', busName='org.cloudeebus'")
+ self.add_method("bus, callback=None, objPath='/sample', srvName='org.cloudeebus'")
self.add_stmt("self.bus = bus")
self.add_stmt("self.objPath = objPath")
+ self.add_stmt("self.srvName = srvName")
self.add_stmt("self.callback = callback")
- self.add_stmt("dbus.service.Object.__init__(self, conn=bus, bus_name=busName)")
+ self.add_stmt("dbus.service.Object.__init__(self, conn=bus, bus_name=srvName)")
self.end_method()
## Create 'add_to_connection' method
def add_body_method(self):
if (self.methodToAdd != None):
if (self.args_str != str()):
- self.class_code.append_stmt("self.callback('" + self.methodToAdd + "', self.objPath, '" + self.ifName + "', " + "dbus_async_cb, dbus_async_err_cb, %s)" % self.args_str)
+ self.class_code.append_stmt("self.callback(self.srvName,'" + self.methodToAdd + "', self.objPath, '" + self.ifName + "', " + "dbus_async_cb, dbus_async_err_cb, %s)" % self.args_str)
else:
- self.class_code.append_stmt("self.callback('" + self.methodToAdd + "', self.objPath, '" + self.ifName + "', " + "dbus_async_cb, dbus_async_err_cb)")
+ self.class_code.append_stmt("self.callback(self.srvName,'" + self.methodToAdd + "', self.objPath, '" + self.ifName + "', " + "dbus_async_cb, dbus_async_err_cb)")
def add_body_signal(self):
self.class_code.append_stmt("return") ## TODO: Remove and fix with code ad hoc
self.permissions = {};
self.permissions['permissions'] = permissions['permissions']
self.permissions['authextra'] = permissions['authextra']
+ self.permissions['services'] = permissions['services']
self.proxyObjects = {}
self.proxyMethods = {}
self.pendingCalls = []
self.localCtx = locals()
self.globalCtx = globals()
+ self.patternDbus = re.compile('^dbus\.(\w+)')
+ self.patternDbusBoolean = re.compile('^dbus.Boolean\((\w+)\)$')
+ self.patternDbusByte = re.compile('^dbus.Byte\((\d+)\)$')
+ self.patternDbusInt16 = re.compile('^dbus.Int16\((\d+)\)$')
+ self.patternDbusInt32 = re.compile('^dbus.Int32\((\d+)\)$')
+ self.patternDbusInt64 = re.compile('^dbus.Int64\((\d+)\)$')
+ self.patternDbusUInt16 = re.compile('^dbus.UInt16\((\d+)\)$')
+ self.patternDbusUInt32 = re.compile('^dbus.UInt32\((\d+)\)$')
+ self.patternDbusUInt64 = re.compile('^dbus.UInt64\((\d+)\)$')
+ self.patternDbusDouble = re.compile('^dbus.Double\((\d+\.\d+)\)$')
def proxyObject(self, busName, serviceName, objectName):
'''
self.proxyMethods[id] = obj.get_dbus_method(methodName, interfaceName)
return self.proxyMethods[id]
+ def decodeArgs( self, args ):
+ if isinstance( args, list ):
+ newArgs = []
+ for arg in args:
+ newArgs.append( self.decodeArgs( arg ))
+ return newArgs
+ elif isinstance( args, dict ):
+ newDict = {}
+ for key, value in args.iteritems():
+ key = self.decodeArgs( key )
+ newValue = self.decodeArgs( value )
+ newDict[key] = newValue
+ return newDict
+ elif isinstance( args, basestring ):
+ newArg = self.decodeDbusString( args )
+ return newArg
+ else:
+ return args
+
+ def decodeDbusString( self, dbusString ):
+ matchDbus = self.patternDbus.match( dbusString )
+
+ if not matchDbus:
+ return dbusString
+
+
+ result = {
+ "Boolean" : lambda x : dbus.Boolean( self.patternDbusBoolean.match( x ).group( 1 ).lower() in ("yes", "true", "t", "1")),
+ "Byte" : lambda x : dbus.Byte( int( self.patternDbusByte.match( x ).group( 1 ))),
+ "Int16" : lambda x : dbus.Int16( self.patternDbusInt16.match( x ).group( 1 )),
+ "Int32" : lambda x : dbus.Int32( self.patternDbusInt32.match( x ).group( 1 )),
+ "Int64" : lambda x : dbus.Int64( self.patternDbusInt64.match( x ).group( 1 )),
+ "UInt16" : lambda x : dbus.UInt16( self.patternDbusUInt16.match( x ).group( 1 )),
+ "UInt32" : lambda x : dbus.UInt32( self.patternDbusUInt32.match( x ).group( 1 )),
+ "UInt64" : lambda x : dbus.UInt64( self.patternDbusUInt64.match( x ).group( 1 )),
+ "Double" : lambda x : dbus.Double( self.patternDbusDouble.match( x ).group( 1 ))
+ }[matchDbus.group(1)](dbusString)
+
+ return result
@exportRpc
def dbusRegister(self, list):
# parse JSON arg list
args = []
if len(list) == 6:
- args = json.loads(list[5])
+ jsonArgs = json.loads(list[5])
+ if jsonArgs:
+ args = self.decodeArgs( jsonArgs )
# get dbus proxy method
method = self.proxyMethod(*list[0:5])
@exportRpc
def emitSignal(self, list):
'''
- arguments: agentObjectPath, signalName, result (to emit)
+ arguments: agentObjectPath, signalName, args (to emit)
'''
objectPath = list[0]
className = re.sub('/', '_', objectPath[1:])
signalName = list[1]
- result = list[2]
- if (self.serviceAgents.has_key(className) == True):
- exe_str = "self.serviceAgents['"+ className +"']."+ signalName + "(" + str(result) + ")"
+ args = []
+ jsonArgs = json.loads(list[2])
+ print "JSON Arguments:", jsonArgs
+ if jsonArgs:
+ args = self.decodeArgs( jsonArgs )
+ print "Decoded Arguments: ", args
+
+ if (self.serviceAgents.has_key(className) == True):
+ exe_str = "self.serviceAgents['"+ className +"']."+ signalName + "("
+ if len(args) > 0:
+ exe_str += json.dumps(args[0])
+ for idx in args[1:]:
+ exe_str += "," + json.dumps(idx)
+ exe_str += ")"
eval(exe_str, self.globalCtx, self.localCtx)
else:
raise Exception("No object path " + objectPath)
else:
raise Exception("No methodID " + methodId)
- def srvCB(self, name, objPath, ifName, async_succes_cb, async_error_cb, *args):
- methodId = self.srvName + "#" + objPath + "#" + ifName + "#" + name
+ def srvCB(self, srvName, name, objPath, ifName, async_succes_cb, async_error_cb, *args):
+ methodId = srvName + "#" + objPath + "#" + ifName + "#" + name
cb = { 'successCB': async_succes_cb,
'errorCB': async_error_cb}
if methodId not in self.servicePendingCalls:
self.servicePendingCalls[methodId] = {'count': 0, 'calls': []}
- pendingCallStr = json.dumps({'callIndex': len(self.servicePendingCalls[methodId]['calls']), 'args': args})
+
+ try:
+ pendingCallStr = json.dumps({'callIndex': len(self.servicePendingCalls[methodId]['calls']), 'args': args})
+ except Exception, e:
+ args = eval( str(args).replace("dbus.Byte", "dbus.Int16") )
+ pendingCallStr = json.dumps({'callIndex': len(self.servicePendingCalls[methodId]['calls']), 'args': args})
+
self.servicePendingCalls[methodId]['calls'].append(cb)
self.servicePendingCalls[methodId]['count'] = self.servicePendingCalls[methodId]['count'] + 1
factory.dispatch(methodId, pendingCallStr)
arguments: busName, srvName
'''
busName = list[0]
- self.bus = cache.dbusConnexion( busName['name'] )
- self.srvName = list[1]
- if (self.services.has_key(self.srvName) == False):
- self.services[self.srvName] = dbus.service.BusName(name = self.srvName, bus = self.bus)
- return self.srvName
+ self.bus = cache.dbusConnexion( busName )
+ srvName = list[1]
+ if not OPENDOOR and (SERVICELIST == [] or SERVICELIST != [] and self.permissions['services'] == None):
+ SERVICELIST.index(srvName)
+
+ if (self.services.has_key(srvName) == False):
+ self.services[srvName] = dbus.service.BusName(name = srvName, bus = self.bus)
+ return srvName
@exportRpc
def serviceRelease(self, list):
'''
arguments: busName, srvName
'''
- self.srvName = list[0]
- if (self.services.has_key(self.srvName) == True):
- self.services.pop(self.srvName)
- return self.srvName
+ srvName = list[0]
+ if (self.services.has_key(srvName) == True):
+ self.services.pop(srvName)
+ return srvName
else:
- raise Exception(self.srvName + " do not exist")
+ raise Exception(srvName + " does not exist")
@exportRpc
def serviceAddAgent(self, list):
'''
arguments: objectPath, xmlTemplate
'''
- self.agentObjectPath = list[0]
- xmlTemplate = list[1]
- self.className = re.sub('/', '_', self.agentObjectPath[1:])
- if (self.dynDBusClasses.has_key(self.className) == False):
- self.dynDBusClasses[self.className] = DynDBusClass(self.className, self.globalCtx, self.localCtx)
- self.dynDBusClasses[self.className].createDBusServiceFromXML(xmlTemplate)
- self.dynDBusClasses[self.className].declare()
+ srvName = list[0]
+ agentObjectPath = list[1]
+ xmlTemplate = list[2]
+ className = createClassName(agentObjectPath)
+ if (self.dynDBusClasses.has_key(className) == False):
+ self.dynDBusClasses[className] = DynDBusClass(className, self.globalCtx, self.localCtx)
+ self.dynDBusClasses[className].createDBusServiceFromXML(xmlTemplate)
+ self.dynDBusClasses[className].declare()
## Class already exist, instanciate it if not already instanciated
- if (self.serviceAgents.has_key(self.className) == False):
- self.serviceAgents[self.className] = eval(self.className + "(self.bus, callback=self.srvCB, objPath=self.agentObjectPath, busName=self.srvName)", self.globalCtx, self.localCtx)
+ if (self.serviceAgents.has_key(className) == False):
+ self.serviceAgents[className] = eval(className + "(self.bus, callback=self.srvCB, objPath='" + agentObjectPath + "', srvName='" + srvName + "')", self.globalCtx, self.localCtx)
- self.serviceAgents[self.className].add_to_connection()
- return (self.agentObjectPath)
+ self.serviceAgents[className].add_to_connection()
+ return (agentObjectPath)
@exportRpc
def serviceDelAgent(self, list):
arguments: objectPath, xmlTemplate
'''
agentObjectPath = list[0]
- className = re.sub('/', '_', agentObjectPath[1:])
-
+ className = createClassName(agentObjectPath)
+
if (self.serviceAgents.has_key(className)):
- self.serviceAgents[self.className].remove_from_connection()
- self.serviceAgents.pop(self.className)
+ self.serviceAgents[className].remove_from_connection()
+ self.serviceAgents.pop(className)
else:
raise Exception(agentObjectPath + " doesn't exist!")
def getAuthPermissions(self, key, extra):
return {'permissions': extra.get("permissions", None),
- 'authextra': extra.get("authextra", None)}
+ 'authextra': extra.get("authextra", None),
+ 'services': extra.get("services", None)}
def getAuthSecret(self, key):
secret = CREDENTIALS.get(key, None)
if key is None:
raise Exception("Authentication failed")
# check permissions, array.index throws exception
- for req in permissions['permissions']:
+ if (permissions['permissions'] != None):
+ for req in permissions['permissions']:
WHITELIST.index(req);
+ # check allowed service creation, array.index throws exception
+ if (permissions['services'] != None):
+ for req in permissions['services']:
+ SERVICELIST.index(req);
# create cloudeebus service instance
self.cloudeebusService = CloudeebusService(permissions)
# register it for RPC
parser.add_argument('-c', '--credentials',
help='path to credentials file')
parser.add_argument('-w', '--whitelist',
- help='path to whitelist file')
+ help='path to whitelist file (DBus services to use)')
+ parser.add_argument('-s', '--servicelist',
+ help='path to servicelist file (DBus services to export)')
parser.add_argument('-n', '--netmask',
help='netmask,IP filter (comma separated.) eg. : -n 127.0.0.1,192.168.2.0/24,10.12.16.0/255.255.255.0')
WHITELIST = json.load(jfile)
jfile.close()
+ if args.servicelist:
+ jfile = open(args.servicelist)
+ SERVICELIST = json.load(jfile)
+ jfile.close()
+
if args.netmask:
iplist = args.netmask.split(",")
for ip in iplist:
ipAllowed = ip
mask = "255.255.255.255"
NETMASK.append( {'ipAllowed': ipV4ToHex(ipAllowed), 'mask' : ipV4ToHex(mask)} )
-
+
uri = "ws://localhost:" + args.port
factory = WampServerFactory(uri, debugWamp = args.debug)