| 1 |
|
|---|
| 2 |
import sys |
|---|
| 3 |
import os |
|---|
| 4 |
import socket |
|---|
| 5 |
from optparse import OptionParser |
|---|
| 6 |
|
|---|
| 7 |
from Axon.Scheduler import scheduler |
|---|
| 8 |
from Axon.AxonExceptions import MultipleServiceDeletion |
|---|
| 9 |
|
|---|
| 10 |
from cherrypy.process import bus |
|---|
| 11 |
from cherrypy.process import plugins, servers |
|---|
| 12 |
|
|---|
| 13 |
from microblog.jabber.client import Client |
|---|
| 14 |
from microblog.jabber.atomhandler import FeedReaderComponent |
|---|
| 15 |
from microblog.jabber.monitor import HTTPResourceMonitor |
|---|
| 16 |
from microblog.jabber.profile import ProfileHandler, NewProfileHandler |
|---|
| 17 |
from microblog.atompub.application import AtomPubApplication |
|---|
| 18 |
from microblog.profile.manager import ProfileManager |
|---|
| 19 |
|
|---|
| 20 |
base_dir = os.getcwd() |
|---|
| 21 |
|
|---|
| 22 |
def parse_commandline(): |
|---|
| 23 |
from optparse import OptionParser |
|---|
| 24 |
parser = OptionParser() |
|---|
| 25 |
parser.add_option("-d", "--xmpp-domain", dest="domain", |
|---|
| 26 |
help="XMPP server domain (default: localhost)") |
|---|
| 27 |
parser.set_defaults(domain='localhost') |
|---|
| 28 |
parser.add_option("-a", "--address", dest="address", action="store", |
|---|
| 29 |
help="XMPP server address (default: localhost:5222) ") |
|---|
| 30 |
parser.set_defaults(address='localhost:5222') |
|---|
| 31 |
(options, args) = parser.parse_args() |
|---|
| 32 |
|
|---|
| 33 |
return options |
|---|
| 34 |
|
|---|
| 35 |
class Server(object): |
|---|
| 36 |
def __init__(self): |
|---|
| 37 |
self.running = True |
|---|
| 38 |
self.options = parse_commandline() |
|---|
| 39 |
|
|---|
| 40 |
host, port = self.options.address.split(':') |
|---|
| 41 |
Client.Host = unicode(host) |
|---|
| 42 |
Client.Port = int(port) |
|---|
| 43 |
Client.Domain = unicode(self.options.domain) |
|---|
| 44 |
|
|---|
| 45 |
self.atompub = AtomPubApplication(base_dir) |
|---|
| 46 |
|
|---|
| 47 |
def start(self): |
|---|
| 48 |
httpMonitor = HTTPResourceMonitor(30.0) |
|---|
| 49 |
httpMonitor.activate() |
|---|
| 50 |
|
|---|
| 51 |
newProfileFeedReader = FeedReaderComponent() |
|---|
| 52 |
newProfileFeedReader.activate() |
|---|
| 53 |
|
|---|
| 54 |
newProfileHandler = NewProfileHandler(base_dir, self.atompub) |
|---|
| 55 |
newProfileHandler.link((newProfileFeedReader, 'outbox'), (newProfileHandler, 'inbox')) |
|---|
| 56 |
newProfileHandler.activate() |
|---|
| 57 |
|
|---|
| 58 |
from Kamaelia.Util.OneShot import OneShot |
|---|
| 59 |
shot = OneShot() |
|---|
| 60 |
httpMonitor.link((shot, 'outbox'), (httpMonitor, 'monitor')) |
|---|
| 61 |
shot.send(('http://localhost:8080/profile/new/feed', newProfileFeedReader)) |
|---|
| 62 |
|
|---|
| 63 |
profileFeedReader = FeedReaderComponent() |
|---|
| 64 |
profileFeedReader.activate() |
|---|
| 65 |
|
|---|
| 66 |
profileHandler = ProfileHandler(base_dir, self.atompub) |
|---|
| 67 |
profileFeedReader.link((profileFeedReader, 'outbox'), (profileHandler, 'inbox')) |
|---|
| 68 |
profileHandler.activate() |
|---|
| 69 |
|
|---|
| 70 |
from Kamaelia.Util.OneShot import OneShot |
|---|
| 71 |
shot = OneShot() |
|---|
| 72 |
httpMonitor.link((shot, 'outbox'), (httpMonitor, 'monitor')) |
|---|
| 73 |
shot.send(('http://localhost:8080/profile/feed', profileFeedReader)) |
|---|
| 74 |
|
|---|
| 75 |
self.running = True |
|---|
| 76 |
|
|---|
| 77 |
start.priority = 90 |
|---|
| 78 |
|
|---|
| 79 |
def stop(self): |
|---|
| 80 |
for client in Client.Sessions: |
|---|
| 81 |
Client.Sessions[client].shutdown() |
|---|
| 82 |
Client.Sessions.clear() |
|---|
| 83 |
self.running = False |
|---|
| 84 |
|
|---|
| 85 |
def exit(self): |
|---|
| 86 |
self.stop() |
|---|
| 87 |
|
|---|
| 88 |
def run_components(self): |
|---|
| 89 |
while self.running: |
|---|
| 90 |
try: |
|---|
| 91 |
for i in scheduler.run.main(0.02, canblock=True): |
|---|
| 92 |
if not self.running: |
|---|
| 93 |
break |
|---|
| 94 |
except KeyError, ke: |
|---|
| 95 |
print ke |
|---|
| 96 |
pass |
|---|
| 97 |
except MultipleServiceDeletion, ex: |
|---|
| 98 |
print ex |
|---|
| 99 |
|
|---|
| 100 |
def setup(server): |
|---|
| 101 |
import cherrypy |
|---|
| 102 |
cherrypy.server.unsubscribe() |
|---|
| 103 |
plugins.SignalHandler(bus) |
|---|
| 104 |
bus.subscribe('start', server.start) |
|---|
| 105 |
bus.subscribe('graceful', server.stop) |
|---|
| 106 |
bus.subscribe('exit', server.exit) |
|---|
| 107 |
|
|---|
| 108 |
def serve(s): |
|---|
| 109 |
bus.start_with_callback(s.run_components) |
|---|
| 110 |
bus.block() |
|---|
| 111 |
|
|---|
| 112 |
if __name__ == '__main__': |
|---|
| 113 |
s = Server() |
|---|
| 114 |
setup(s) |
|---|
| 115 |
serve(s) |
|---|