test-session: Add multiple session support
authorJeff Zheng <jeff.zheng@intel.com>
Wed, 14 Sep 2011 07:30:01 +0000 (15:30 +0800)
committerDaniel Wagner <daniel.wagner@bmw-carit.de>
Thu, 15 Sep 2011 08:41:56 +0000 (10:41 +0200)
Add an new parameter session_name in test-session so all session
operations are based on app_path and session_name

test/test-session

index 5f0685b..119faca 100755 (executable)
@@ -41,13 +41,14 @@ class Notification(dbus.service.Object):
        @dbus.service.method("net.connman.Notification",
                                in_signature='', out_signature='')
        def Release(self):
-               print("Release")
-               self.app.release()
+               print "Release %s" % (self._object_path)
+               session_name = self._object_path.split('/')[-1]
+               self.app.release(sessioin_name)
 
        @dbus.service.method("net.connman.Notification",
                                in_signature='a{sv}', out_signature='')
        def Update(self, settings):
-               print "Update called"
+               print "Update called at %s" % (self._object_path)
 
                try:
                        for key in settings.keys():
@@ -67,12 +68,8 @@ class SessionApplication(dbus.service.Object):
                dbus.service.Object.__init__(self, bus, object_path)
 
                self.manager = None
-               self.notify_path = object_path + "/notify"
-               self.notify = None
-               self.session_path = None
                self.mainloop = mainloop
-               self.session = None
-               self.settings = { }
+               self.sessions = {}
 
                try:
                        bus = dbus.SystemBus()
@@ -92,22 +89,27 @@ class SessionApplication(dbus.service.Object):
                        else:
                                print "connman disappeared on D-Bus"
                                self.manager = None
-                               self.session = None
-                               if self.notify:
-                                       self.notify.remove_from_connection()
-                                       self.notify = None
+                               for s in self.sessions.keys():
+                                       self.sessions[s]['notify'].remove_from_connection()
+                                       self.sessions[s]['notify'] = None
+
+                               self.sessions = {}
 
                except dbus.DBusException:
                        traceback.print_exc()
                        exit(1)
 
-       def release(self):
-               if self.session:
-                       self.manager.DestroySession(self.session_path)
-                       self.session = None
-               if self.notify:
-                       self.notify.remove_from_connection()
-                       self.notify = None
+       def release(self, session_name):
+               s = self.find_session(session_name)
+               if not s:
+                       return
+               if s['session']:
+                       self.manager.DestroySession(s['session_path'])
+                       s['session'] = None
+               if s['notify']:
+                       s['notify'].remove_from_connection()
+                       s['notify'] = None
+               del self.sessions[session_name]
 
        def type_convert(self, key, value):
                if key in [ "AllowedBearers", "RoamingPolicy" ]:
@@ -123,28 +125,37 @@ class SessionApplication(dbus.service.Object):
 
                return value
 
+       def find_session(self, session_name):
+               if not session_name in self.sessions.keys():
+                       return None
+               return self.sessions[session_name]
+
        @dbus.service.method("com.example.TestSession",
                                in_signature='', out_signature='')
-       def CreateSession(self):
+       def CreateSession(self, session_name):
                print "Create session"
 
-               if self.session:
-                       print "already running a session -> drop reqest"
+               s = self.find_session(session_name)
+               if s and s['session'] :
+                       print "Session %s already created-> drop reqest" % (session_name)
                        return
 
                try:
                        bus = dbus.SystemBus()
 
