def iproute(args): '''An iproute wrapper''' try: IPROUTE_PATH = settings.IPROUTE_PATH except AttributeError: IPROUTE_PATH = os.path.join('/', 'sbin', 'ip') if not os.path.isfile(IPROUTE_PATH): error_msg = ('Can not find %s.\n' 'Have you got iproute properly installed?') raise ImproperlyConfigured(error_msg % IPROUTE_PATH) args_list = args.split() cmd = [IPROUTE_PATH] + args_list logging.debug(' '.join(cmd)) proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) stdout_value, stderr_value = proc.communicate() if stderr_value: raise IPROUTECommandError(stderr_value) return stdout_value
def radar(self): """ Send broadcast packets and store the results in neigh """ self.radar_id = randint(0, 2**32 - 1) logging.debug('radar scan %s' % self.radar_id) # we're sending the broadcast packets NOW self.bcast_send_time = self.xtime.time() # send all packets in the bouquet for i in xrange(self.max_bouquet): self.broadcast.radar.reply(self.ntkd_id, self.radar_id) # then wait self.xtime.swait(self.max_wait_time * 1000) # update the neighbours' ip_table self.neigh.store(self.get_all_avg_rtt()) # Send the event self.bouquet_numb += 1 self.events.send('SCAN_DONE', (self.bouquet_numb, )) # We're done. Reset. self.radar_reset()
def func_get(self, func_name): """Returns the function (if any and if remotable), which has the given `func_name' `func_name' is a string in the form "mod1.mod2.mod3....modn.func" or just "func". In the latter case "func" is searched in the globals() """ if not 'radar' in func_name: logging.debug("func_get: " + str(func_name)) splitted = func_name.split('.') if not len(splitted): return None mods, func = splitted[:-1], splitted[-1] p = self.root_instance try: for m in mods: p = getattr(p, m) if func in map(lambda f: f.__name__, p.remotable_funcs): return getattr(p, func) except AttributeError: return None return None
def rpc_call(self, func_name, params): '''Performs a rpc call @param func_name: name of the remote callable @param params: a tuple of arguments to pass to the remote callable ''' while not self.connected: self.connect() self.xtime.swait(500) data = rencode.dumps((func_name, params)) self.socket.sendall(_data_pack(data)) recv_encoded_data = _data_unpack_from_stream_socket(self.socket) if not recv_encoded_data: raise RPCNetError('Connection closed before reply') recv_data = rencode.loads(recv_encoded_data) print recv_data logging.debug('Recvd data: %s' % str(recv_data)) # Handling errors # I receive a message with the following format: # ('rmt_error', message_error) # where message_error is a string if isinstance(recv_data, tuple) and recv_data[0] == 'rmt_error': raise RPCError(recv_data[1]) return recv_data
def radar(self): """ Send broadcast packets and store the results in neigh """ self.radar_id = randint(0, 2**32-1) logging.debug('radar scan %s' % self.radar_id) # we're sending the broadcast packets NOW self.bcast_send_time = self.xtime.time() # send all packets in the bouquet for i in xrange(self.max_bouquet): self.broadcast.radar.reply(self.ntkd_id, self.radar_id) # then wait self.xtime.swait(self.max_wait_time * 1000) # update the neighbours' ip_table self.neigh.store(self.get_all_avg_rtt()) # Send the event self.bouquet_numb += 1 self.events.send('SCAN_DONE', (self.bouquet_numb,)) # We're done. Reset. self.radar_reset()
def func_get(self, func_name): """Returns the function (if any and if remotable), which has the given `func_name' `func_name' is a string in the form "mod1.mod2.mod3....modn.func" or just "func". In the latter case "func" is searched in the globals() """ if not "radar" in func_name: logging.debug("func_get: " + str(func_name)) splitted = func_name.split(".") if not len(splitted): return None mods, func = splitted[:-1], splitted[-1] p = self.root_instance try: for m in mods: p = getattr(p, m) if func in map(lambda f: f.__name__, p.remotable_funcs): return getattr(p, func) except AttributeError: return None return None
def rpc_call(self, func_name, params): """Performs a rpc call @param func_name: name of the remote callable @param params: a tuple of arguments to pass to the remote callable """ while not self.connected: self.connect() self.xtime.swait(500) data = rencode.dumps((func_name, params)) self.socket.sendall(_data_pack(data)) recv_encoded_data = _data_unpack_from_stream_socket(self.socket) if not recv_encoded_data: raise RPCNetError("Connection closed before reply") recv_data = rencode.loads(recv_encoded_data) print recv_data logging.debug("Recvd data: %s" % str(recv_data)) # Handling errors # I receive a message with the following format: # ('rmt_error', message_error) # where message_error is a string if isinstance(recv_data, tuple) and recv_data[0] == "rmt_error": raise RPCError(recv_data[1]) return recv_data
def dgram_request_handler(sock, clientaddr, packet, dev, rpcdispatcher): '''RPC stream request handler class Handles all request and try to decode them. ''' caller = CallerInfo(clientaddr[0], clientaddr[1], dev, sock) #logging.debug('UDP packet from %s, dev %s', clientaddr, dev) try: data = _data_unpack_from_buffer(packet) #logging.debug('Handling data: %s', data) response = rpcdispatcher.marshalled_dispatch(caller, data) except RPCError: logging.debug('An error occurred during request handling')
def dgram_request_handler(sock, clientaddr, packet, dev, rpcdispatcher): """RPC stream request handler class Handles all request and try to decode them. """ caller = CallerInfo(clientaddr[0], clientaddr[1], dev, sock) # logging.debug('UDP packet from %s, dev %s', clientaddr, dev) try: data = _data_unpack_from_buffer(packet) # logging.debug('Handling data: %s', data) response = rpcdispatcher.marshalled_dispatch(caller, data) except RPCError: logging.debug("An error occurred during request handling")
def _dispatch(self, caller, func_name, params): if not "radar" in func_name: logging.debug("_dispatch: " + func_name + "(" + str(params) + ")") func = self.func_get(func_name) if func is None: raise RPCFuncNotRemotable("Function %s is not remotable" % func_name) try: if "_rpc_caller" in func.im_func.func_code.co_varnames: return func(caller, *params) else: return func(*params) except Exception, e: # I propagate all exceptions to `dispatch' raise
def marshalled_dispatch(self, caller, data): '''Dispatches a RPC function from marshalled data''' error = 0 try: unpacked = rencode.loads(data) except ValueError: error = 1 if error or not isinstance(unpacked, tuple) or not len(unpacked) == 2: e = 'Malformed packet received from ' + caller.ip logging.debug(e) response = ('rmt_error', str(e)) else: response = self.dispatch(caller, *unpacked) return rencode.dumps(response)
def _dispatch(self, caller, func_name, params): if not 'radar' in func_name: logging.debug("_dispatch: " + func_name + "(" + str(params) + ")") func = self.func_get(func_name) if func is None: raise RPCFuncNotRemotable('Function %s is not remotable' % func_name) try: if '_rpc_caller' in func.im_func.func_code.co_varnames: return func(caller, *params) else: return func(*params) except Exception, e: # I propagate all exceptions to `dispatch' raise
def marshalled_dispatch(self, caller, data): """Dispatches a RPC function from marshalled data""" error = 0 try: unpacked = rencode.loads(data) except ValueError: error = 1 if error or not isinstance(unpacked, tuple) or not len(unpacked) == 2: e = "Malformed packet received from " + caller.ip logging.debug(e) response = ("rmt_error", str(e)) else: response = self.dispatch(caller, *unpacked) return rencode.dumps(response)
def stream_request_handler(sock, clientaddr, dev, rpcdispatcher): logging.debug('Connected from %s, dev %s', clientaddr, dev) caller = CallerInfo(clientaddr[0], clientaddr[1], dev, sock) while True: try: data = _data_unpack_from_stream_socket(sock) if not data: break logging.debug('Handling data: %s', data) response = rpcdispatcher.marshalled_dispatch(caller, data) logging.debug('Response: %s', response) except RPCError: logging.debug('An error occurred during request handling') sock.send(_data_pack(response)) #self.request.close() logging.debug('Response sent') sock.close()
def stream_request_handler(sock, clientaddr, dev, rpcdispatcher): logging.debug("Connected from %s, dev %s", clientaddr, dev) caller = CallerInfo(clientaddr[0], clientaddr[1], dev, sock) while True: try: data = _data_unpack_from_stream_socket(sock) if not data: break logging.debug("Handling data: %s", data) response = rpcdispatcher.marshalled_dispatch(caller, data) logging.debug("Response: %s", response) except RPCError: logging.debug("An error occurred during request handling") sock.send(_data_pack(response)) # self.request.close() logging.debug("Response sent") sock.close()
def dispatch(self, caller, func, params): try: response = self._dispatch(caller, func, params) except Exception, e: logging.debug(str(e)) response = ('rmt_error', str(e))
def dispatch(self, caller, func, params): try: response = self._dispatch(caller, func, params) except Exception, e: logging.debug(str(e)) response = ("rmt_error", str(e))
if '_rpc_caller' in func.im_func.func_code.co_varnames: return func(caller, *params) else: return func(*params) except Exception, e: # I propagate all exceptions to `dispatch' raise def dispatch(self, caller, func, params): try: response = self._dispatch(caller, func, params) except Exception, e: logging.debug(str(e)) response = ('rmt_error', str(e)) if not 'radar' in func: logging.debug("dispatch response: " + str(response)) return response def marshalled_dispatch(self, caller, data): '''Dispatches a RPC function from marshalled data''' error = 0 try: unpacked = rencode.loads(data) except ValueError: error = 1 if error or not isinstance(unpacked, tuple) or not len(unpacked) == 2: e = 'Malformed packet received from ' + caller.ip logging.debug(e) response = ('rmt_error', str(e)) else: response = self.dispatch(caller, *unpacked)
if "_rpc_caller" in func.im_func.func_code.co_varnames: return func(caller, *params) else: return func(*params) except Exception, e: # I propagate all exceptions to `dispatch' raise def dispatch(self, caller, func, params): try: response = self._dispatch(caller, func, params) except Exception, e: logging.debug(str(e)) response = ("rmt_error", str(e)) if not "radar" in func: logging.debug("dispatch response: " + str(response)) return response def marshalled_dispatch(self, caller, data): """Dispatches a RPC function from marshalled data""" error = 0 try: unpacked = rencode.loads(data) except ValueError: error = 1 if error or not isinstance(unpacked, tuple) or not len(unpacked) == 2: e = "Malformed packet received from " + caller.ip logging.debug(e) response = ("rmt_error", str(e)) else: response = self.dispatch(caller, *unpacked)