/
main.py
executable file
·149 lines (117 loc) · 4.58 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python3
# -- Content-Encoding: UTF-8 --
"""
Chat entry point
"""
# Module version
__version_info__ = (0, 1, 0)
__version__ = ".".join(str(x) for x in __version_info__)
# Documentation strings format
__docformat__ = "restructuredtext en"
# -----------------------------------------------------------------------------
# Local
# import chat.constants
# Pelix
from pelix.ipopo.constants import use_ipopo
import pelix.framework
# Standard library
import argparse
import sys
# -----------------------------------------------------------------------------
def start_server(context):
"""
Starts the server component
"""
# Just start the server bundle
# context.install_bundle('chat.server').start()
context.install_bundle("time_provider").start()
def start_client(context, name):
"""
Starts a client with the given name
"""
# Start the client bundle
context.install_bundle('chat.client').start()
# Instantiate the client component
# with use_ipopo(context) as ipopo:
# ipopo.instantiate(chat.constants.FACTORY_CLIENT, "chat-client",
# {chat.constants.PROP_CLIENT_HANDLE: name})
# Bonus...
context.install_bundle("time_consumer").start()
def start_remote_services(context):
"""
Starts remote services components
"""
# Start bundles
for bundle in ("pelix.http.basic",
"pelix.remote.dispatcher",
"pelix.remote.registry",
"pelix.remote.discovery.multicast",
"experiment.disco_file",
"experiment.jabsorb",
"experiment.jabsorb_rpc",
"pelix.remote.json_rpc",
"experiment.mdnssd"):
context.install_bundle(bundle).start()
# Instantiate components
with use_ipopo(context) as ipopo:
# ... HTTP Service on a random port
ipopo.instantiate("pelix.http.service.basic.factory",
"pelix.http.service.basic",
{"pelix.http.port": 0})
# ... dispatcher servlet
ipopo.instantiate("pelix-remote-dispatcher-servlet-factory",
"pelix-remote-dispatcher-servlet", {})
# ... multicast discovery
# ipopo.instantiate("pelix-remote-discovery-multicast-factory",
# "pelix-remote-discovery-multicast", {})
# ... mDNS discovery
ipopo.instantiate("experiment-zeroconf-discovery-factory",
"experiment-zeroconf-discovery", {})
# ... JSON-RPC exporter and importer
# ipopo.instantiate("pelix-jsonrpc-exporter-factory",
# "pelix-jsonrpc-exporter", {})
# ipopo.instantiate("pelix-jsonrpc-importer-factory",
# "pelix-jsonrpc-importer", {})
# ... JABSORB-RPC exporter and importer
ipopo.instantiate("cohorte-jabsorbrpc-exporter-factory",
"cohorte-jabsorbrpc-exporter", {})
ipopo.instantiate("cohorte-jabsorbrpc-importer-factory",
"cohorte-jabsorbrpc-importer", {})
# -----------------------------------------------------------------------------
def main(args):
"""
Entry point
"""
# Parse arguments
parser = argparse.ArgumentParser(
description="Remote Services demonstration: Chat")
parser.add_argument("--server", action="store_true", dest="server",
help="Run in server mode")
parser.add_argument("--name", action="store", dest="name",
help="Set the client name")
args = parser.parse_args(args)
# Start the framework
framework = pelix.framework.create_framework(("pelix.ipopo.core",
"pelix.shell.core",
"pelix.shell.ipopo",
"pelix.shell.console",
"chat.constants"))
framework.start()
context = framework.get_bundle_context()
# Start remote services
start_remote_services(context)
# Start the server
if args.server:
start_server(context)
else:
# Start the client
start_client(context, args.name)
# Wait for the framework to stop
framework.wait_for_stop()
if __name__ == "__main__":
# Script call
import logging
logging.basicConfig(level=logging.DEBUG)
# Avoid to be drowned in PyZeroconf traces
logging.getLogger("zeroconf").setLevel(logging.WARNING)
main(sys.argv[1:])