~ubuntu-branches/ubuntu/precise/fibranet/precise

« back to all changes in this revision

Viewing changes to sandbox/server.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2006-10-27 01:02:56 UTC
  • Revision ID: james.westby@ubuntu.com-20061027010256-n265y24bk98s9lbe
Tags: upstream-10
ImportĀ upstreamĀ versionĀ 10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import pygame
 
2
import nanothreads
 
3
import nanotubes
 
4
import time
 
5
import weakref
 
6
import random
 
7
 
 
8
 
 
9
class PygameListener(nanotubes.Listener):
 
10
    """
 
11
    A custom listener which tracks connections in a list. Connected sockets
 
12
    are available via the connections attribute, and use the MessageProtocol
 
13
    to communicate.
 
14
    """
 
15
    def __init__(self, (host, port)):
 
16
        nanotubes.Listener.__init__(self, (host, port), nanotubes.MessageProtocol)
 
17
        self.connections = []
 
18
        
 
19
    def broadcast(self, name, **kw):
 
20
        """
 
21
        Broadcast a message to all connected sockets.
 
22
        """
 
23
        for conn_ref in self.connections:
 
24
            conn_ref().protocol.post(name, **kw)
 
25
    
 
26
    def cleanup(self):
 
27
        """
 
28
        Remove dead connections.
 
29
        """
 
30
        connections = []
 
31
        for conn_ref in self.connections:
 
32
            if conn_ref() is not None:
 
33
                connections.append(conn_ref)
 
34
        self.connections[:] = connections
 
35
        
 
36
    def handle_new(self, conn):
 
37
        self.connections.append(weakref.ref(conn))
 
38
        
 
39
 
 
40
def main():
 
41
    pygame.init()
 
42
    #start a server listening for connections on port 1984
 
43
    server = PygameListener(('10.1.1.55',1984))
 
44
    screen = pygame.display.set_mode((320,200))
 
45
 
 
46
    while True:
 
47
        time.sleep(0.01)
 
48
        #the SocketHandler must be polled regularly.
 
49
        nanotubes.SocketHandler.poll()
 
50
        for event in pygame.event.get():
 
51
            if event.type == pygame.QUIT:
 
52
                raise SystemExit
 
53
            elif event.type == pygame.MOUSEMOTION:
 
54
                #broadcast MOUSEMOTION events to all connected clients.
 
55
                server.broadcast(event.type, **event.dict)
 
56
        
 
57
        for conn_ref in server.connections:
 
58
            #get a reference to the connection
 
59
            conn = conn_ref()
 
60
            #if the reference is dead, ignore it
 
61
            if conn is None: continue
 
62
            #recieved events are stored in .protocol.messages
 
63
            for event_type, args in conn.protocol.messages:
 
64
                #if recieved event is pygame.MOUSEMOTION, draw a pixel
 
65
                if event_type == pygame.MOUSEMOTION:
 
66
                    screen.set_at(args['pos'], (255,255,255))
 
67
                #remove messages from the connections queue.
 
68
                conn.protocol.messages[:] = []
 
69
        pygame.display.flip()
 
70
        #cleanup and remove dead connections.
 
71
        server.cleanup()
 
72
        yield None
 
73
        
 
74
 
 
75
nanothreads.install(main())
 
76
nanothreads.loop()