comparison sat_pubsub/tap_http.py @ 232:923281d4c5bc

renamed idavoll directory to sat_pubsub
author Goffi <goffi@goffi.org>
date Thu, 17 May 2012 12:48:14 +0200
parents idavoll/tap_http.py@a321f9300054
children 564ae55219e1
comparison
equal deleted inserted replaced
231:d99047cd90f9 232:923281d4c5bc
1 # Copyright (c) 2003-2008 Ralph Meijer
2 # See LICENSE for details.
3
4 from twisted.application import internet, service, strports
5 from twisted.conch import manhole, manhole_ssh
6 from twisted.cred import portal, checkers
7 from twisted.web2 import channel, log, resource, server
8 from twisted.web2.tap import Web2Service
9
10 from idavoll import gateway, tap
11 from idavoll.gateway import RemoteSubscriptionService
12
13 class Options(tap.Options):
14 optParameters = [
15 ('webport', None, '8086', 'Web port'),
16 ]
17
18
19
20 def getManholeFactory(namespace, **passwords):
21 def getManHole(_):
22 return manhole.Manhole(namespace)
23
24 realm = manhole_ssh.TerminalRealm()
25 realm.chainedProtocolFactory.protocolFactory = getManHole
26 p = portal.Portal(realm)
27 p.registerChecker(
28 checkers.InMemoryUsernamePasswordDatabaseDontUse(**passwords))
29 f = manhole_ssh.ConchFactory(p)
30 return f
31
32
33
34 def makeService(config):
35 s = tap.makeService(config)
36
37 bs = s.getServiceNamed('backend')
38 cs = s.getServiceNamed('component')
39
40 # Set up XMPP service for subscribing to remote nodes
41
42 if config['backend'] == 'pgsql':
43 from idavoll.pgsql_storage import GatewayStorage
44 gst = GatewayStorage(bs.storage.dbpool)
45 elif config['backend'] == 'memory':
46 from idavoll.memory_storage import GatewayStorage
47 gst = GatewayStorage()
48
49 ss = RemoteSubscriptionService(config['jid'], gst)
50 ss.setHandlerParent(cs)
51 ss.startService()
52
53 # Set up web service
54
55 root = resource.Resource()
56
57 # Set up resources that exposes the backend
58 root.child_create = gateway.CreateResource(bs, config['jid'],
59 config['jid'])
60 root.child_delete = gateway.DeleteResource(bs, config['jid'],
61 config['jid'])
62 root.child_publish = gateway.PublishResource(bs, config['jid'],
63 config['jid'])
64 root.child_list = gateway.ListResource(bs)
65
66 # Set up resources for accessing remote pubsub nodes.
67 root.child_subscribe = gateway.RemoteSubscribeResource(ss)
68 root.child_unsubscribe = gateway.RemoteUnsubscribeResource(ss)
69 root.child_items = gateway.RemoteItemsResource(ss)
70
71 if config["verbose"]:
72 root = log.LogWrapperResource(root)
73
74 site = server.Site(root)
75 w = internet.TCPServer(int(config['webport']), channel.HTTPFactory(site))
76
77 if config["verbose"]:
78 logObserver = log.DefaultCommonAccessLoggingObserver()
79 w2s = Web2Service(logObserver)
80 w.setServiceParent(w2s)
81 w = w2s
82
83 w.setServiceParent(s)
84
85 # Set up a manhole
86
87 namespace = {'service': s,
88 'component': cs,
89 'backend': bs,
90 'root': root}
91
92 f = getManholeFactory(namespace, admin='admin')
93 manholeService = strports.service('2222', f)
94 manholeService.setServiceParent(s)
95
96 return s
97