Permalink
Browse files

Added a mosquitto example.

  • Loading branch information...
1 parent 52dfd7d commit 43d6ed402f98fa39046d455ffdcbbde0a244c943 @elopio committed Jan 22, 2016
@@ -0,0 +1 @@
+user root
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import argparse
+
+from paho.mqtt import client as mqtt_client
+
+
+DEFAULT_HOST = 'localhost'
+DEFAULT_PORT = 1883
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument(
+ 'host', nargs='?', default=DEFAULT_HOST,
+ help=('The IP or hostname of the MQTT server. '
+ 'Defaults to {}.'.format(DEFAULT_HOST)))
+ parser.add_argument(
+ 'port', type=int, nargs='?', default=DEFAULT_PORT,
+ help=('The port of the MQTT server. '
+ 'Defaults to {}.'.format(DEFAULT_PORT)))
+ parser.add_argument('topic', help=('The topic to publish to.'))
+ parser.add_argument('payload', help=('The payload to send to the topic.'))
+ args = parser.parse_args()
+
+ client = mqtt_client.Client()
+ client.on_connect = on_connect
+ client.on_message = on_message
+
+ client.connect(args.host, args.port)
+ try:
+ client.loop_start()
+ client.publish(args.topic, args.payload)
+ finally:
+ client.loop_stop()
+
+
+if __name__ == "__main__":
+ main()
@@ -0,0 +1,35 @@
+name: mosquitto
+version: 0.1
+summary: mosquitto server and client
+description: |
+ MQTT example with a server, a publisher and a subscriber.
+apps:
+ mosquitto:
+ command: usr/sbin/mosquitto -c $SNAP/mosquitto.conf
+ daemon: simple
+ caps:
+ - network-listener
+ - network-service
+ subscribe:
+ command: bin/subscribe
+ caps:
+ - network-listener
+ security-override:
+ read-paths:
+ - /etc/hosts.deny
+ - /etc/hosts.allow
+ publish:
+ command: bin/publish
+
+parts:
+ mosquitto:
+ plugin: copy
+ stage-packages: [mosquitto]
+ files:
+ mosquitto.conf: mosquitto.conf
+ mqtt-client:
+ plugin: copy
+ files:
+ subscribe.py: bin/subscribe
+ publish.py: bin/publish
+ after: [mqtt-paho-py3]
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+
+import argparse
+
+from paho.mqtt import client as mqtt_client
+
+
+DEFAULT_HOST = 'localhost'
+DEFAULT_PORT = 1883
+
+topic = ''
+
+
+def on_connect(client, userdata, unused1, unused2):
+ # Ignore the unused arguments.
+ del unused1, unused2
+ print('MQTT subscriber connected.')
+ client.subscribe(topic)
+
+
+def on_message(unused1, unused2, message):
+ # Ignore the unused arguments.
+ del unused1, unused2
+ print(message.topic + ' ' + str(message.payload))
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument(
+ 'host', nargs='?', default=DEFAULT_HOST,
+ help=('The IP or hostname of the MQTT server. '
+ 'Defaults to {}.'.format(DEFAULT_HOST)))
+ parser.add_argument(
+ 'port', type=int, nargs='?', default=DEFAULT_PORT,
+ help=('The port of the MQTT server. '
+ 'Defaults to {}.'.format(DEFAULT_PORT)))
+ parser.add_argument('topic', help=('The topic to subscribe to.'))
+ args = parser.parse_args()
+
+ client = mqtt_client.Client()
+ client.on_connect = on_connect
+ client.on_message = on_message
+
+ global topic
+ topic = args.topic
+ client.connect(args.host, args.port)
+ client.loop_forever()
+
+
+if __name__ == "__main__":
+ main()

2 comments on commit 43d6ed4

Owner

elopio replied Jan 22, 2016

ping @jdstrand, I'm trying this, but when I connect the subscriber the snappy-debug tells me to /etc/hosts.deny and /etc/hosts.allow to the security-override. I've done that, but the error keeps telling me the same.
The other suggestion is to only use files in $SNAP, which I can't easily do because the host_access call is in https://git.eclipse.org/c/mosquitto/org.eclipse.mosquitto.git/tree/src/net.c#n98

Can you give me a hand?

FYI, on 16.04 the file accesses were added to the network-listener cap in ubuntu-core-security 16.04.13. These accesses are not in ubuntu-core OS snap 16.04.0-7.

Please sign in to comment.