[Bio] / Speck / SpeckServer.py Repository:
ViewVC logotype

View of /Speck/SpeckServer.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (download) (as text) (annotate) (vendor branch)
Thu Sep 9 19:54:06 2004 UTC (15 years, 2 months ago) by sheradon
Branch: init, MAIN
CVS Tags: v0, HEAD
Changes since 1.1: +0 -0 lines
Initial load to CVS

import BaseHTTPServer
import sys, os, xmlrpclib, traceback
from wxPython.wx import * 

class SpeckServer(BaseHTTPServer.HTTPServer):
	def __init__(self, server_address, RequestHandlerClass, canvas):
		BaseHTTPServer.HTTPServer.__init__(self, server_address, RequestHandlerClass)
		self.canvas = canvas

	def go(self):
		# The try/except prevents "unhandled exception in thread" msg when we exit
		try: self.serve_forever()
		except: pass 

class SpeckRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
	def do_POST(self):
		try:
			# get arguments
			data = self.rfile.read(int(self.headers["content-length"]))
			params, method = xmlrpclib.loads(data)

			# generate response
			try:
				response = self.call(method, params)
				# wrap response in a singleton tuple
				response = (response,)
			except:
				traceback.print_exc()
				# report exception back to server
				response = xmlrpclib.dumps(xmlrpclib.Fault(1, "%s:%s" % sys.exc_info()[:2]))
			else:
				response = xmlrpclib.dumps(response, methodresponse=1)
		except:
			# internal error, report as HTTP server error
			self.send_response(500)
			self.end_headers()
		else:
			# got a valid XML RPC response
			self.send_response(200)
			self.send_header("Content-type", "text/xml")
			self.send_header("Content-length", str(len(response)))
			self.end_headers()
			self.wfile.write(response)

			# shut down the connection (from Skip Montanaro)
			self.wfile.flush()
			self.connection.shutdown(1)

	# I don't like how you spam my console. Stop it.
	def log_message(self, format, *params): pass

	# -----------------------------
	def call(self, method, params):
	# -----------------------------
		"""Contains handlers for received RPC events. They just create a wxEvent
			equivalent to the one on the RemoteControl and send it to the window.
			@type method: string
			@type params: string
			@rtype: int
		"""
		if method == 'exit':
			# Stop listening on socket so we can open another later
			self.server.socket.close()
			# Catch the SystemExit exception so xmlrpclib doesn't gag on it
			try: sys.exit(0)
			except SystemExit: return 0

		# Keyboard events...
		if method == 'fireKeyDown':
			(keyCode,) = params 
			e = wxKeyEvent(wxEVT_KEY_DOWN)
			e.m_keyCode = keyCode
			wxPostEvent(self.server.canvas, e)
			return 1
		elif method == 'fireKeyUp':
			(keyCode,) = params 
			e = wxKeyEvent(wxEVT_KEY_UP)
			e.m_keyCode = keyCode
			wxPostEvent(self.server.canvas, e)
			return 1

		# All the following are mouse events:
		try: x, y = params
		except: return 0
		x, y = x*self.server.canvas.GetSize()[0], y*self.server.canvas.GetSize()[1]
		x, y = int(x), int(y)

		if method == 'fireMotion':
			e = wxMouseEvent(wxEVT_MOTION)
			e.m_x, e.m_y = x, y
		# Left mouse button...	
		elif method == 'fireLeftDown':
			e = wxMouseEvent(wxEVT_LEFT_DOWN)
			e.m_x, e.m_y = x, y
		elif method == 'fireLeftUp':
			e = wxMouseEvent(wxEVT_LEFT_UP)
			e.m_x, e.m_y = x, y
		elif method == 'fireLeftDClick':
			e = wxMouseEvent(wxEVT_LEFT_DCLICK)
			e.m_x, e.m_y = x, y
		# Right mouse button...	
		elif method == 'fireRightDown':
			e = wxMouseEvent(wxEVT_RIGHT_DOWN)
			e.m_x, e.m_y = x, y
		elif method == 'fireRightUp':
			e = wxMouseEvent(wxEVT_RIGHT_UP)
			e.m_x, e.m_y = x, y
		elif method == 'fireRightDClick':
			e = wxMouseEvent(wxEVT_RIGHT_DCLICK)
			e.m_x, e.m_y = x, y
		# Middle mouse button...	
		elif method == 'fireMiddleDown':
			e = wxMouseEvent(wxEVT_MIDDLE_DOWN)
			e.m_x, e.m_y = x, y
		elif method == 'fireMiddleUp':
			e = wxMouseEvent(wxEVT_MIDDLE_UP)
			e.m_x, e.m_y = x, y
		elif method == 'fireMiddleDClick':
			e = wxMouseEvent(wxEVT_MIDDLE_DCLICK)
			e.m_x, e.m_y = x, y
		else:
			return 0 

		wxPostEvent(self.server.canvas, e)
		return 1 


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3