Skip to content

Commit

Permalink
Replaced the file based server with a SocketServer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Stoppani authored and Jonathan Stoppani committed Aug 2, 2010
1 parent 5d2f2b9 commit af920b3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
7 changes: 4 additions & 3 deletions ernie/__init__.py
Expand Up @@ -2,11 +2,12 @@

__version__ = "0.0.1"

import SocketServer
from ernie import Ernie

def mod(name):
return Ernie.mod(name)

def start():
server = Ernie()
server.start()
def start(host='localhost', port=9999):
Ernie.log("Starting")
SocketServer.TCPServer((host, port), Ernie).serve_forever()
77 changes: 39 additions & 38 deletions ernie/ernie.py
Expand Up @@ -2,8 +2,10 @@
import logging
import bert
import struct
import SocketServer

class Ernie(object):

class Ernie(SocketServer.StreamRequestHandler):
mods = {}
logger = None

Expand Down Expand Up @@ -49,46 +51,45 @@ def write_berp(self, output, obj):
output.write(data)
output.flush()

def start(self):
Ernie.log("Starting")
input = os.fdopen(3)
output = os.fdopen(4, "w")
def handle(self):
input = self.rfile
output = self.wfile

ipy = self.read_berp(input)
if ipy == None:
print 'Could not read BERP length header. Ernie server may have gone away. Exiting now.'
exit()

while(True):
ipy = self.read_berp(input)
if ipy == None:
print 'Could not read BERP length header. Ernie server may have gone away. Exiting now.'
exit()

if len(ipy) == 4 and ipy[0] == bert.Atom('call'):
mod, fun, args = ipy[1:4]
self.log("-> " + ipy.__str__())
try:
res = self.dispatch(mod, fun, args)
opy = (bert.Atom('reply'), res)
self.log("<- " + opy.__str__())
self.write_berp(output, opy)
except ServerError, e:
opy = (bert.Atom('error'), (bert.Atom('server'), 0, str(type(e)), str(e), ''))
self.log("<- " + opy.__str__())
self.write_berp(output, opy)
except Exception, e:
opy = (bert.Atom('error'), (bert.Atom('user'), 0, str(type(e)), str(e), ''))
self.log("<- " + opy.__str__())
self.write_berp(output, opy)
elif len(ipy) == 4 and ipy[0] == bert.Atom('cast'):
mod, fun, args = ipy[1:4]
self.log("-> " + ipy.__str__())
try:
res = self.dispatch(mod, fun, args)
except:
pass
self.write_berp(output, (bert.Atom('noreply')))
else:
self.log("-> " + ipy.__str__())
opy = (bert.Atom('error'), (bert.Atom('server'), 0, "Invalid request: " + ipy.__str__()))
if len(ipy) == 4 and ipy[0] == bert.Atom('call'):
mod, fun, args = ipy[1:4]
self.log("-> " + ipy.__str__())
try:
res = self.dispatch(mod, fun, args)
opy = (bert.Atom('reply'), res)
self.log("<- " + opy.__str__())
self.write_berp(output, opy)
except ServerError, e:
opy = (bert.Atom('error'), (bert.Atom('server'), 0, str(type(e)), str(e), ''))
self.log("<- " + opy.__str__())
self.write_berp(output, opy)
except Exception, e:
opy = (bert.Atom('error'), (bert.Atom('user'), 0, str(type(e)), str(e), ''))
self.log("<- " + opy.__str__())
self.write_berp(output, opy)
elif len(ipy) == 4 and ipy[0] == bert.Atom('cast'):
mod, fun, args = ipy[1:4]
self.log("-> " + ipy.__str__())
try:
res = self.dispatch(mod, fun, args)
except:
pass
self.write_berp(output, (bert.Atom('noreply')))
else:
self.log("-> " + ipy.__str__())
opy = (bert.Atom('error'), (bert.Atom('server'), 0, "Invalid request: " + ipy.__str__()))
self.log("<- " + opy.__str__())
self.write_berp(output, opy)


class ServerError(Exception):
def __init__(self, value):
Expand Down

0 comments on commit af920b3

Please sign in to comment.