forked from cengwins/ahc_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
124 lines (92 loc) · 4.9 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
import os
import sys
import time, random, math
from enum import Enum
from pickle import FALSE
sys.path.insert(0, os.getcwd())
from ahc.Ahc import ComponentModel, Event, ConnectorTypes, Topology, EventTypes, GenericMessage, GenericMessageHeader, FramerObjects
from ahc.Ahc import ComponentRegistry
from ahc.PhysicalLayers.UsrpB210OfdmFlexFramePhy import UsrpB210OfdmFlexFramePhy
from ahc.MAC.CSMA import MacCsmaPPersistent,MacCsmaPPersistentConfigurationParameters
registry = ComponentRegistry()
from ahc.Channels.Channels import FIFOBroadcastPerfectChannel
from ahc.EttusUsrp.UhdUtils import AhcUhdUtils
framers = FramerObjects()
# define your own message types
class ApplicationLayerMessageTypes(Enum):
BROADCAST = "BROADCAST"
# define your own message header structure
class ApplicationLayerMessageHeader(GenericMessageHeader):
pass
class UsrpApplicationLayerEventTypes(Enum):
STARTBROADCAST = "startbroadcast"
class UsrpApplicationLayer(ComponentModel):
def on_init(self, eventobj: Event):
self.counter = 0
def __init__(self, componentname, componentid):
super().__init__(componentname, componentid)
self.eventhandlers[UsrpApplicationLayerEventTypes.STARTBROADCAST] = self.on_startbroadcast
def on_message_from_top(self, eventobj: Event):
# print(f"I am {self.componentname}.{self.componentinstancenumber},sending down eventcontent={eventobj.eventcontent}\n")
self.send_down(Event(self, EventTypes.MFRT, eventobj.eventcontent))
def on_message_from_bottom(self, eventobj: Event):
evt = Event(self, EventTypes.MFRT, eventobj.eventcontent)
print(f"I am Node.{self.componentinstancenumber}, received from Node.{eventobj.eventcontent.header.messagefrom} a message: {eventobj.eventcontent.payload}")
if self.componentinstancenumber == 1:
evt.eventcontent.header.messageto = 0
evt.eventcontent.header.messagefrom = 1
else:
evt.eventcontent.header.messageto = 1
evt.eventcontent.header.messagefrom = 0
evt.eventcontent.payload = eventobj.eventcontent.payload
#print(f"I am {self.componentname}.{self.componentinstancenumber}, sending down eventcontent={eventobj.eventcontent.payload}\n")
self.send_down(evt) # PINGPONG
def on_startbroadcast(self, eventobj: Event):
if self.componentinstancenumber == 1:
hdr = ApplicationLayerMessageHeader(ApplicationLayerMessageTypes.BROADCAST, 1, 0)
else:
hdr = ApplicationLayerMessageHeader(ApplicationLayerMessageTypes.BROADCAST, 0, 1)
self.counter = self.counter + 1
payload = "BMSG-" + str(self.counter)
broadcastmessage = GenericMessage(hdr, payload)
evt = Event(self, EventTypes.MFRT, broadcastmessage)
# time.sleep(3)
self.send_down(evt)
#print("Starting broadcast")
class UsrpNode(ComponentModel):
counter = 0
def on_init(self, eventobj: Event):
pass
def __init__(self, componentname, componentid):
# SUBCOMPONENTS
macconfig = MacCsmaPPersistentConfigurationParameters(0.5)
self.appl = UsrpApplicationLayer("UsrpApplicationLayer", componentid)
self.phy = UsrpB210OfdmFlexFramePhy("UsrpB210OfdmFlexFramePhy", componentid)
self.mac = MacCsmaPPersistent("MacCsmaPPersistent", componentid, configurationparameters=macconfig)
# CONNECTIONS AMONG SUBCOMPONENTS
self.appl.connect_me_to_component(ConnectorTypes.UP, self) #Not required if nodemodel will do nothing
self.appl.connect_me_to_component(ConnectorTypes.DOWN, self.mac)
self.mac.connect_me_to_component(ConnectorTypes.UP, self.appl)
self.mac.connect_me_to_component(ConnectorTypes.DOWN, self.phy)
# Connect the bottom component to the composite component....
self.phy.connect_me_to_component(ConnectorTypes.UP, self.mac)
self.phy.connect_me_to_component(ConnectorTypes.DOWN, self)
# self.phy.connect_me_to_component(ConnectorTypes.DOWN, self)
# self.connect_me_to_component(ConnectorTypes.DOWN, self.appl)
super().__init__(componentname, componentid)
def main():
topo = Topology()
# Note that the topology has to specific: usrp winslab_b210_0 is run by instance 0 of the component
# Therefore, the usrps have to have names winslab_b210_x where x \in (0 to nodecount-1)
topo.construct_winslab_topology_without_channels(4, UsrpNode)
# topo.construct_winslab_topology_with_channels(2, UsrpNode, FIFOBroadcastPerfectChannel)
# time.sleep(1)
# topo.nodes[0].send_self(Event(topo.nodes[0], UsrpNodeEventTypes.STARTBROADCAST, None))
topo.start()
i = 0
while(i < 10):
topo.nodes[1].appl.send_self(Event(topo.nodes[0], UsrpApplicationLayerEventTypes.STARTBROADCAST, None))
time.sleep(1)
i = i + 1
if __name__ == "__main__":
main()