clXMLRPC.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2008 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 ## @package clXMLRPC
00022 # This module will - perhaps - be used for inter process
00023 # communication later.
00024 # Since I just need this feature in order to connect pyjama
00025 # with jamendo playlists, I will use a more failsafe mechanism
00026 # for the time being. 
00027 
00028 
00029 from SimpleXMLRPCServer import SimpleXMLRPCServer
00030 
00031 import socket
00032 socket.setdefaulttimeout(3)
00033 import xmlrpclib
00034 
00035 from threading import Thread
00036 
00037 class XmlrpcHandler:
00038     def __init__(self, pyjama):
00039         self.pyjama = pyjama
00040 
00041     def test(self, inte):
00042         return True
00043 
00044     def load_playlist(self, playlist_file):
00045         print ("Got playlist %s" % playlist_file)
00046         self.pyjama.playlist_to_load = playlist_file
00047 
00048     #~ def print_string(self, string):
00049         #~ print string
00050 
00051 class XMLRPC:
00052     def __init__(self, pyjama):
00053         self.pyjama = pyjama
00054 
00055         self.connect_or_create()
00056 
00057     def connect_or_create(self):
00058         self.server = None
00059         self.role = None
00060         ## check if a server is running
00061         # if not, serve
00062         try:
00063             self.server = xmlrpclib.ServerProxy("http://localhost:50506", allow_none=True) 
00064             self.server.test(23)
00065             self.role = "client"
00066             print ("Found XMLRPC-Server on localhost:50506")
00067         except socket.error, inst:
00068             thr = Thread(target = self.serve, args = ())
00069             thr.start()
00070             self.role = "server"
00071 
00072     def send_playlist(self, playlist_file):
00073         self.server.load_playlist(playlist_file)
00074 
00075     # run by client
00076     def test(self):
00077         if self.role == "client":
00078             try:
00079                 self.server.test(23)
00080             except:
00081                 print ("Server no more running")
00082                 self.connect_or_create()
00083                 
00084 
00085     def quit(self):
00086         if self.role == "server":
00087             try:
00088                 self.server.shutdown()
00089             except Exception, inst:
00090                 print ("Error shutting down the XMLRPC-server: %s" % inst)
00091 
00092     def serve(self):
00093         self.server = SimpleXMLRPCServer(("localhost", 50506), allow_none=True)
00094         self.server.register_instance(XmlrpcHandler(self.pyjama))
00095         print ("Running XMLRPC-Server on localhost:50506")
00096         self.server.serve_forever()
00097         return self.server

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8