-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
136 lines (110 loc) · 3.95 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
# A program that implements a content centric (CCN) node. It
# has a 3-layer protocol architecture, where different modules can
# be configured and loaded to serve different supported specific
# layer supporting modules.
#
# - app - application layer
# - ccn - forwarding layer
# - link - link layer
#
# All layer modules and the parameters have to be defined in the
# lib/settings.py module. Depending on the node type, the settings.py
# must be created by copying the appropriate file as follows.
#
# - settings.node.py
# for an internet based CCN node
# - settings.sensor.py
# for a CCN based sensor node
# - settings.gateway.py
# for a CCN based IoT gateway conntecting a sensor network and the internet
#
# @author: Asanga Udugama (adu@comnets.uni-bremen.de)
# @date: 22-jun-2023
#
import sys
sys.path.append('./lib')
from threading import Thread
from threading import Lock
import settings
import common
import time
# import module references
appmodules = []
ccnmodule = None
linkmodules = []
def main():
global appmodules
global ccnmodule
global linkmodules
# setup the environment
try:
# initialize and setup basic environment
common.setup()
# log
logmsg = 'main : Initialization started...'
with common.system_lock:
common.log_activity(logmsg)
# load & call setup of forwarding module
ccn = __import__(settings.CCN_LAYER)
handler = ccn.setup(dispatch)
# save references for later use
modinfo = common.ModuleInfo()
modinfo.module_name = settings.CCN_LAYER
modinfo.module_ref = ccn
modinfo.handler_ref = handler
ccnmodule = modinfo
# load & setup each link module
for module in settings.LINK_LAYER:
# import link module
link = __import__(module)
# call setup to intialize and start threads
handler = link.setup(dispatch)
# save references for later use
modinfo = common.ModuleInfo()
modinfo.module_name = module
modinfo.module_ref = link
modinfo.handler_ref = handler
linkmodules.append(modinfo)
# load and setup each application module
for module in settings.APP_LAYER:
# import link module
app = __import__(module)
# call setup to intialize and start threads
handler = app.setup(dispatch)
# save references for later use
modinfo = common.ModuleInfo()
modinfo.module_name = module
modinfo.module_ref = app
modinfo.handler_ref = handler
appmodules.append(modinfo)
# log
logmsg = 'main : Initialization completed'
with common.system_lock:
common.log_activity(logmsg)
# wait endlessly while the rest of the threads do their work
while True:
# loop with a pause
time.sleep(5)
except Exception as e:
print(e)
# All layer handling modules (e.g. eth, tempreader, stdccn) will call
# dispatch to deliver to their packets to other layer handling
# modules. The encap must carry information about where to deliver.
def dispatch(encap):
# CCN module destined packet
if encap.to_direction == common.DirectionType.TO_CCN:
ccnmodule.handler_ref.handle_msg(encap)
# application module destined packet, find from list
elif encap.to_direction == common.DirectionType.TO_APP:
for modinfo in appmodules:
if encap.to_module_name == modinfo.module_name:
modinfo.handler_ref.handle_msg(encap)
break
# link module destined packet, find from list
elif encap.to_direction == common.DirectionType.TO_LINK:
for modinfo in linkmodules:
if encap.to_module_name == modinfo.module_name:
modinfo.handler_ref.handle_msg(encap)
break
if __name__ == "__main__":
main()