class AtlasStream(object): CHANNEL_RESULT = "atlas_result" CHANNEL_PROBE = "atlas_probestatus" CHANNEL_ERROR = "atlas_error" CHANNELS = { "result": CHANNEL_RESULT, "probe": CHANNEL_PROBE, "error": CHANNEL_ERROR, } def __init__(self): """Initialize stream""" self.iosocket_server = "atlas-stream.ripe.net" self.iosocket_resource = "/stream/socket.io" self.socketIO = None def connect(self): """Initiate the channel we want to start streams from.""" self.socketIO = SocketIO(host=self.iosocket_server, port=80, resource=self.iosocket_resource, transports=["websocket"]) def disconnect(self): """Exits the channel k shuts down connection.""" self.socketIO.disconnect() self.socketIO.__exit__([]) def bind_channel(self, channel, callback): """Bind given channel with the given callback""" try: self.socketIO.on(self.CHANNELS[channel], callback) except KeyError: print("The given channel: <{0}> is not valid".format(channel)) def start_stream(self, stream_type, **stream_parameters): """Starts new stream for given type with given parameters""" if stream_type in ("result", "probestatus"): self.subscribe(stream_type, **stream_parameters) else: print("Given stream type: <{0}> is not valid".format(stream_type)) def subscribe(self, stream_type, **parameters): """Subscribe to stream with give parameters.""" parameters.update({"stream_type": stream_type}) self.socketIO.emit('atlas_subscribe', parameters) def timeout(self, seconds=None): """ Times out all streams after n seconds or wait forever if seconds is None """ if seconds is None: self.socketIO.wait() else: self.socketIO.wait(seconds=seconds)
class AtlasStream(object): CHANNEL_RESULT = "atlas_result" CHANNEL_PROBE = "atlas_probe" CHANNEL_ERROR = "atlas_error" CHANNELS = {"result": CHANNEL_RESULT, "probe": CHANNEL_PROBE, "error": CHANNEL_ERROR} def __init__(self, **kwargs): """Initialize stream""" self.iosocket_server = "atlas-stream.ripe.net" self.iosocket_resource = "/stream/socket.io" self.socketIO = None def connect(self): """Initiate the channel we want to start streams from.""" self.socketIO = SocketIO( host=self.iosocket_server, port=80, resource=self.iosocket_resource, transports=["websocket"] ) def disconnect(self): """Exits the channel k shuts down connection.""" self.socketIO.disconnect() self.socketIO.__exit__([]) def bind_stream(self, stream_type, callback): """Bind given type stream with the given callback""" try: self.socketIO.on(self.CHANNELS[stream_type], callback) except KeyError: print "The given stream type: <{}> is not valid".format(stream_type) def start_stream(self, stream_type, **stream_parameters): """Starts new stream for given type with given parameters""" if stream_type in ("result", "probestatus"): self.subscribe(stream_type, **stream_parameters) else: print "Given stream type: <%s> is not valid" % stream_type def subscribe(self, stream_type, **parameters): """Subscribe to stream with give parameters.""" parameters.update({"stream_type": stream_type}) self.socketIO.emit("atlas_subscribe", parameters) def timeout(self, seconds=None): """ Times out all streams after n seconds or wait forever if seconds is None """ if seconds is None: self.socketIO.wait() else: self.socketIO.wait(seconds=seconds)
class AtlasStream(object): EVENT_NAME_RESULTS = "atlas_result" EVENT_NAME_SUBSCRIBE = "atlas_subscribe" EVENT_NAME_ERROR = "atlas_error" # Remove the following list when deprecation time expires CHANNELS = { "result": "atlas_result", "probe": "atlas_probestatus", "error": "atlas_error", } # ------------------------------------------------------- def __init__(self, debug=False, server=False, proxies=None, headers=None): """Initialize stream""" self.iosocket_server = "atlas-stream.ripe.net" self.iosocket_resource = "/stream/socket.io" self.socketIO = None self.debug = debug self.error_callback = None self.proxies = proxies or {} self.headers = headers or {} if self.debug and server: self.iosocket_server = server def handle_error(self, error): if self.error_callback is not None: self.error_callback(error) else: print(error) def connect(self): """Initiate the channel we want to start streams from.""" self.socketIO = SocketIO( host=self.iosocket_server, port=80, resource=self.iosocket_resource, proxies=self.proxies, headers=self.headers, transports=["websocket"] ) self.socketIO.on(self.EVENT_NAME_ERROR, self.handle_error) def disconnect(self): """Exits the channel k shuts down connection.""" self.socketIO.disconnect() self.socketIO.__exit__([]) def unpack_results(self, callback, data): if isinstance(data, list): for result in data: callback(result) else: callback(data) def bind_channel(self, channel, callback): """Bind given channel with the given callback""" # Remove the following list when deprecation time expires if channel in self.CHANNELS: warning = ( "The event name '{}' will soon be deprecated. Use " "the real event name '{}' instead." ).format(channel, self.CHANNELS[channel]) self.handle_error(warning) channel = self.CHANNELS[channel] # ------------------------------------------------------- if channel == self.EVENT_NAME_ERROR: self.error_callback = callback elif channel == self.EVENT_NAME_RESULTS: self.socketIO.on(channel, partial(self.unpack_results, callback)) else: self.socketIO.on(channel, callback) def start_stream(self, stream_type, **stream_parameters): """Starts new stream for given type with given parameters""" if stream_type: self.subscribe(stream_type, **stream_parameters) else: self.handle_error("You need to set a stream type") def subscribe(self, stream_type, **parameters): """Subscribe to stream with give parameters.""" parameters["stream_type"] = stream_type if (stream_type == "result") and ("buffering" not in parameters): parameters["buffering"] = True self.socketIO.emit(self.EVENT_NAME_SUBSCRIBE, parameters) def timeout(self, seconds=None): """ Times out all streams after n seconds or wait forever if seconds is None """ if seconds is None: self.socketIO.wait() else: self.socketIO.wait(seconds=seconds)
class AtlasStream(object): EVENT_NAME_RESULTS = "atlas_result" EVENT_NAME_SUBSCRIBE = "atlas_subscribe" EVENT_NAME_ERROR = "atlas_error" # Remove the following list when deprecation time expires CHANNELS = { "result": "atlas_result", "probe": "atlas_probestatus", "error": "atlas_error", } # ------------------------------------------------------- def __init__(self, debug=False, server=False, proxies=None, headers=None): """Initialize stream""" self.iosocket_server = "atlas-stream.ripe.net" self.iosocket_resource = "/stream/socket.io" self.socketIO = None self.debug = debug self.error_callback = None self.proxies = proxies or {} self.headers = headers or {} if not self.headers or not self.headers.get("User-Agent", None): user_agent = "RIPE ATLAS Cousteau v{0}".format(__version__) self.headers["User-Agent"] = user_agent if self.debug and server: self.iosocket_server = server def handle_error(self, error): if self.error_callback is not None: self.error_callback(error) else: print(error) def connect(self): """Initiate the channel we want to start streams from.""" self.socketIO = SocketIO( host=self.iosocket_server, port=80, resource=self.iosocket_resource, proxies=self.proxies, headers=self.headers, transports=["websocket"], Namespace=AtlasNamespace, ) self.socketIO.on(self.EVENT_NAME_ERROR, self.handle_error) def disconnect(self): """Exits the channel k shuts down connection.""" self.socketIO.disconnect() self.socketIO.__exit__([]) def unpack_results(self, callback, data): if isinstance(data, list): for result in data: callback(result) else: callback(data) def bind_channel(self, channel, callback): """Bind given channel with the given callback""" # Remove the following list when deprecation time expires if channel in self.CHANNELS: warning = ( "The event name '{}' will soon be deprecated. Use " "the real event name '{}' instead." ).format(channel, self.CHANNELS[channel]) self.handle_error(warning) channel = self.CHANNELS[channel] # ------------------------------------------------------- if channel == self.EVENT_NAME_ERROR: self.error_callback = callback elif channel == self.EVENT_NAME_RESULTS: self.socketIO.on(channel, partial(self.unpack_results, callback)) else: self.socketIO.on(channel, callback) def start_stream(self, stream_type, **stream_parameters): """Starts new stream for given type with given parameters""" if stream_type: self.subscribe(stream_type, **stream_parameters) else: self.handle_error("You need to set a stream type") def subscribe(self, stream_type, **parameters): """Subscribe to stream with give parameters.""" parameters["stream_type"] = stream_type if (stream_type == "result") and ("buffering" not in parameters): parameters["buffering"] = True self.socketIO.emit(self.EVENT_NAME_SUBSCRIBE, parameters) def timeout(self, seconds=None): """ Times out all streams after n seconds or wait forever if seconds is None """ if seconds is None: self.socketIO.wait() else: self.socketIO.wait(seconds=seconds)