From 66b02591136c7110c5f8c6a85d93455ce7399158 Mon Sep 17 00:00:00 2001 From: Alina Quereilhac Date: Mon, 10 Feb 2014 22:43:10 +0100 Subject: [PATCH] ns-3 test passing --- src/nepi/resources/linux/ns3/ns3client.py | 55 +++++++++++-------- src/nepi/resources/linux/ns3/ns3simulation.py | 29 ++-------- src/nepi/resources/ns3/ns3client.py | 20 +++---- src/nepi/resources/ns3/ns3simulation.py | 40 +++++++------- src/nepi/resources/ns3/ns3wrapper.py | 5 +- test/resources/linux/ns3/ns3simulation.py | 39 ++----------- 6 files changed, 72 insertions(+), 116 deletions(-) diff --git a/src/nepi/resources/linux/ns3/ns3client.py b/src/nepi/resources/linux/ns3/ns3client.py index 76a37bf8..2263197d 100644 --- a/src/nepi/resources/linux/ns3/ns3client.py +++ b/src/nepi/resources/linux/ns3/ns3client.py @@ -48,12 +48,16 @@ class LinuxNS3Client(NS3Client): self.simulation.local_socket, self.simulation.remote_socket) - def send_msg(self, msg, *args): + def send_msg(self, msg, *args, **kwargs): args = list(args) - + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(self.simulation.local_socket) + timeout = kwargs.get("timeout") + if timeout: + sock.settimeout(timeout) + args.insert(0, msg) def encode(arg): arg = cPickle.dumps(arg) @@ -61,60 +65,63 @@ class LinuxNS3Client(NS3Client): encoded = "|".join(map(encode, args)) sock.send("%s\n" % encoded) - + reply = sock.recv(1024) return cPickle.loads(base64.b64decode(reply)) - def create(self, clazzname, *args): + def create(self, clazzname, *args, **kwargs): args = list(args) args.insert(0, clazzname) - return self.send_msg(NS3WrapperMessage.CREATE, *args) + return self.send_msg(NS3WrapperMessage.CREATE, *args, **kwargs) def factory(self, type_name, **kwargs): args = [type_name] args.append(kwargs) - return self.send_msg(NS3WrapperMessage.FACTORY, *args) + return self.send_msg(NS3WrapperMessage.FACTORY, *args, **kwargs) - def invoke(self, uuid, operation, *args): + def invoke(self, uuid, operation, *args, **kwargs): args = list(args) args.insert(0, operation) args.insert(0, uuid) - return self.send_msg(NS3WrapperMessage.INVOKE, *args) + return self.send_msg(NS3WrapperMessage.INVOKE, *args, **kwargs) - def set(self, uuid, name, value): + def set(self, uuid, name, value, **kwargs): args = [uuid, name, value] - return self.send_msg(NS3WrapperMessage.SET, *args) + return self.send_msg(NS3WrapperMessage.SET, *args, **kwargs) - def get(self, uuid, name): + def get(self, uuid, name, **kwargs): args = [uuid, name] - return self.send_msg(NS3WrapperMessage.GET, *args) + return self.send_msg(NS3WrapperMessage.GET, *args, **kwargs) - def enable_trace(self, *args): - return self.send_msg(NS3WrapperMessage.ENABLE_TRACE, *args) + def enable_trace(self, *args, **kwargs): + return self.send_msg(NS3WrapperMessage.ENABLE_TRACE, *args, **kwargs) - def flush(self): - return self.send_msg(NS3WrapperMessage.FLUSH, []) + def flush(self, **kwargs): + args = [] + return self.send_msg(NS3WrapperMessage.FLUSH, *args, **kwargs) - def start(self): - return self.send_msg(NS3WrapperMessage.START, []) + def start(self, **kwargs): + args = [] + return self.send_msg(NS3WrapperMessage.START, *args, **kwargs) - def stop(self, time = None): + def stop(self, **kwargs): args = [] - if time: - args.append(time) + time = kwargs.get("time") + if time: args.append(time) - return self.send_msg(NS3WrapperMessage.STOP, *args) + return self.send_msg(NS3WrapperMessage.STOP, *args, **kwargs) - def shutdown(self): + def shutdown(self, **kwargs): + args = [] ret = None try: - ret = self.send_msg(NS3WrapperMessage.SHUTDOWN, []) + ret = self.send_msg(NS3WrapperMessage.SHUTDOWN, *args, **kwargs) except: pass diff --git a/src/nepi/resources/linux/ns3/ns3simulation.py b/src/nepi/resources/linux/ns3/ns3simulation.py index d4f37b0e..a613a732 100644 --- a/src/nepi/resources/linux/ns3/ns3simulation.py +++ b/src/nepi/resources/linux/ns3/ns3simulation.py @@ -24,7 +24,7 @@ from nepi.execution.resource import ResourceManager, clsinit_copy, \ from nepi.resources.linux.application import LinuxApplication from nepi.util.timefuncs import tnow, tdiffsec from nepi.resources.ns3.ns3simulation import NS3Simulation -from nepi.resources.ns3.ns3wrapper import GLOBAL_VALUE_UUID +from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID, GLOBAL_VALUE_UUID from nepi.resources.linux.ns3.ns3client import LinuxNS3Client import os @@ -61,10 +61,6 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): type = Types.Bool, flags = Flags.Design) - stop_time = Attribute("stopTime", - "Time to stop the simulation", - flags = Flags.Design) - ns_log = Attribute("nsLog", "NS_LOG environment variable. " \ " Will only generate output if ns-3 is compiled in DEBUG mode. ", @@ -78,7 +74,6 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): cls._register_attribute(impl_type) cls._register_attribute(sched_type) cls._register_attribute(check_sum) - cls._register_attribute(stop_time) cls._register_attribute(ns_log) cls._register_attribute(verbose) @@ -174,10 +169,6 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): stype = self.create("StringValue", sched_type) self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue) - if self._attrs.get("stopTime").has_changed(): - stop_time = self.get("stopTime") - self.stop(time = stop_time) - def do_deploy(self): if not self.node or self.node.state < ResourceState.READY: self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state ) @@ -240,8 +231,7 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): """ if self.state == ResourceState.STARTED: self._client.stop() - self._client.shutdown() - LinuxApplication.do_stop(self) + self.set_stopped() def do_release(self): self.info("Releasing resource") @@ -251,22 +241,11 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation): self.node.execute(tear_down) self.do_stop() + self._client.shutdown() + LinuxApplication.do_stop(self) super(LinuxApplication, self).do_release() - @property - def state(self): - """ Returns the state of the application - """ - state = super(LinuxApplication, self).state - if state == ResourceState.STARTED: - # Check simulator - is_finished = self.invoke(SIMULATOR_UUID, "IsFinished") - if is_finished: - self.do_stop() - - return self._state - @property def _start_command(self): command = [] diff --git a/src/nepi/resources/ns3/ns3client.py b/src/nepi/resources/ns3/ns3client.py index c9e6d775..b7c8be72 100644 --- a/src/nepi/resources/ns3/ns3client.py +++ b/src/nepi/resources/ns3/ns3client.py @@ -22,33 +22,33 @@ class NS3Client(object): def __init__(self): super(NS3Client, self).__init__() - def create(self, clazzname, *args): + def create(self, *args, **kwargs): pass - def factory(self, type_name, **kwargs): + def factory(self, *args, **kwargs): pass - def invoke(self, uuid, operation, *args): + def invoke(self, *args, **kwargs): pass - def set(self, uuid, name, value): + def set(self, *args, **kwargs): pass - def get(self, uuid, name): + def get(self, *args, **kwargs): pass - def trace(self, *args): + def trace(self, *args, **kwargs): pass - def flush(self): + def flush(self, *args, **kwargs): pass - def start(self): + def start(self, *args, **kwargs): pass - def stop(self, time = None): + def stop(self, *args, **kwargs): pass - def shutdown(self): + def shutdown(self, *args, **kwargs): pass diff --git a/src/nepi/resources/ns3/ns3simulation.py b/src/nepi/resources/ns3/ns3simulation.py index abcf8473..165435bb 100644 --- a/src/nepi/resources/ns3/ns3simulation.py +++ b/src/nepi/resources/ns3/ns3simulation.py @@ -22,33 +22,33 @@ class NS3Simulation(object): def client(self): return self._client - def create(self, clazzname, *args): - return self.client.create(clazzname, *args) + def create(self, *args, **kwargs): + return self.client.create(*args, **kwargs) - def factory(self, type_name, **kwargs): - return self.client.factory(type_name, **kwargs) + def factory(self, *args, **kwargs): + return self.client.factory(*args, **kwargs) - def invoke(self, uuid, operation, *args): - return self.client.invoke(uuid, operation, *args) + def invoke(self, *args, **kwargs): + return self.client.invoke(*args, **kwargs) - def set(self, uuid, name, value): - return self.client.set(uuid, name, value) + def set(self, *args, **kwargs): + return self.client.set(*args, **kwargs) - def get(self, uuid, name): - return self.client.get(uuid, name) + def get(self, *args, **kwargs): + return self.client.get(*args, **kwargs) - def enable_trace(self, *args): - return self.client.enable_trace(*args) + def enable_trace(self, *args, **kwargs): + return self.client.enable_trace(*args, **kwargs) - def flush(self): - return self.client.flush() + def flush(self, *args, **kwargs): + return self.client.flush(*args, **kwargs) - def start(self): - return self.client.start() + def start(self, *args, **kwargs): + return self.client.start(*args, **kwargs) - def stop(self, time = None): - return self.client.stop(time) + def stop(self, *args, **kwargs): + return self.client.stop(*args, **kwargs) - def shutdown(self): - return self.client.shutdown() + def shutdown(self, *args, **kwargs): + return self.client.shutdown(*args, **kwargs) diff --git a/src/nepi/resources/ns3/ns3wrapper.py b/src/nepi/resources/ns3/ns3wrapper.py index e3a933fa..77e9b138 100644 --- a/src/nepi/resources/ns3/ns3wrapper.py +++ b/src/nepi/resources/ns3/ns3wrapper.py @@ -224,9 +224,10 @@ class NS3Wrapper(object): result = method(*realargs) - if not result: + if result is None or \ + isinstance(result, bool): return result - + newuuid = self.make_uuid() self._objects[newuuid] = result diff --git a/test/resources/linux/ns3/ns3simulation.py b/test/resources/linux/ns3/ns3simulation.py index 93995634..31c28674 100644 --- a/test/resources/linux/ns3/ns3simulation.py +++ b/test/resources/linux/ns3/ns3simulation.py @@ -41,14 +41,14 @@ class LinuxNS3ClientTest(unittest.TestCase): #self.fedora_host = "peeramide.irisa.fr" self.fedora_user = "inria_test" - def ztest_simple_ping(self): + def test_simple_ping(self): ec = ExperimentController(exp_id = "test-ns3-ping") node = ec.register_resource("LinuxNode") ec.set(node, "hostname", self.fedora_host) ec.set(node, "username", self.fedora_user) ec.set(node, "cleanProcesses", True) - ec.set(node, "cleanHome", True) + #ec.set(node, "cleanHome", True) simu = ec.register_resource("LinuxNS3Simulation") ec.set(simu, "verbose", True) @@ -119,14 +119,14 @@ class LinuxNS3ClientTest(unittest.TestCase): ec.shutdown() - def ztest_real_time(self): + def test_real_time(self): ec = ExperimentController(exp_id = "test-ns3-real-time") node = ec.register_resource("LinuxNode") ec.set(node, "hostname", self.fedora_host) ec.set(node, "username", self.fedora_user) ec.set(node, "cleanProcesses", True) - ec.set(node, "cleanHome", True) + #ec.set(node, "cleanHome", True) simu = ec.register_resource("LinuxNS3Simulation") ec.set(simu, "verbose", True) @@ -206,37 +206,6 @@ class LinuxNS3ClientTest(unittest.TestCase): ec.shutdown() - def test_stop_time(self): - ec = ExperimentController(exp_id = "test-ns3-stop-time") - - node = ec.register_resource("LinuxNode") - ec.set(node, "hostname", self.fedora_host) - ec.set(node, "username", self.fedora_user) - ec.set(node, "cleanProcesses", True) - #ec.set(node, "cleanHome", True) - - simu = ec.register_resource("LinuxNS3Simulation") - ec.set(simu, "verbose", True) - ec.set(simu, "simulatorImplementationType", "ns3::RealtimeSimulatorImpl") - ec.set(simu, "checksumEnabled", True) - ec.set(simu, "stopTime", "20s") - ec.register_connection(simu, node) - - ec.deploy() - - ec.wait_finished([simu]) - - rm = ec.get_resource(simu) - start_time = rm.start_time - stop_time = rm.stop_time - delta = stop_time - start_time - - print delta.seconds - self.assertTrue(delta.seconds >= 20) - self.assertTrue(delta.seconds < 25) - - ec.shutdown() - if __name__ == '__main__': unittest.main() -- 2.43.0