MQTT(mosquitto)の動作確認

## 概要
MQTT(Message Queueing Telemetry Transport)のOSSであるmosquittoについて簡単に動作確認する。

MQTTの詳細概要はhttps://kfep.jp/solution/iot-mqtt/mqtt 等を参照。
以下の構成のPublish/Subscribe型モデルの軽量プロトコル（ヘッダが2バイト）。TCP上のプロトコル。1対1、1対多、多対多に対応している。
Facebook Messengerにも採用されているようである。

Publisher→→Broker→→Subscriber

## 環境
* osxのmultipass上で動作するUbuntu20.04
* mosquitto_sub version 2.0.14 running on libmosquitto 2.0.14
* mosquitto_pub version 2.0.14 running on libmosquitto 2.0.14.

## 動作確認
* 環境構築
<pre>$ sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
$ sudo apt-get update
$ sudo apt install mosquitto</pre>

* バージョン確認
<pre>ubuntu@mqtt:~$ systemctl status mosquitto
● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor pre>
     Active: active (running) since Sun 2022-04-03 18:58:24 JST; 36s ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
   Main PID: 3015 (mosquitto)
      Tasks: 1 (limit: 1132)
     Memory: 1.0M
     CGroup: /system.slice/mosquitto.service
             └─3015 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Apr 03 18:58:24 mqtt systemd[1]: Starting Mosquitto MQTT Broker...
Apr 03 18:58:24 mqtt systemd[1]: Started Mosquitto MQTT Broker.
</pre>

* クライアントインストール。
<pre>$ sudo apt install mosquitto-clients
</pre>

* subscriber起動
<pre>$ mosquitto_sub -t 'test/topic' -v
</pre>

* publisher起動
<pre>$ mosquitto_pub -t 'test/topic' -m 'hello world'
test/topic hello world
</pre>

## パケット確認
Subscriberポート : 48652
Brokerポート：1883
Publisherポート：48654

(1) SubscriberからのBorkerへ登録
<pre>No.     Time  Source       Destination  Protocol Length sport  
      1 0.000000 ::1 ::1 TCP      96     48652  48652 → 1883 [SYN] Seq=0 Win=65476 Len=0 MSS=65476 SACK_PERM=1 TSval=2807574009 TSecr=0 WS=128
      2 0.000013 ::1 ::1 TCP      96     1883   1883 → 48652 [SYN, ACK] Seq=0 Ack=1 Win=65464 Len=0 MSS=65476 SACK_PERM=1 TSval=2807574009 TSecr=2807574009 WS=128
      3 0.000024 ::1 ::1 TCP      88     48652  48652 → 1883 [ACK] Seq=1 Ack=1 Win=65536 Len=0 TSval=2807574009 TSecr=2807574009
      4 0.000173 ::1 ::1 MQTT     102    48652  Connect Command
      5 0.000181 ::1 ::1 TCP      88     1883   1883 → 48652 [ACK] Seq=1 Ack=15 Win=65536 Len=0 TSval=2807574009 TSecr=2807574009
      6 0.000229 ::1 ::1 MQTT     92     1883   Connect Ack
      7 0.000243 ::1 ::1 TCP      88     48652  48652 → 1883 [ACK] Seq=15 Ack=5 Win=65536 Len=0 TSval=2807574009 TSecr=2807574009
      8 0.000275 ::1 ::1 MQTT     105    48652  Subscribe Request
      9 0.000277 ::1 ::1 TCP      88     1883   1883 → 48652 [ACK] Seq=5 Ack=32 Win=65536 Len=0 TSval=2807574009 TSecr=2807574009
     10 0.000305 ::1 ::1 MQTT     93     1883   Subscribe Ack
     11 0.000315 ::1 ::1 TCP      88     48652  48652 → 1883 [ACK] Seq=32 Ack=10 Win=65536 Len=0 TSval=2807574009 TSecr=2807574009
