Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / howto / listings / TwistedQuotes / quotetap2.py
1 from TwistedQuotes import quoteproto    # Protocol and Factory
2 from TwistedQuotes import quoters       # "give me a quote" code
3 from TwistedQuotes import pbquote       # perspective broker binding
4         
5 from twisted.application import service, internet
6 from twisted.python import usage        # twisted command-line processing
7 from twisted.spread import pb           # Perspective Broker
8
9 class Options(usage.Options):
10     optParameters = [["port", "p", 8007,
11                       "Port number to listen on for QOTD protocol."],
12                      ["static", "s", "An apple a day keeps the doctor away.",
13                       "A static quote to display."],
14                      ["file", "f", None,
15                       "A fortune-format text file to read quotes from."],
16                      ["pb", "b", None,
17                       "Port to listen with PB server"]]
18
19 def makeService(config):
20     svc = service.MultiService()
21     if config["file"]:                  # If I was given a "file" option...
22         # Read quotes from a file, selecting a random one each time,
23         quoter = quoters.FortuneQuoter([config['file']])
24     else:                               # otherwise,
25         # read a single quote from the command line (or use the default).
26         quoter = quoters.StaticQuoter(config['static'])
27     port = int(config["port"])          # TCP port to listen on
28     factory = quoteproto.QOTDFactory(quoter) # here we create a QOTDFactory
29     # Finally, set up our factory, with its custom quoter, to create QOTD
30     # protocol instances when events arrive on the specified port.
31     pbport = config['pb']               # TCP PB port to listen on
32     if pbport:
33         pbfact = pb.PBServerFactory(pbquote.QuoteReader(quoter))
34         svc.addService(internet.TCPServer(int(pbport), pbfact))
35     svc.addService(internet.TCPServer(port, factory))
36     return svc