annot-server/annotserver.py
changeset 0 e1d4d7a8255a
child 2 fc1ab0074e29
equal deleted inserted replaced
-1:000000000000 0:e1d4d7a8255a
       
     1 
       
     2 from autobahn.twisted.resource import WebSocketResource, \
       
     3                                       WSGIRootResource, \
       
     4                                       HTTPChannelHixie76Aware
       
     5 
       
     6 
       
     7 from twisted.application import service, internet, strports
       
     8 from twisted.internet import reactor
       
     9 from twisted.python.threadpool import ThreadPool
       
    10 from twisted.web import server
       
    11 from twisted.web.wsgi import WSGIResource
       
    12 from twisted.web.static import Data
       
    13 
       
    14 from oscserver import OSCServerProtocol
       
    15 from websockets import BroadcastServerFactory, AnotationServerFactory
       
    16 from webapp import app
       
    17 
       
    18 
       
    19 WEB_PORT = 8080
       
    20 WS_PORT = 8090
       
    21 OSC_PORT = 9090
       
    22 
       
    23 def make_service(config, conn, debug=False):
       
    24 
       
    25     s = service.MultiService()
       
    26     # Create and start a thread pool,
       
    27     wsgiThreadPool = ThreadPool()
       
    28     wsgiThreadPool.start()
       
    29 
       
    30     # ensuring that it will be stopped when the reactor shuts down
       
    31     reactor.addSystemEventTrigger('after', 'shutdown', wsgiThreadPool.stop)
       
    32 
       
    33 
       
    34     wsFactory = BroadcastServerFactory("ws://localhost:%d/broadcast" % config.get('ws_port',WS_PORT),
       
    35                                        debug = debug,
       
    36                                        debugCodePaths = debug)
       
    37     wsFactory.setProtocolOptions(allowHixie76 = True)
       
    38     wsResource = WebSocketResource(wsFactory)
       
    39 
       
    40 
       
    41     wsAnnotFactory = AnotationServerFactory("ws://localhost:%d/annot" % config.get('ws_port',WS_PORT),
       
    42                                        debug = debug,
       
    43                                        debugCodePaths = debug,
       
    44                                        ws_factory = wsFactory,
       
    45                                        conn = conn)
       
    46     wsAnnotFactory.setProtocolOptions(allowHixie76 = True)
       
    47     wsAnnotResource = WebSocketResource(wsAnnotFactory)
       
    48 
       
    49 
       
    50     rootWs = Data("", "text/plain")
       
    51 
       
    52     rootWs.putChild("broadcast", wsResource)
       
    53     rootWs.putChild("annot", wsAnnotResource)
       
    54 
       
    55 
       
    56     # Create the WSGI resource
       
    57     wsgiAppAsResource = WSGIResource(reactor, wsgiThreadPool, app)
       
    58 
       
    59     # Hooks for twistd
       
    60 
       
    61     webserver = strports.service('tcp:%d' % config.get('web_port',WEB_PORT), server.Site(wsgiAppAsResource))
       
    62     webserver.setServiceParent(s)
       
    63 
       
    64     wsserver = strports.service('tcp:%d' % config.get('ws_port',WS_PORT), server.Site(rootWs))
       
    65     wsserver.setServiceParent(s)
       
    66 
       
    67     oscservice = internet.UDPServer(config.get('osc_port',OSC_PORT), OSCServerProtocol(wsFactory, conn))
       
    68     oscservice.setServiceParent(s)
       
    69 
       
    70     return s