</pre>
(2) PublisherからBorkerへ接続とメッセージ送信。
<pre>     12 2.872744 ::1 ::1 TCP      96     48654  48654 → 1883 [SYN] Seq=0 Win=65476 Len=0 MSS=65476 SACK_PERM=1 TSval=2807576881 TSecr=0 WS=128
     13 2.872757 ::1 ::1 TCP      96     1883   1883 → 48654 [SYN, ACK] Seq=0 Ack=1 Win=65464 Len=0 MSS=65476 SACK_PERM=1 TSval=2807576881 TSecr=2807576881 WS=128
     14 2.872769 ::1 ::1 TCP      88     48654  48654 → 1883 [ACK] Seq=1 Ack=1 Win=65536 Len=0 TSval=2807576881 TSecr=2807576881
     15 2.873002 ::1 ::1 MQTT     102    48654  Connect Command
     16 2.873013 ::1 ::1 TCP      88     1883   1883 → 48654 [ACK] Seq=1 Ack=15 Win=65536 Len=0 TSval=2807576882 TSecr=2807576882
     17 2.873405 ::1 ::1 MQTT     92     1883   Connect Ack
     18 2.873411 ::1 ::1 TCP      88     48654  48654 → 1883 [ACK] Seq=15 Ack=5 Win=65536 Len=0 TSval=2807576882 TSecr=2807576882
     19 2.873470 ::1 ::1 MQTT     113    48654  Publish Message
     20 2.873473 ::1 ::1 TCP      88     1883   1883 → 48654 [ACK] Seq=5 Ack=40 Win=65536 Len=0 TSval=2807576882 TSecr=2807576882
</pre>
(3) BorkerからSubscriberへメッセージ送信。
<pre>     21 2.873503 ::1 ::1 MQTT     113    1883   Publish Message
     22 2.873510 ::1 ::1 TCP      88     48652  48652 → 1883 [ACK] Seq=32 Ack=35 Win=65536 Len=0 TSval=2807576882 TSecr=2807576882
</pre>
(4) Publisher、SubscriberがBrokerと切断。
<pre>     23 2.874045 ::1 ::1 MQTT     90     48654  Disconnect Req
     24 2.874050 ::1 ::1 TCP      88     1883   1883 → 48654 [ACK] Seq=5 Ack=42 Win=65536 Len=0 TSval=2807576883 TSecr=2807576883
     25 2.874139 ::1 ::1 TCP      88     1883   1883 → 48654 [FIN, ACK] Seq=5 Ack=42 Win=65536 Len=0 TSval=2807576883 TSecr=2807576883
     26 2.874258 ::1 ::1 TCP      88     48654  48654 → 1883 [FIN, ACK] Seq=42 Ack=6 Win=65536 Len=0 TSval=2807576883 TSecr=2807576883
     27 2.874269 ::1 ::1 TCP      88     1883   1883 → 48654 [ACK] Seq=6 Ack=43 Win=65536 Len=0 TSval=2807576883 TSecr=2807576883
     28 6.103422 ::1 ::1 MQTT     90     48652  Disconnect Req
     29 6.103442 ::1 ::1 TCP      88     1883   1883 → 48652 [ACK] Seq=35 Ack=34 Win=65536 Len=0 TSval=2807580112 TSecr=2807580112
     30 6.103521 ::1 ::1 TCP      88     1883   1883 → 48652 [FIN, ACK] Seq=35 Ack=34 Win=65536 Len=0 TSval=2807580112 TSecr=2807580112
     31 6.103667 ::1 ::1 TCP      88     48652  48652 → 1883 [FIN, ACK] Seq=34 Ack=36 Win=65536 Len=0 TSval=2807580112 TSecr=2807580112
     32 6.103676 ::1 ::1 TCP      88     1883   1883 → 48652 [ACK] Seq=36 Ack=35 Win=65536 Len=0 TSval=2807580112 TSecr=2807580112
</pre>

## 参考
参考にさせていただきました。
https://symfoware.blog.fc2.com/blog-entry-2205.html