Skip to content

Commit 0067a7f

Browse files
authored
Merge pull request #23 from zevv/binaryBroker
Implement a MQTT broker binary
2 parents 0e6df8c + 673a58f commit 0067a7f

9 files changed

Lines changed: 1139 additions & 62 deletions

File tree

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
test/connection
2-
test/subscribe
3-
test/publish
4-
test/tester
1+
test/tester
2+
nmqtt
3+
nmqtt_password
4+
nmqtt_pub
5+
nmqtt_sub

README.md

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Native Nim MQTT client library
22

33
This is a hybrid package including a native Nim MQTT library and
4-
binaries for publishing and subscribing to a MQTT-broker.
4+
binaries for a MQTT broker, publisher and subscriber.
55

66
* [Install](#Install)
77
* [Binaries](#Binaries)
8-
* [Publish](#Publish)
9-
* [Subscribe](#Subscribe)
8+
* [nmqtt](#nmqtt)
9+
* [nmqtt_password](#nmqtt_password)
10+
* [nmqtt_pub](#nmqtt_pub)
11+
* [nmqtt_sub](#nmqtt_sub)
1012
* [Library](#Library)
1113
* [Examples](#Examples)
1214
* [Procs](#Procs)
@@ -27,11 +29,72 @@ $ nimble install
2729

2830
# Binaries
2931

30-
The package provides 2 binaries for publishing messages to a MQTT-broker and
31-
for subscribing to a MQTT-broker.
32+
The package provides 4 MQTT binaries:
33+
1) `nmqtt` -> Broker
34+
2) `nmqtt_password` -> Password utility for the broker
35+
3) `nmqtt_pub` -> MQTT publisher
36+
4) `nmqtt_sub` -> MQTT subscriber
3237

33-
## Publish
34-
```bash
38+
39+
## nmqtt
40+
```
41+
$ nmqtt --help
42+
nmqtt is a MQTT v3.1.1 broker
43+
44+
USAGE
45+
nmqtt [options]
46+
nmqtt [-c /path/to/config.conf]
47+
nmqtt [-h hostIP -p port]
48+
49+
CONFIG
50+
Use the configuration file for detailed settings,
51+
such as SSL, adjusting keep alive timer, etc. or
52+
specify options at the command line.
53+
54+
To add and delete users from the password file
55+
please use nmqtt_password:
56+
- nmqtt_password -a|-b|-d [options]
57+
58+
OPTIONS
59+
-?, --help print this cligen-erated help
60+
-c=, --config= absolute path to the config file. Overrides all other options.
61+
-h=, --host= IP-address to serve the broker on.
62+
-p=, --port= network port to accept connecting from.
63+
-v=, --verbosity= verbosity from 0-3.
64+
--max-conn= max simultaneous connections. Defaults to no limit.
65+
--clientid-maxlen= max lenght of clientid. Defaults to 65535.
66+
--clientid-spaces allow spaces in clientid. Defaults to false.
67+
--clientid-empty allow empty clientid and assign random id. Defaults to false.
68+
--client-kickold kick old client, if new client has same clientid. Defaults to false.
69+
--clientid-pass pass clientid in payload {clientid:payload}. Defaults to false.
70+
--password-file= absolute path to the password file
71+
```
72+
_SSL is not supported_
73+
74+
75+
## nmqtt_password
76+
```
77+
$ nmqtt_password --help
78+
Add users and passwords to nmqtt's password file.
79+
80+
USAGE
81+
nmqtt_password -a {password_file.conf} {username}
82+
nmqtt_password -b {password_file.conf} {username} {password}
83+
nmqtt_password -d {password_file.conf} {username}
84+
85+
CONFIG
86+
Add or delete users from nmqtt password file.
87+
88+
OPTIONS
89+
-?, --help print this cligen-erated help
90+
-a, --adduser add a new user to the password file.
91+
-b, --batch run in batch mode to allow passing passwords on the command line.
92+
-d, --deluser delete a user form the password file.
93+
```
94+
95+
96+
## nmqtt_pub
97+
```
3598
$ ./nmqtt_pub --help
3699
Publish MQTT messages to a MQTT-broker.
37100
@@ -65,8 +128,8 @@ OPTIONS
65128
_`-verbose` not implemented yet_
66129

67130

68-
## Subscribe
69-
```bash
131+
## nmqtt_sub
132+
```
70133
$ ./nmqtt_sub --help
71134
Subscribe to a topic on a MQTT-broker.
72135

config/nmqtt.conf

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Config file for nmqtt
2+
#
3+
# Source code is available at https://github.com/zevv/nmqtt
4+
#
5+
# Default values are shown.
6+
#
7+
# Use the # character to indicate a comment, but only if it is the very first
8+
# character on the line. Do NOT insert a comment in front of the options.
9+
10+
# The host is the IP-address which the broker is binding to. As default your
11+
# localhost, `127.0.0.1`, is used.
12+
host = 127.0.0.1
13+
14+
# The port which the broker is bind to. It is normal practice to use `1883` for
15+
# NON-SSL connections while `8883` is used for SSL-connections.
16+
port = 1883
17+
18+
# The verbosity level handles the stderr-output.
19+
# Level `1`
20+
# - This will print when a client connects and disconnect.
21+
# Level `2`
22+
# - This will print level 1 information and packet data.
23+
# Level `3`
24+
# - This will print level 1 and 2 information and detailed data on each client
25+
# and the broker. This will reveal clients password, so it should NOT be
26+
# used in production.
27+
verbosity = 1
28+
29+
# The max lenght of the clients id. Max length is `65535`.
30+
clientid_maxlen = 60
31+
32+
# If spaces in the client id should be allowed.
33+
clientid_spaces = false
34+
35+
# If empty client id's is allowed. If it is set to `true`, the client will be
36+
# assigned a random integer as the client id.
37+
clientid_empty = false
38+
39+
# If set to true the publishers client id will be inserted in front of the
40+
# messages. This will reveal the clients id to all other connected clients.
41+
#
42+
# `false`
43+
# - message
44+
# `true`
45+
# - clientid:message
46+
clientid_pass = false
47+
48+
# Disconnect an existing client if new client has the same client id.
49+
client_kickold = false
50+
51+
# The max simultaneous connections to the broker. Disabled when set
52+
# to `0`.
53+
max_conn = 0
54+
55+
# Allow anonymous connection - no need for username and password. If set to
56+
# true, then an absolute path to `password_file` is required.
57+
allow_anonymous = true
58+
59+
# The absolute path to the password file containing usernames and hashed
60+
# passwords.
61+
#
62+
# To generate the password files and insert entities use `nmqtt_password`:
63+
# $ nmqtt_password -a /path/to/nmqtt_pwd.conf NewUser
64+
password_file =

nmqtt.nimble

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,52 @@ description = "Native MQTT client library and binaries"
55
license = "MIT"
66
srcDir = "src"
77
installFiles = @["nmqtt.nim"]
8-
bin = @["nmqtt_pub", "nmqtt_sub"]
8+
bin = @["nmqtt", "nmqtt_password", "nmqtt_pub", "nmqtt_sub"]
99

1010

1111
# Dependencies
1212
requires "nim >= 1.0.6"
13-
requires "cligen >= 0.9.43"
13+
requires "bcrypt >= 0.2.1"
14+
requires "https://github.com/c-blake/cligen#36d5218"
15+
16+
from strutils import format
17+
18+
after install:
19+
var path: string
20+
21+
echo "\nGenerate default nmqtt.conf? (y/N)"
22+
let genConf = readLineFromStdin()
23+
if genConf == "y" or genConf == "Y":
24+
let confPath = "/home/" & getEnv("USER") & "/.config/nmqtt"
25+
echo "\nAbsolute path to nmqtt config folder. Default: " & confPath
26+
27+
path = readLineFromStdin()
28+
if path == "":
29+
path = confPath
30+
31+
if not dirExists(path):
32+
mkDir(path)
33+
34+
cpFile("config/nmqtt.conf", path & "/nmqtt.conf")
35+
echo "The brokers configuration has been saved at: " & path & "/nmqtt.conf"
36+
37+
echo """
38+
39+
nmqtt v$1 has been installed.
40+
41+
42+
LIBRARY:
43+
Access the nim-libary with an import statement in your code:
44+
45+
`import nmqtt`
46+
47+
48+
BINARIES:
49+
Access the binaries directly with. For help append `--help`.
50+
51+
nmqtt
52+
nmqtt_password
53+
nmqtt_pub
54+
nmqtt_sub
55+
56+
""".format(version)

0 commit comments

Comments
 (0)