fb11e647
vrobic
reseau statique a...
|
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
150
151
152
153
154
155
156
157
|
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Philipp Rosenkranz <philipp.rosenkranz@fu-berlin.de>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
import sys, signal, threading
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, Factory
class PubProtocol(Protocol):
def __init__(self, factory):
self.factory = factory
def connectionMade(self):
print("new connection made")
def connectionLost(self, reason):
self.factory.numProtocols = self.factory.numProtocols - 1
def connectionLost(self, reason):
self.factory.clients = {key: value for key, value in self.factory.clients.items()
if value is not self.transport}
def dataReceived(self, data):
if data.startswith("hostname: "):
remoteHostname = data.split()[1].strip()
print("dataReceived added: " + remoteHostname)
self.factory.clients[remoteHostname] = self.transport
else:
print("received some useless data...")
class PubFactory(Factory):
def __init__(self):
self.clients = dict()
def buildProtocol(self, addr):
return PubProtocol(self)
class ExperimentRunner():
def __init__(self, experiment, testbed):
self.publisher = PubFactory()
self.port = int(testbed.serverPort)
self.experiment = experiment(self.publisher, self)
self.testbed = testbed
def run(self):
signal.signal(signal.SIGINT, self.handle_sigint)
self.experiment.run()
reactor.listenTCP(self.port, self.publisher)
# clean logfiles and start nodes but don't flash nodes
self.testbed.initClean()
reactor.run()
def stop(self):
self.testbed.stop()
reactor.callFromThread(self.safeReactorStop)
def safeReactorStop(self):
if reactor.running:
try:
reactor.stop()
except:
print("tried to shutdown reactor twice!")
def handle_sigint(self, signal, frame):
self.experiment.stop()
self.testbed.stop()
self.stop() # shutdown if experiment didn't already
class Experiment():
def __init__(self, factory, runner):
self.factory = factory
self.runner = runner
self.hostid = dict()
self.sumDelay = 0
def run(self):
print("Running preHook")
self.preHook()
print("Running experiment")
self.start()
def start(self):
raise NotImplementedError("Inherit from Experiment and implement start")
def stop(self):
print("Running postHook")
self.postHook()
self.runner.stop()
def preHook(self):
pass
def postHook(self):
pass
def readHostFile(self, path):
id = 1
with open(path) as f:
for line in f:
self.hostid[line.strip()] = id
id += 1
def sendToHost(self, host=None, cmd=""):
if host in self.factory.clients:
self.factory.clients[host].write(cmd + "\n")
else:
print("sendToHost: no such host known: " + host + " !")
def sendToAll(self, cmd=""):
for host, transport in self.factory.clients.items():
self.sendToHost(host, cmd)
def pauseInSeconds(self, seconds=0):
from time import time, sleep
start = time()
while (time() - start < seconds):
sleep(seconds - (time() - start))
def callLater(self, absoluteDelay = 0.0, function = None):
reactor.callLater(absoluteDelay, function)
def waitAndCall(self, relativeDelay = 0.0, function = None):
self.sumDelay += relativeDelay
self.callLater(self.sumDelay, function)
def clientIterator(self):
return self.factory.clients.items()
def connectionByHostname(self, host=None):
if host in self.factory.clients:
return self.factory.clients[host]
@staticmethod
def sendToConnection(connection, line):
connection.write(line + "\n")
|