- Timestamp:
- 01/26/06 10:14:22 (19 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykoticon/trunk/bin/pykoticon
r87 r88 31 31 import socket 32 32 import threading 33 import xmlrpclib 33 34 import SimpleXMLRPCServer 34 35 … … 68 69 class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer) : 69 70 """My own server class.""" 70 def __init__(self, frame, printserver, localport) :71 myIPAddress = socket.gethostbyaddr(socket.gethostname())[2][0]72 SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self, ( myIPAddress, localport))71 allow_reuse_address = True 72 def __init__(self, frame, printserver, localport, debug=False) : 73 SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self, ('0.0.0.0', localport)) 73 74 self.printServer = printserver 74 75 self.frame = frame 76 self.debug = debug 75 77 loop = threading.Thread(target=self.mainloop) 76 78 loop.start() 77 79 80 def logDebug(self, message) : 81 """Logs a debug message if debug mode is active.""" 82 if self.debug : 83 sys.stderr.write("%s\n" % message) 84 78 85 def export_quitApplication(self) : 79 86 """Makes the application quit.""" 87 self.logDebug("Remote host asked to close the application.") 80 88 self.frame.quitEvent.set() 81 89 return True … … 83 91 def export_openDialog(self) : 84 92 """Opens a dialog to ask username, password, etc...""" 85 print "Open dialog !"93 self.logDebug("Open dialog !") 86 94 return ("jerome", "blah") 95 96 def export_nop(self) : 97 """Does nothing, but allows a clean shutdown from the frame itself.""" 98 self.logDebug("No operation !") 99 return True 87 100 88 101 def verify_request(self, request, client_address) : … … 90 103 (client, port) = client_address 91 104 if socket.gethostbyname(self.printServer) == client : 105 self.logDebug("%s accepted." % client) 92 106 return True 93 107 else : 94 108 # Unauthorized access ! 109 self.logDebug("%s rejected." % client) 95 110 return False 96 111 … … 103 118 self.register_function(self.export_openDialog) 104 119 self.register_function(self.export_quitApplication) 120 self.register_function(self.export_nop) 105 121 while not self.frame.quitEvent.isSet() : 122 self.logDebug("Loop!") 106 123 self.handle_request() 107 self.frame.Close() 124 self.server_close() 125 try : 126 if not self.frame.closing : 127 self.frame.Close() 128 except : 129 # Probably already closed 130 pass 108 131 sys.exit(0) 109 132 … … 148 171 wxPython.wx.EVT_CLOSE(self, self.OnClose) 149 172 173 self.closing = False 174 175 def closeServer(self) : 176 """Tells the xml-rpc server to exit.""" 177 if not self.quitEvent.isSet() : 178 self.quitEvent.set() 179 server = xmlrpclib.ServerProxy("http://localhost:%s" % self.port) 180 try : 181 server.nop() 182 except : 183 # Probably already stopped 184 pass 185 150 186 def postInit(self, printserver, localport) : 151 187 """Starts the XML-RPC server.""" 152 188 self.quitEvent = threading.Event() 153 self.server = MyXMLRPCServer(self, printserver, localport) 189 self.port = localport 190 self.server = MyXMLRPCServer(self, printserver, localport, debug=True) 154 191 155 192 def OnIconify(self, event) : … … 165 202 def OnClose(self, event) : 166 203 sys.stderr.write("Close event !\n") 167 if not self.quitEvent.isSet() :168 self.quitEvent.set()204 self.closing = True 205 self.closeServer() 169 206 if hasattr(self, "menu") : 170 207 self.menu.Destroy()