Skip to content

Commit 2cf7b32

Browse files
authored
Merge pull request #19 from dist-sys/add-authentication
Add authentication
2 parents 6779f04 + 12b5b2e commit 2cf7b32

File tree

8 files changed

+37
-15
lines changed

8 files changed

+37
-15
lines changed

doc/usage_en.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# MQTTLoader usage (v0.7.2)
1+
# MQTTLoader usage (v0.7.3)
22
MQTTLoader is a load testing tool (client tool) for MQTT.
33
It supports both MQTT v5.0 and v3.1.1.
44

@@ -27,11 +27,11 @@ You can display the help by:
2727
`$ ./mqttloader -h`
2828

2929
```
30-
MQTTLoader version 0.7.0
30+
MQTTLoader version 0.7.3
3131
usage: mqttloader.Loader -b <arg> [-v <arg>] [-p <arg>] [-s <arg>] [-pq
3232
<arg>] [-sq <arg>] [-ss] [-r] [-t <arg>] [-d <arg>] [-m <arg>] [-ru
3333
<arg>] [-rd <arg>] [-i <arg>] [-st <arg>] [-et <arg>] [-l <arg>]
34-
[-n <arg>] [-im] [-h]
34+
[-n <arg>] [-im] [-un <arg>] [-pw <arg>] [-h]
3535
-b,--broker <arg> Broker URL. E.g., tcp://127.0.0.1:1883
3636
-v,--version <arg> MQTT version ("3" for 3.1.1 or "5" for 5.0).
3737
:
@@ -98,6 +98,8 @@ Please refer to **3. Parameteres of MQTTLoader** for more details of each parame
9898
| -l \<arg\> | INFO | Log level. <br>Valid values are `SEVERE`/`WARNING`/`INFO`/`ALL`. |
9999
| -n \<arg\> | (none) | URL of the NTP server. By setting this, time synchronization is enabled. <br>Ex. `ntp.nict.jp` |
100100
| -im \<arg\> | (none) | Run MQTTLoader by in-memory mode. By default, MQTTLoader writes out measurement records to a file. |
101+
| -un \<arg\> | (none) | User name. Required if the broker has the configuration of password authentication. |
102+
| -pw \<arg\> | (none) | Password. Required if the broker has the configuration of password authentication. |
101103
| -h | | Display help. |
102104

103105
MQTTLoader starts to terminate when all of the following conditions are met.

doc/usage_jp.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# MQTTLoader 利用方法 (v0.7.2)
1+
# MQTTLoader 利用方法 (v0.7.3)
22
MQTTLoaderは、MQTT v5.0とv3.1.1に対応した負荷テストツール(クライアントツール)です。
33

44
## 1. 環境要件
@@ -28,11 +28,11 @@ Windowsユーザはmqttloader.bat(バッチファイル)を、Linux等のユ
2828
`$ ./mqttloader -h`
2929

3030
```
31-
MQTTLoader version 0.7.0
31+
MQTTLoader version 0.7.3
3232
usage: mqttloader.Loader -b <arg> [-v <arg>] [-p <arg>] [-s <arg>] [-pq
3333
<arg>] [-sq <arg>] [-ss] [-r] [-t <arg>] [-d <arg>] [-m <arg>] [-ru
3434
<arg>] [-rd <arg>] [-i <arg>] [-st <arg>] [-et <arg>] [-l <arg>]
35-
[-n <arg>] [-im] [-h]
35+
[-n <arg>] [-im] [-un <arg>] [-pw <arg>] [-h]
3636
-b,--broker <arg> Broker URL. E.g., tcp://127.0.0.1:1883
3737
-v,--version <arg> MQTT version ("3" for 3.1.1 or "5" for 5.0).
3838
:
@@ -99,6 +99,8 @@ publisherとsubscriberを別マシンで動かすことで、負荷が相互に
9999
| -l \<arg\> | INFO | ログレベル。<br>設定可能な値:`SEVERE`/`WARNING`/`INFO`/`ALL` |
100100
| -n \<arg\> | (無し) | NTPサーバのURL。設定すると時刻同期が有効になる(デフォルト:無効)。<br>例:`ntp.nict.jp`  |
101101
| -im \<arg\> | (無し) | MQTTLoaderをメモリ上でのみ動作させる。デフォルトでは、測定レコードはファイルに書き出される。 |
102+
| -un \<arg\> | (無し) | ユーザ名(ブローカにてパスワード認証が設定されている場合に指定)。 |
103+
| -pw \<arg\> | (無し) | パスワード(ブローカにてパスワード認証が設定されている場合に指定)。 |
102104
| -h | | ヘルプを表示 |
103105