-                       self.notify = Notification(bus, self, self.notify_path)
-                       self.notify.add_to_connection(bus, self.notify_path)
-
-                       self.session_path = self.manager.CreateSession(self.settings, self.notify_path)
-
-                       print "notify path %s" % (self.notify_path)
-                       print "session path %s" % (self.session_path)
-
-                       self.session = dbus.Interface(bus.get_object("net.connman", self.session_path),
+                       if s == None:
+                               s = {}
+                       s['notify_path'] = self._object_path + "/" + session_name
+                       s['notify'] = Notification(bus, self, s['notify_path'])
+                       s['notify'].add_to_connection(bus, s['notify_path'])
+                       if not 'settings' in s.keys():
+                               s['settings'] = {};
+                       s['session_path'] = self.manager.CreateSession(s['settings'], s['notify_path'])
+                       print "notify path %s" % (s['notify_path'])
+                       print "session path %s" % (s['session_path'])
+                       s['session'] = dbus.Interface(bus.get_object("net.connman", s['session_path']),
                                                      "net.connman.Session")
+                       self.sessions[session_name] = s
 
                except dbus.DBusException, e:
                        if e.get_dbus_name() in ['net.connman.Error.Failed']:
@@ -155,30 +166,32 @@ class SessionApplication(dbus.service.Object):
 
        @dbus.service.method("com.example.TestSession",
                                in_signature='', out_signature='')
-       def DestroySession(self):
+       def DestroySession(self, session_name):
                print "Destroy session"
 
-               if self.session == None:
-                       print "no session running -> drop request"
+               s = self.find_session(session_name)
+               if s == None or s['session'] == None:
+                       print "The session is not running -> drop request"
                        return
 
                try:
-                       self.release()
+                       self.release(session_name)
                except dbus.DBusException:
                        traceback.print_exc()
                        exit(1)
 
        @dbus.service.method("com.example.TestSession",
                                in_signature='', out_signature='')
-       def Connect(self):
+       def Connect(self, session_name):
                print "Connect session"
 
-               if self.session == None:
-                       print "no session running -> drop request"
+               s = self.find_session(session_name)
+               if s == None or s['session'] == None:
+                       print "The session is not running -> drop request"
                        return
 
                try:
-                       self.session.Connect()
+                       s['session'].Connect()
                except dbus.DBusException, e:
                        if e.get_dbus_name() in ['net.connman.Error.Failed']:
                                print e.get_dbus_message()
@@ -188,15 +201,16 @@ class SessionApplication(dbus.service.Object):
 
        @dbus.service.method("com.example.TestSession",
                                in_signature='', out_signature='')
-       def Disconnect(self):
+       def Disconnect(self, session_name):
                print "Disconnect session"
 
-               if self.session == None:
-                       print "no session running -> drop request"
+               s = self.find_session(session_name)
+               if s == None or s['session'] == None:
+                       print "The session is not running -> drop request"
                        return
 
                try:
-                       self.session.Disconnect()
+                       s['session'].Disconnect()
                except dbus.DBusException, e:
                        if e.get_dbus_name() in ['net.connman.Error.Failed']:
                                print e.get_dbus_message()
@@ -205,17 +219,18 @@ class SessionApplication(dbus.service.Object):
                        exit(1)
 
        @dbus.service.method("com.example.TestSession",
-                               in_signature='sv', out_signature='')
-       def Change(self, key, value):
+                               in_signature='', out_signature='')
+       def Change(self, session_name, key, value):
                print "Update session settings"
 
-               if self.session == None:
-                       print "no session running -> drop request"
+               s = self.find_session(session_name)
+               if s == None or s['session'] == None:
+                       print "The session is not running -> drop request"
                        return
 
                try:
                        val = self.type_convert(key, value)
-                       self.session.Change(key, val)
+                       s['session'].Change(key, val)
                except dbus.DBusException, e:
                        if e.get_dbus_name() in ['net.connman.Error.Failed']:
                                print e.get_dbus_message()
@@ -224,11 +239,24 @@ class SessionApplication(dbus.service.Object):
                        exit(1)
 
        @dbus.service.method("com.example.TestSession",
-                               in_signature='sv', out_signature='')
-       def Configure(self, key, value):
+                               in_signature='', out_signature='')
+       def Configure(self, session_name, key, value):
                print "Configure session settings"
+               s = self.find_session(session_name)
+               if s == None:
+                       s = {}
+                       s['notify_path'] = None
+                       s['notify'] = None
+                       if not 'settings' in s.keys():
+                               s['settings'] = {};
+                       s['session_path'] = None
+                       s['session'] = None
+                       self.sessions[session_name] = s
+               if s and s['session']:
+                       print "The session is running, use change -> drop request"
+                       return
                val = self.type_convert(key, value)
-               self.settings[key] = val
+               s['settings'][key] = val
 
 def main():
        if len(sys.argv) < 2:
@@ -236,12 +264,12 @@ def main():
                print ""
                print "  enable"
                print "  disable"
-               print "  create <app_path>"
-               print "  destroy <app_path>"
-               print "  connect <app_path>"
-               print "  disconnect <app_path>"
-               print "  change <app_path> <key> <value>"
-               print "  configure <app_path> <key> <value>"
+               print "  create <app_path> <session_name>"
+               print "  destroy <app_path> <session_name>"
+               print "  connect <app_path> <session_name>"
+               print "  disconnect <app_path> <session_name>"
+               print "  change <app_path> <session_name> <key> <value>"
+               print "  configure <app_path> <session_name> <key> <value>"
                print ""
                print "  run <app_path>"
                sys.exit(1)
@@ -284,30 +312,30 @@ def main():
                             "com.example.TestSession")
 
        if sys.argv[1] == "create":
-               app.CreateSession()
+               app.CreateSession(sys.argv[3])
 
        elif sys.argv[1] == "destroy":
-               app.DestroySession()
+               app.DestroySession(sys.argv[3])
 
        elif sys.argv[1] == "connect":
-               app.Connect()
+               app.Connect(sys.argv[3])
 
        elif sys.argv[1] == "disconnect":
-               app.Disconnect()
+               app.Disconnect(sys.argv[3])
 
        elif sys.argv[1] == "change":
                if len(sys.argv) < 5:
                        print "Arguments missing"
                        sys.exit(1)
 
-               app.Change(sys.argv[3], sys.argv[4:])
+               app.Change(sys.argv[3], sys.argv[4], sys.argv[5:])
 
        elif sys.argv[1] == "configure":
                if len(sys.argv) < 5:
                        print "Arguments missing"
                        sys.exit(1)
 
-               app.Configure(sys.argv[3], sys.argv[4:])
+               app.Configure(sys.argv[3], sys.argv[4], sys.argv[5:])
 
        else:
                print "Unknown command '%s'" % sys.argv[1]