[shpy-commit] r2829 - in shpy/trunk/dist/shpy: . net
arigo@codespeak.net
arigo@codespeak.net
Sun, 18 Jan 2004 22:54:17 +0100 (MET)
Author: arigo
Date: Sun Jan 18 22:54:17 2004
New Revision: 2829
Modified:
shpy/trunk/dist/shpy/net/gateway.py
shpy/trunk/dist/shpy/net/register.py
shpy/trunk/dist/shpy/net/xx.py
shpy/trunk/dist/shpy/ui_pygame.py
Log:
Wohow, two clients displaying the same cursor moving around :-)
Modified: shpy/trunk/dist/shpy/net/gateway.py
==============================================================================
--- shpy/trunk/dist/shpy/net/gateway.py (original)
+++ shpy/trunk/dist/shpy/net/gateway.py Sun Jan 18 22:54:17 2004
@@ -6,7 +6,11 @@
from shpy.net import common
class SocketGateway:
- def __init__(self, sock):
+ def __init__(self, sock, ns = None):
+ if ns is None:
+ ns = {}
+ ns['gateway'] = self
+ self.ns = ns
self.sock = sock
self.incoming = Queue.Queue()
self.outgoing = Queue.Queue()
@@ -58,13 +62,12 @@
self.queue = queue
def run(self):
- ns = { 'clientgw' : self.gateway}
while self.running:
source = self.queue.get()
try:
print "executing source", source[:10]
co = dyncode.makecode(source)
- exec co in ns
+ exec co in self.gateway.ns
except:
import traceback, sys
l = traceback.format_exception(*sys.exc_info())
Modified: shpy/trunk/dist/shpy/net/register.py
==============================================================================
--- shpy/trunk/dist/shpy/net/register.py (original)
+++ shpy/trunk/dist/shpy/net/register.py Sun Jan 18 22:54:17 2004
@@ -1,40 +1,50 @@
import autopath, os
+import inspect, socket, pickle
+from shpy.net import gateway
-def register(hostport = ':8888', share_ns = 'share_ns'):
- if isinstance(hostport, str):
- host, port = hostport.split(':')
- hostport = (host, int(port))
-
- import inspect, socket, pickle
- from shpy.net import gateway
-
- source = [inspect.getsource(gateway)]
- source.append('thread = SocketGateway(clientsock)')
- source.append('pickler.dump("ok")')
- source = "\n".join(source)
-
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.connect(hostport)
- sockfile = sock.makefile('r+b',0)
- p = pickle.Pickler(sockfile)
- p.dump(source)
- u = pickle.Unpickler(sockfile)
- res = u.load()
- assert res=='ok', ("could not establish server gateway %r" %
- hostport)
-
- modsource = open(os.path.join(autopath.thisdir, 'shared.py'), 'r').read()
- gw = gateway.SocketGateway(sock)
- gw.exec_remote("""
- try:
- import %(share_ns)s as shared
- except ImportError:
- import new, sys
- shared = new.module(%(share_ns)r)
- exec %(modsource)r in vars(shared)
- sys.modules[%(share_ns)r] = shared
- shared.registerclient(clientgw)
-
- """ % locals())
- return gw
+
+class ServerGateway(gateway.SocketGateway):
+
+ def __init__(self, hostport = ':8888', ns = None):
+ if isinstance(hostport, str):
+ host, port = hostport.split(':')
+ hostport = (host, int(port))
+
+ source = [inspect.getsource(gateway)]
+ source.append('thread = SocketGateway(clientsock)')
+ source.append('pickler.dump("ok")')
+ source = "\n".join(source)
+
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.connect(hostport)
+ sockfile = sock.makefile('r+b',0)
+ p = pickle.Pickler(sockfile)
+ p.dump(source)
+ u = pickle.Unpickler(sockfile)
+ res = u.load()
+ assert res=='ok', ("could not establish server gateway %r" %
+ hostport)
+ gateway.SocketGateway.__init__(self, sock, ns)
+
+ def import_remote(self, filename, modname, reload=False):
+ filename = os.path.join(autopath.thisdir, filename)
+ modsource = open(filename, 'r').read()
+ l = []
+ if not reload:
+ l.append("""
+ try:
+ import %(modname)s
+ except ImportError:
+ """)
+ l.append("""
+ import new, sys
+ %(modname)s = new.module(%(modname)r)
+ exec %(modsource)r in vars(%(modname)s)
+ sys.modules[%(modname)r] = %(modname)s
+ """)
+ self.exec_remote("".join(l) % locals())
+
+ def registerclient(self):
+ self.import_remote('shared.py', 'shared')
+ self.exec_remote("shared.registerclient(gateway)")
Modified: shpy/trunk/dist/shpy/net/xx.py
==============================================================================
--- shpy/trunk/dist/shpy/net/xx.py (original)
+++ shpy/trunk/dist/shpy/net/xx.py Sun Jan 18 22:54:17 2004
@@ -2,4 +2,4 @@
rlcompleter2.setup()
import register
-gw = register.register()
+gw = register.ServerGateway()
Modified: shpy/trunk/dist/shpy/ui_pygame.py
==============================================================================
--- shpy/trunk/dist/shpy/ui_pygame.py (original)
+++ shpy/trunk/dist/shpy/ui_pygame.py Sun Jan 18 22:54:17 2004
@@ -1,5 +1,7 @@
+import sys
import pygame
from pygame.locals import *
+import net.register
#RESOLUTION = (800, 600)
RESOLUTION = (400, 300)
@@ -7,6 +9,8 @@
FONT = None
HEIGHT = 24
+REPAINTEVENT = USEREVENT
+
class InputCell:
@@ -89,13 +93,16 @@
class Terminal:
- def __init__(self):
+ def __init__(self, hostport):
+ ns = {'terminal': self}
+ self.servergateway = net.register.ServerGateway(hostport, ns)
pygame.init()
self.screen = pygame.display.set_mode(RESOLUTION)
self.font = pygame.font.Font(FONT, HEIGHT)
self.cell = InputCell()
self.cursor = Cursor()
self.cursor.entercell(self.cell, (10, 10))
+ self.servergateway.registerclient()
def repaint(self):
self.screen.fill((255,255,255))
@@ -118,12 +125,30 @@
method = getattr(receiver, keyname, None)
if method:
method()
+ self.sendcursorupdate()
invalid = True
break
if event.type == QUIT:
break
+ if event.type == REPAINTEVENT:
+ invalid = True
+ def postrepaintevent(self):
+ pygame.event.post(pygame.event.Event(REPAINTEVENT))
-if __name__ == '__main__':
- Terminal().run()
+ def sendcursorupdate(self):
+ self.servergateway.exec_remote('''
+ for c in shared.getclientlist():
+ if c is not gateway:
+ c.exec_remote("terminal.setcursorto(%(x)d, %(y)d)")
+
+ ''' % {'x': self.cursor.x, 'y': self.cursor.y})
+
+ def setcursorto(self, x, y):
+ self.cursor.x = x
+ self.cursor.y = y
+ self.postrepaintevent()
+
+if __name__ == '__main__':
+ Terminal(sys.argv[1]).run()