2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
27 from optparse import OptionParser, SUPPRESS_HELP
29 from nepi.resources.netns.netnswrapper import NetNSWrapper
31 class NetNSWrapperMessage:
39 def handle_message(wrapper, msg_type, args, kwargs):
40 if msg_type == NetNSWrapperMessage.SHUTDOWN:
45 if msg_type == NetNSWrapperMessage.CREATE:
46 clazzname = args.pop(0)
48 return wrapper.create(clazzname, *args)
50 if msg_type == NetNSWrapperMessage.INVOKE:
52 operation = args.pop(0)
54 return wrapper.invoke(uuid, operation, *args, **kwargs)
56 if msg_type == NetNSWrapperMessage.GET:
60 return wrapper.get(uuid, name)
62 if msg_type == NetNSWrapperMessage.SET:
67 return wrapper.set(uuid, name, value)
69 if msg_type == NetNSWrapperMessage.FLUSH:
70 # Forces flushing output and error streams.
71 # NS-3 output will stay unflushed until the program exits or
72 # explicit invocation flush is done
76 wrapper.logger.debug("FLUSHED")
80 def create_socket(socket_name):
81 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
82 sock.bind(socket_name)
89 while '\n' not in chunk:
91 chunk = conn.recv(1024)
92 except (OSError, socket.error) as e:
93 if e[0] != errno.EINTR:
104 msg = ''.join(msg).strip()
106 # The message is formatted as follows:
107 # MESSAGE_TYPE|args|kwargs
109 # where MESSAGE_TYPE, args and kwargs are pickld and enoded in base64
112 item = base64.b64decode(item).rstrip()
113 return pickle.loads(item)
115 decoded = [ decode(x) for x in msg.split("|")]
118 dmsg_type = decoded.pop(0)
119 dargs = list(decoded.pop(0)) # transforming touple into list
120 dkwargs = decoded.pop(0)
122 return (dmsg_type, dargs, dkwargs)
124 def send_reply(conn, reply):
125 encoded = base64.b64encode(pickle.dumps(reply))
126 conn.send("%s\n" % encoded)
129 usage = ("usage: %prog -S <socket-name> -D <enable-dump> -v ")
131 parser = OptionParser(usage = usage)
133 parser.add_option("-S", "--socket-name", dest="socket_name",
134 help = "Name for the unix socket used to interact with this process",
135 default = "tap.sock", type="str")
137 parser.add_option("-D", "--enable-dump", dest="enable_dump",
138 help = "Enable dumping the remote executed commands to a script "
139 "in order to later reproduce and debug the experiment",
140 action = "store_true",
143 parser.add_option("-v", "--verbose",
144 help="Print debug output",
146 dest="verbose", default=False)
148 (options, args) = parser.parse_args()
150 return (options.socket_name, options.verbose, options.enable_dump)
152 def run_server(socket_name, level = logging.INFO,
153 enable_dump = False):
155 ###### wrapper instantiation
156 if level == logging.DEBUG:
157 from syslog import LOG_DEBUG
159 netns.environ.set_log_level(LOG_DEBUG)
161 wrapper = NetNSWrapper(loglevel=level, enable_dump = enable_dump)
163 wrapper.logger.info("STARTING...")
165 # create unix socket to receive instructions
166 sock = create_socket(socket_name)
169 # wait for messages to arrive and process them
173 conn, addr = sock.accept()
177 (msg_type, args, kwargs) = recv_msg(conn)
178 except socket.timeout as e:
183 # Ignore - connection lost
186 if msg_type == NetNSWrapperMessage.SHUTDOWN:
190 reply = handle_message(wrapper, msg_type, args, kwargs)
193 err = traceback.format_exc()
194 wrapper.logger.error(err)
198 send_reply(conn, reply)
202 wrapper.logger.info("EXITING...")
204 if __name__ == '__main__':
206 (socket_name, verbose, enable_dump) = get_options()
209 FORMAT = "%(asctime)s %(name)s %(levelname)-4s %(message)s"
210 level = logging.DEBUG if verbose else logging.INFO
212 logging.basicConfig(format = FORMAT, level = level)
215 run_server(socket_name, level, enable_dump)