Skip to content

Commit

Permalink
Merge pull request #216 from prabhuramachandran/improve-mayavi-viewer
Browse files Browse the repository at this point in the history
Improve mayavi viewer
  • Loading branch information
prabhuramachandran committed May 29, 2019
2 parents b0fd689 + 2699ef7 commit bb420dd
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 51 deletions.
47 changes: 29 additions & 18 deletions pysph/solver/solver_interfaces.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

import threading
import os
import socket
try:
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from SimpleXMLRPCServer import (SimpleXMLRPCServer,
SimpleXMLRPCRequestHandler)
from SimpleHTTPServer import SimpleHTTPRequestHandler
except ImportError:
# Python 3.x
Expand All @@ -13,13 +13,21 @@
from multiprocessing.managers import BaseManager, BaseProxy


def get_authkey_bytes(authkey):
if isinstance(authkey, bytes):
return authkey
else:
return authkey.encode('utf-8')


class MultiprocessingInterface(BaseManager):
""" A multiprocessing interface to the solver controller
This object exports a controller instance proxy over the multiprocessing
interface. Control actions can be performed by connecting to the interface
and calling methods on the controller proxy instance """
def __init__(self, address=None, authkey=None, try_next_port=False):
authkey = get_authkey_bytes(authkey)
BaseManager.__init__(self, address, authkey)
self.authkey = authkey
self.try_next_port = try_next_port
Expand All @@ -35,7 +43,7 @@ def start(self, controller):
host, port = self.address
while self.try_next_port:
try:
BaseManager.__init__(self, (host,port), self.authkey)
BaseManager.__init__(self, (host, port), self.authkey)
self.get_server().serve_forever()
self.try_next_port = False
except socket.error as e:
Expand All @@ -46,13 +54,16 @@ def start(self, controller):
else:
raise


class MultiprocessingClient(BaseManager):
""" A client for the multiprocessing interface
Override the run() method to do appropriate actions on the proxy
instance of the controller object or add an interface using the add_interface
methods similar to the Controller.add_interface method """
def __init__(self, address=None, authkey=None, serializer='pickle', start=True):
instance of the controller object or add an interface using the
add_interface methods similar to the Controller.add_interface method """
def __init__(self, address=None, authkey=None, serializer='pickle',
start=True):
authkey = get_authkey_bytes(authkey)
BaseManager.__init__(self, address, authkey, serializer)
if start:
self.start()
Expand Down Expand Up @@ -89,6 +100,7 @@ def add_interface(self, callable):
thr.start()
return thr


class CrossDomainXMLRPCRequestHandler(SimpleXMLRPCRequestHandler,
SimpleHTTPRequestHandler):
""" SimpleXMLRPCRequestHandler subclass which attempts to do CORS
Expand All @@ -102,14 +114,14 @@ def do_OPTIONS(self):
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-METHODS", "POST,GET,OPTIONS")
#self.send_header("Access-Control-Max-Age", "60")
# self.send_header("Access-Control-Max-Age", "60")
self.send_header("Content-length", "0")
self.end_headers()

def do_GET(self):
""" Handle http requests to serve html/image files only """
print(self.path, self.translate_path(self.path))
permitted_extensions = ['.html','.png','.svg','.jpg', '.js']
permitted_extensions = ['.html', '.png', '.svg', '.jpg', '.js']
if not os.path.splitext(self.path)[1] in permitted_extensions:
self.send_error(404, 'File Not Found/Allowed')
else:
Expand All @@ -122,6 +134,7 @@ def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
SimpleXMLRPCRequestHandler.end_headers(self)


class XMLRPCInterface(SimpleXMLRPCServer):
""" An XML-RPC interface to the solver controller
Expand All @@ -132,7 +145,7 @@ def __init__(self, addr, requestHandler=CrossDomainXMLRPCRequestHandler,
logRequests=True, allow_none=True,
encoding=None, bind_and_activate=True):
SimpleXMLRPCServer.__init__(self, addr, requestHandler, logRequests,
allow_none, encoding, bind_and_activate)
allow_none, encoding, bind_and_activate)

def start(self, controller):
self.register_instance(controller, allow_dotted_names=False)
Expand All @@ -146,9 +159,9 @@ def start(self, controller):
while True:
try:
try:
inp = raw_input('pysph[%d]>>> '%controller.get('count'))
inp = raw_input('pysph[%d]>>> ' % controller.get('count'))
except NameError:
inp = input('pysph[%d]>>> '%controller.get('count'))
inp = input('pysph[%d]>>> ' % controller.get('count'))
cmd = inp.strip().split()
try:
cmd, args = cmd[0], cmd[1:]
Expand All @@ -165,15 +178,15 @@ def start(self, controller):
finally:
args2.append(arg)

if cmd=='p' or cmd=='pause':
if cmd == 'p' or cmd == 'pause':
controller.pause_on_next()
elif cmd=='c' or cmd=='cont':
elif cmd == 'c' or cmd == 'cont':
controller.cont()
elif cmd=='g' or cmd=='get':
elif cmd == 'g' or cmd == 'get':
print(controller.get(args[0]))
elif cmd=='s' or cmd=='set':
elif cmd == 's' or cmd == 'set':
print(controller.set(args[0], args2[1]))
elif cmd=='q' or cmd=='quit':
elif cmd == 'q' or cmd == 'quit':
break
else:
print(getattr(controller, cmd)(*args2))
Expand All @@ -188,5 +201,3 @@ def help(self):
g | get <name>
s | set <name> <value>
q | quit -- quit commandline interface (solver keeps running)''')


0 comments on commit bb420dd

Please sign in to comment.