Esempio n. 1
0
def send():
    if request.method == 'POST':
        # obtain sent message
        message = request.form.get('message')
        message = 'event: ranstr\ndata: ' + message + '\n\n'
        # send sent message to be published to a channel on pushpin
        pub = GripPubControl({'control_uri': 'http://pushpin:5561'})
        pub.publish_http_stream('test', message)
        return render_template('send.html')

    elif request.method == 'GET':
        return render_template('send.html')
    else:
        return redirect(url_for('stream_bp.home'))
Esempio n. 2
0
class PushpinExt(Connector):
    def get_connection_exception(self):
        return ServiceUnavailable

    def connect(self, **kwargs):

        variables = self.variables.copy()
        variables.update(kwargs)

        host = variables.get("host")
        port = variables.get("port")

        control_uri = f"http://{host}:{port}"
        self.pubctrl = GripPubControl({"control_uri": control_uri})

        is_active = self.publish_on_stream("admin",
                                           "Connection test",
                                           sync=True)

        if not is_active:
            raise ServiceUnavailable(f"Pushpin unavailable on {control_uri}")
        return self

    def disconnect(self) -> None:
        self.disconnected = True

    def is_connected(self) -> bool:
        log.warning("pushpin.is_connected method is not implemented")
        return not self.disconnected

    @staticmethod
    def callback(result, message):
        if result:
            log.debug("Message successfully published on pushpin")
        else:  # pragma: no cover
            log.error("Publish failed on pushpin: {}", message)

    def publish_on_stream(self, channel, message, sync=False):
        if not sync:
            self.pubctrl.publish_http_stream(channel,
                                             message,
                                             callback=PushpinExt.callback)
            return True

        try:
            self.pubctrl.publish_http_stream(channel, message, blocking=True)
            log.debug("Message successfully published on pushpin")
            return True
        except BaseException as e:
            log.error("Publish failed on pushpin: {}", message)
            log.error(e)
            return False

    def publish_on_socket(self, channel, message, sync=False):
        item = Item(WebSocketMessageFormat(message, binary=False))
        if not sync:
            self.pubctrl.publish(channel, item, callback=self.callback)
            return True

        try:
            self.pubctrl.publish(channel, item, blocking=True)
            log.debug("Message successfully published on pushpin")
            return True
        except BaseException as e:  # pragma: no cover
            log.error("Publish failed on pushpin: {}", message)
            log.error(e)
            return False