104106
MQTTLoaderは、以下の条件をすべて満たすと、クライアントを切断させ終了します。

src/main/java/mqttloader/Constants.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.text.SimpleDateFormat;
2020

2121
public class Constants {
22-
public static final String VERSION = "0.7.2";
22+
public static final String VERSION = "0.7.3";
2323
public static final String FILE_NAME_PREFIX = "mqttloader_";
2424
private static final String HOST_ID = Util.genRandomChars(4);
2525
public static final String SUB_CLIENT_ID_PREFIX = "ml-"+HOST_ID+"-s-";
@@ -51,6 +51,8 @@ public enum Opt {
5151
LOG_LEVEL("l", "log", true, "Log level (SEVERE/WARNING/INFO/ALL).", "INFO"),
5252
NTP("n", "ntp", true, "NTP server. E.g., ntp.nict.jp", null),
5353
IN_MEMORY("im", "inmemory", false, "Enable in-memory mode", null),
54+
USERNAME("un", "username", true, "User name for authentication", null),
55+
PASSWORD("pw", "password", true, "Password for authentication", null),
5456
HELP("h", "help", false, "Display help.", null);
5557

5658
private String name;

src/main/java/mqttloader/Loader.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ private void prepareClients() {
160160
broker = "tcp://"+broker;
161161
}
162162
int version = Util.getOptValInt(Opt.MQTT_VERSION);
163+
String userName = Util.getOptVal(Opt.USERNAME);
164+
String password = Util.getOptVal(Opt.PASSWORD);
163165
int numPub = Util.getOptValInt(Opt.NUM_PUB);
164166
int numSub = Util.getOptValInt(Opt.NUM_SUB);
165167
int pubQos = Util.getOptValInt(Opt.PUB_QOS);
@@ -172,17 +174,17 @@ private void prepareClients() {
172174
int pubInterval = Util.getOptValInt(Opt.INTERVAL);
173175
for(int i=0;i<numPub;i++){
174176
if(version==5){
175-
publishers.add(new PublisherV5(i, broker, pubQos, retain, topic, payloadSize, numMessage, pubInterval, recorder));
177+
publishers.add(new PublisherV5(i, broker, userName, password, pubQos, retain, topic, payloadSize, numMessage, pubInterval, recorder));
176178
}else{
177-
publishers.add(new PublisherV3(i, broker, pubQos, retain, topic, payloadSize, numMessage, pubInterval, recorder));
179+
publishers.add(new PublisherV3(i, broker, userName, password, pubQos, retain, topic, payloadSize, numMessage, pubInterval, recorder));
178180
}
179181
}
180182

181183
for(int i=0;i<numSub;i++){
182184
if(version==5){
183-
subscribers.add(new SubscriberV5(i, broker, subQos, shSub, topic, recorder));
185+
subscribers.add(new SubscriberV5(i, broker, userName, password, subQos, shSub, topic, recorder));
184186
}else{
185-
subscribers.add(new SubscriberV3(i, broker, subQos, topic, recorder));
187+
subscribers.add(new SubscriberV3(i, broker, userName, password, subQos, topic, recorder));
186188
}
187189
}
188190
}

src/main/java/mqttloader/client/PublisherV3.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package mqttloader.client;
1818

19+
import java.nio.charset.StandardCharsets;
20+
1921
import mqttloader.Loader;
2022
import mqttloader.Recorder;
2123
import mqttloader.Util;
@@ -29,14 +31,16 @@ public class PublisherV3 extends AbstractPublisher {
2931
private MqttClient client;
3032
private MqttMessage message = new MqttMessage();
3133

32-
public PublisherV3(int clientNumber, String broker, int qos, boolean retain, String topic, int payloadSize, int numMessage, int pubInterval, Recorder recorder) {
34+
public PublisherV3(int clientNumber, String broker, String userName, String password, int qos, boolean retain, String topic, int payloadSize, int numMessage, int pubInterval, Recorder recorder) {
3335
super(clientNumber, topic, payloadSize, numMessage, pubInterval, recorder);
3436
message.setQos(qos);
3537
message.setRetained(retain);
3638

3739
MqttConnectOptions options = new MqttConnectOptions();
3840
options.setMqttVersion(4);
3941
options.setCleanSession(true);
42+
if(userName != null) options.setUserName(userName);
43+
if(password != null) options.setPassword(password.toCharArray());
4044
try {
4145
client = new MqttClient(broker, clientId, new MemoryPersistence());
4246
client.connect(options);

src/main/java/mqttloader/client/PublisherV5.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package mqttloader.client;
1818

19+
import java.nio.charset.StandardCharsets;
20+
1921
import mqttloader.Loader;
2022
import mqttloader.Recorder;
2123
import mqttloader.Util;
@@ -29,13 +31,15 @@ public class PublisherV5 extends AbstractPublisher {
2931
private MqttClient client;
3032
private MqttMessage message = new MqttMessage();
3133

32-
public PublisherV5(int clientNumber, String broker, int qos, boolean retain, String topic, int payloadSize, int numMessage, int pubInterval, Recorder recorder) {
34+
public PublisherV5(int clientNumber, String broker, String userName, String password, int qos, boolean retain, String topic, int payloadSize, int numMessage, int pubInterval, Recorder recorder) {
3335
super(clientNumber, topic, payloadSize, numMessage, pubInterval, recorder);
3436
message.setQos(qos);
3537
message.setRetained(retain);
3638

3739
MqttConnectionOptions options = new MqttConnectionOptions();
3840
options.setCleanStart(true);
41+
if(userName != null) options.setUserName(userName);
42+
if(password != null) options.setPassword(password.getBytes(StandardCharsets.UTF_8));
3943
try {
4044
client = new MqttClient(broker, clientId, new MemoryPersistence());
4145
client.connect(options);

src/main/java/mqttloader/client/SubscriberV3.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
public class SubscriberV3 extends AbstractSubscriber implements MqttCallback {
3131
private MqttClient client;
3232

33-
public SubscriberV3(int clientNumber, String broker, int qos, String topic, Recorder recorder) {
33+
public SubscriberV3(int clientNumber, String broker, String userName, String password, int qos, String topic, Recorder recorder) {
3434
super(clientNumber, recorder);
3535
MqttConnectOptions options = new MqttConnectOptions();
3636
options.setMqttVersion(4);
3737
options.setCleanSession(true);
38+
if(userName != null) options.setUserName(userName);
39+
if(password != null) options.setPassword(password.toCharArray());
3840
try {
3941
client = new MqttClient(broker, clientId, new MemoryPersistence());
4042
client.setCallback(this);

src/main/java/mqttloader/client/SubscriberV5.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package mqttloader.client;
1818

19+
import java.nio.charset.StandardCharsets;
20+
1921
import mqttloader.Loader;
2022
import mqttloader.Recorder;
2123
import org.eclipse.paho.mqttv5.client.IMqttToken;
@@ -31,10 +33,12 @@
3133
public class SubscriberV5 extends AbstractSubscriber implements MqttCallback {
3234
private MqttClient client;
3335

34-
public SubscriberV5(int clientNumber, String broker, int qos, boolean shSub, String topic, Recorder recorder) {
36+
public SubscriberV5(int clientNumber, String broker, String userName, String password, int qos, boolean shSub, String topic, Recorder recorder) {
3537
super(clientNumber, recorder);
3638
MqttConnectionOptions options = new MqttConnectionOptions();
3739
options.setCleanStart(true);
40+
if(userName != null) options.setUserName(userName);
41+
if(password != null) options.setPassword(password.getBytes(StandardCharsets.UTF_8));
3842
try {
3943
client = new MqttClient(broker, clientId, new MemoryPersistence());
4044
client.setCallback(this);

0 commit comments

Comments
 (0)