Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
paho.mqtt.c
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
eclipse
paho.mqtt.c
Commits
a3162cc7
Commit
a3162cc7
authored
May 15, 2016
by
Ian Craggs
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #129 from guilhermeferreira/fix-samples
Fix samples
parents
01cda138
32e8eaa2
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
176 additions
and
130 deletions
+176
-130
Makefile
Makefile
+1
-1
MQTTAsync_publish.c
src/samples/MQTTAsync_publish.c
+3
-3
MQTTAsync_subscribe.c
src/samples/MQTTAsync_subscribe.c
+3
-3
pahopub.c
src/samples/pahopub.c
+4
-4
pubasync.c
src/samples/pubasync.c
+1
-1
pubsync.c
src/samples/pubsync.c
+1
-1
stdinpub.c
src/samples/stdinpub.c
+44
-33
stdinpuba.c
src/samples/stdinpuba.c
+45
-34
stdoutsub.c
src/samples/stdoutsub.c
+44
-32
stdoutsuba.c
src/samples/stdoutsuba.c
+29
-17
subasync.c
src/samples/subasync.c
+1
-1
No files found.
Makefile
View file @
a3162cc7
...
...
@@ -87,7 +87,7 @@ HEADERS_A = $(HEADERS)
SAMPLE_FILES_C
=
stdinpub stdoutsub pubsync pubasync subasync
SYNC_SAMPLES
=
${
addprefix
${
blddir
}
/samples/,
${
SAMPLE_FILES_C
}}
SAMPLE_FILES_A
=
stdoutsuba MQTTAsync_subscribe MQTTAsync_publish
SAMPLE_FILES_A
=
std
inpuba std
outsuba MQTTAsync_subscribe MQTTAsync_publish
ASYNC_SAMPLES
=
${
addprefix
${
blddir
}
/samples/,
${
SAMPLE_FILES_A
}}
TEST_FILES_C
=
test1 test2 sync_client_test test_mqtt4sync
...
...
src/samples/MQTTAsync_publish.c
View file @
a3162cc7
...
...
@@ -77,7 +77,7 @@ void onSend(void* context, MQTTAsync_successData* response)
if
((
rc
=
MQTTAsync_disconnect
(
client
,
&
opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start sendMessage, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
}
...
...
@@ -110,7 +110,7 @@ void onConnect(void* context, MQTTAsync_successData* response)
if
((
rc
=
MQTTAsync_sendMessage
(
client
,
TOPIC
,
&
pubmsg
,
&
opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start sendMessage, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
}
...
...
@@ -135,7 +135,7 @@ int main(int argc, char* argv[])
if
((
rc
=
MQTTAsync_connect
(
client
,
&
conn_opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
printf
(
"Waiting for publication of %s
\n
"
...
...
src/samples/MQTTAsync_subscribe.c
View file @
a3162cc7
...
...
@@ -126,7 +126,7 @@ void onConnect(void* context, MQTTAsync_successData* response)
if
((
rc
=
MQTTAsync_subscribe
(
client
,
TOPIC
,
QOS
,
&
opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start subscribe, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
}
...
...
@@ -153,7 +153,7 @@ int main(int argc, char* argv[])
if
((
rc
=
MQTTAsync_connect
(
client
,
&
conn_opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
while
(
!
subscribed
)
...
...
@@ -175,7 +175,7 @@ int main(int argc, char* argv[])
if
((
rc
=
MQTTAsync_disconnect
(
client
,
&
disc_opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start disconnect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
while
(
!
disc_finished
)
#if defined(WIN32)
...
...
src/samples/pahopub.c
View file @
a3162cc7
...
...
@@ -55,7 +55,7 @@
volatile
int
toStop
=
0
;
void
usage
()
void
usage
(
void
)
{
printf
(
"MQTT stdin publisher
\n
"
);
printf
(
"Usage: stdinpub topicname <options>, where options are:
\n
"
);
...
...
@@ -68,7 +68,7 @@ void usage()
printf
(
" --maxdatalen 100
\n
"
);
printf
(
" --username none
\n
"
);
printf
(
" --password none
\n
"
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
...
...
@@ -78,7 +78,7 @@ void myconnect(MQTTClient* client, MQTTClient_connectOptions* opts)
if
(
MQTTClient_connect
(
*
client
,
opts
)
!=
0
)
{
printf
(
"Failed to connect
\n
"
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
printf
(
"Connected
\n
"
);
}
...
...
@@ -194,7 +194,7 @@ int main(int argc, char** argv)
MQTTClient_destroy
(
&
client
);
return
0
;
return
EXIT_SUCCESS
;
}
void
getopts
(
int
argc
,
char
**
argv
)
...
...
src/samples/pubasync.c
View file @
a3162cc7
...
...
@@ -78,7 +78,7 @@ int main(int argc, char* argv[])
if
((
rc
=
MQTTClient_connect
(
client
,
&
conn_opts
))
!=
MQTTCLIENT_SUCCESS
)
{
printf
(
"Failed to connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
pubmsg
.
payload
=
PAYLOAD
;
pubmsg
.
payloadlen
=
strlen
(
PAYLOAD
);
...
...
src/samples/pubsync.c
View file @
a3162cc7
...
...
@@ -42,7 +42,7 @@ int main(int argc, char* argv[])
if
((
rc
=
MQTTClient_connect
(
client
,
&
conn_opts
))
!=
MQTTCLIENT_SUCCESS
)
{
printf
(
"Failed to connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
pubmsg
.
payload
=
PAYLOAD
;
pubmsg
.
payloadlen
=
strlen
(
PAYLOAD
);
...
...
src/samples/stdinpub.c
View file @
a3162cc7
...
...
@@ -12,6 +12,7 @@
*
* Contributors:
* Ian Craggs - initial contribution
* Guilherme Maciel Ferreira - add keep alive option
*******************************************************************************/
/*
...
...
@@ -27,8 +28,9 @@
--port 1883
--qos 0
--delimiters \n
--clientid stdin
_
publisher
--clientid stdin
-
publisher
--maxdatalen 100
--keepalive 10
--userid none
--password none
...
...
@@ -55,20 +57,40 @@
volatile
int
toStop
=
0
;
void
usage
()
struct
{
char
*
clientid
;
char
*
delimiter
;
int
maxdatalen
;
int
qos
;
int
retained
;
char
*
username
;
char
*
password
;
char
*
host
;
char
*
port
;
int
verbose
;
int
keepalive
;
}
opts
=
{
"stdin-publisher"
,
"
\n
"
,
100
,
0
,
0
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
,
10
};
void
usage
(
void
)
{
printf
(
"MQTT stdin publisher
\n
"
);
printf
(
"Usage: stdinpub topicname <options>, where options are:
\n
"
);
printf
(
" --host <hostname> (default is
localhost)
\n
"
);
printf
(
" --port <port> (default is
1883)
\n
"
);
printf
(
" --qos <qos> (default is
0)
\n
"
);
printf
(
" --retained (default is
off)
\n
"
);
printf
(
" --delimiter <delim> (default is
\\
n)"
);
printf
(
" --clientid <clientid> (default is
hostname+timestamp)"
);
printf
(
" --maxdatalen
100
\n
"
);
printf
(
" --host <hostname> (default is
%s)
\n
"
,
opts
.
host
);
printf
(
" --port <port> (default is
%s)
\n
"
,
opts
.
port
);
printf
(
" --qos <qos> (default is
%d)
\n
"
,
opts
.
qos
);
printf
(
" --retained (default is
%s)
\n
"
,
opts
.
retained
?
"on"
:
"off
"
);
printf
(
" --delimiter <delim> (default is
\\
n)
\n
"
);
printf
(
" --clientid <clientid> (default is
%s)
\n
"
,
opts
.
clientid
);
printf
(
" --maxdatalen
<bytes> (default is %d)
\n
"
,
opts
.
maxdatalen
);
printf
(
" --username none
\n
"
);
printf
(
" --password none
\n
"
);
exit
(
-
1
);
printf
(
" --keepalive <seconds> (default is %d seconds)
\n
"
,
opts
.
keepalive
);
exit
(
EXIT_FAILURE
);
}
...
...
@@ -78,7 +100,7 @@ void myconnect(MQTTClient* client, MQTTClient_connectOptions* opts)
if
(
MQTTClient_connect
(
*
client
,
opts
)
!=
0
)
{
printf
(
"Failed to connect
\n
"
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
}
...
...
@@ -89,24 +111,6 @@ void cfinish(int sig)
toStop
=
1
;
}
struct
{
char
*
clientid
;
char
*
delimiter
;
int
maxdatalen
;
int
qos
;
int
retained
;
char
*
username
;
char
*
password
;
char
*
host
;
char
*
port
;
int
verbose
;
}
opts
=
{
"publisher"
,
"
\n
"
,
100
,
0
,
0
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
};
void
getopts
(
int
argc
,
char
**
argv
);
int
messageArrived
(
void
*
context
,
char
*
topicName
,
int
topicLen
,
MQTTClient_message
*
m
)
...
...
@@ -130,7 +134,7 @@ int main(int argc, char** argv)
getopts
(
argc
,
argv
);
sprintf
(
url
,
"%s:%s"
,
opts
.
host
,
opts
.
port
);
if
(
opts
.
verbose
)
if
(
opts
.
verbose
)
printf
(
"URL is %s
\n
"
,
url
);
topic
=
argv
[
1
];
...
...
@@ -143,7 +147,7 @@ int main(int argc, char** argv)
rc
=
MQTTClient_setCallbacks
(
client
,
NULL
,
NULL
,
messageArrived
,
NULL
);
conn_opts
.
keepAliveInterval
=
10
;
conn_opts
.
keepAliveInterval
=
opts
.
keepalive
;
conn_opts
.
reliable
=
0
;
conn_opts
.
cleansession
=
1
;
conn_opts
.
username
=
opts
.
username
;
...
...
@@ -188,9 +192,9 @@ int main(int argc, char** argv)
MQTTClient_disconnect
(
client
,
0
);
MQTTClient_destroy
(
&
client
);
MQTTClient_destroy
(
&
client
);
return
0
;
return
EXIT_SUCCESS
;
}
void
getopts
(
int
argc
,
char
**
argv
)
...
...
@@ -268,6 +272,13 @@ void getopts(int argc, char** argv)
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--keepalive"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
keepalive
=
atoi
(
argv
[
count
]);
else
usage
();
}
count
++
;
}
...
...
src/samples/stdinpuba.c
View file @
a3162cc7
...
...
@@ -12,6 +12,7 @@
*
* Contributors:
* Ian Craggs - initial contribution
* Guilherme Maciel Ferreira - add keep alive option
*******************************************************************************/
/*
...
...
@@ -27,8 +28,9 @@
--port 1883
--qos 0
--delimiters \n
--clientid stdin
_publisher
--clientid stdin
-publisher-async
--maxdatalen 100
--keepalive 10
--userid none
--password none
...
...
@@ -54,31 +56,6 @@
volatile
int
toStop
=
0
;
void
usage
()
{
printf
(
"MQTT stdin publisher
\n
"
);
printf
(
"Usage: stdinpub topicname <options>, where options are:
\n
"
);
printf
(
" --host <hostname> (default is localhost)
\n
"
);
printf
(
" --port <port> (default is 1883)
\n
"
);
printf
(
" --qos <qos> (default is 0)
\n
"
);
printf
(
" --retained (default is off)
\n
"
);
printf
(
" --delimiter <delim> (default is
\\
n)"
);
printf
(
" --clientid <clientid> (default is hostname+timestamp)"
);
printf
(
" --maxdatalen 100
\n
"
);
printf
(
" --username none
\n
"
);
printf
(
" --password none
\n
"
);
exit
(
-
1
);
}
void
cfinish
(
int
sig
)
{
signal
(
SIGINT
,
NULL
);
toStop
=
1
;
}
struct
{
char
*
clientid
;
...
...
@@ -90,12 +67,39 @@ struct
char
*
password
;
char
*
host
;
char
*
port
;
int
verbose
;
int
verbose
;
int
keepalive
;
}
opts
=
{
"
publisher"
,
"
\n
"
,
100
,
0
,
0
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
"
stdin-publisher-async"
,
"
\n
"
,
100
,
0
,
0
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
,
1
0
};
void
usage
(
void
)
{
printf
(
"MQTT stdin publisher
\n
"
);
printf
(
"Usage: stdinpub topicname <options>, where options are:
\n
"
);
printf
(
" --host <hostname> (default is %s)
\n
"
,
opts
.
host
);
printf
(
" --port <port> (default is %s)
\n
"
,
opts
.
port
);
printf
(
" --qos <qos> (default is %d)
\n
"
,
opts
.
qos
);
printf
(
" --retained (default is %s)
\n
"
,
opts
.
retained
?
"on"
:
"off"
);
printf
(
" --delimiter <delim> (default is
\\
n)
\n
"
);
printf
(
" --clientid <clientid> (default is %s)
\n
"
,
opts
.
clientid
);
printf
(
" --maxdatalen <bytes> (default is %d)
\n
"
,
opts
.
maxdatalen
);
printf
(
" --username none
\n
"
);
printf
(
" --password none
\n
"
);
printf
(
" --keepalive <seconds> (default is 10 seconds)
\n
"
);
exit
(
EXIT_FAILURE
);
}
void
cfinish
(
int
sig
)
{
signal
(
SIGINT
,
NULL
);
toStop
=
1
;
}
void
getopts
(
int
argc
,
char
**
argv
);
int
messageArrived
(
void
*
context
,
char
*
topicName
,
int
topicLen
,
MQTTAsync_message
*
m
)
...
...
@@ -139,7 +143,7 @@ void myconnect(MQTTAsync* client)
int
rc
=
0
;
printf
(
"Connecting
\n
"
);
conn_opts
.
keepAliveInterval
=
10
;
conn_opts
.
keepAliveInterval
=
opts
.
keepalive
;
conn_opts
.
cleansession
=
1
;
conn_opts
.
username
=
opts
.
username
;
conn_opts
.
password
=
opts
.
password
;
...
...
@@ -152,7 +156,7 @@ void myconnect(MQTTAsync* client)
if
((
rc
=
MQTTAsync_connect
(
*
client
,
&
conn_opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
while
(
connected
==
0
)
#if defined(WIN32)
...
...
@@ -201,7 +205,7 @@ void connectionLost(void* context, char* cause)
if
((
rc
=
MQTTAsync_connect
(
client
,
&
conn_opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
}
...
...
@@ -394,7 +398,7 @@ int main(int argc, char** argv)
if
((
rc
=
MQTTAsync_disconnect
(
client
,
&
disc_opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start disconnect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
while
(
!
disconnected
)
...
...
@@ -404,9 +408,9 @@ int main(int argc, char** argv)
usleep
(
10000L
);
#endif
MQTTAsync_destroy
(
&
client
);
MQTTAsync_destroy
(
&
client
);
return
0
;
return
EXIT_SUCCESS
;
}
void
getopts
(
int
argc
,
char
**
argv
)
...
...
@@ -484,6 +488,13 @@ void getopts(int argc, char** argv)
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--keepalive"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
keepalive
=
atoi
(
argv
[
count
]);
else
usage
();
}
count
++
;
}
...
...
src/samples/stdoutsub.c
View file @
a3162cc7
...
...
@@ -13,6 +13,7 @@
* Contributors:
* Ian Craggs - initial contribution
* Ian Craggs - change delimiter option from char to string
* Guilherme Maciel Ferreira - add keep alive option
*******************************************************************************/
/*
...
...
@@ -29,7 +30,9 @@
--port 1883
--qos 2
--delimiter \n
--clientid stdout_subscriber
--clientid stdout-subscriber
--showtopics off
--keepalive 10
--userid none
--password none
...
...
@@ -55,19 +58,38 @@
volatile
int
toStop
=
0
;
void
usage
()
struct
opts_struct
{
char
*
clientid
;
int
nodelimiter
;
char
*
delimiter
;
int
qos
;
char
*
username
;
char
*
password
;
char
*
host
;
char
*
port
;
int
showtopics
;
int
keepalive
;
}
opts
=
{
"stdout-subscriber"
,
0
,
"
\n
"
,
2
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
,
10
};
void
usage
(
void
)
{
printf
(
"MQTT stdout subscriber
\n
"
);
printf
(
"Usage: stdoutsub topicname <options>, where options are:
\n
"
);
printf
(
" --host <hostname> (default is
localhost)
\n
"
);
printf
(
" --port <port> (default is
1883)
\n
"
);
printf
(
" --qos <qos> (default is
2)
\n
"
);
printf
(
" --host <hostname> (default is
%s)
\n
"
,
opts
.
host
);
printf
(
" --port <port> (default is
%s)
\n
"
,
opts
.
port
);
printf
(
" --qos <qos> (default is
%d)
\n
"
,
opts
.
qos
);
printf
(
" --delimiter <delim> (default is
\\
n)
\n
"
);
printf
(
" --clientid <clientid> (default is
hostname+timestamp)
\n
"
);
printf
(
" --clientid <clientid> (default is
%s)
\n
"
,
opts
.
clientid
);
printf
(
" --username none
\n
"
);
printf
(
" --password none
\n
"
);
printf
(
" --showtopics <on or off> (default is on if the topic has a wildcard, else off)
\n
"
);
exit
(
-
1
);
printf
(
" --showtopics <on or off> (default is on if the topic has a wildcard, else off)
\n
"
);
printf
(
" --keepalive <seconds> (default is %d seconds)
\n
"
,
opts
.
keepalive
);
exit
(
EXIT_FAILURE
);
}
...
...
@@ -77,7 +99,7 @@ void myconnect(MQTTClient* client, MQTTClient_connectOptions* opts)
if
((
rc
=
MQTTClient_connect
(
*
client
,
opts
))
!=
0
)
{
printf
(
"Failed to connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
}
...
...
@@ -88,23 +110,6 @@ void cfinish(int sig)
toStop
=
1
;
}
struct
opts_struct
{
char
*
clientid
;
int
nodelimiter
;
char
*
delimiter
;
int
qos
;
char
*
username
;
char
*
password
;
char
*
host
;
char
*
port
;
int
showtopics
;
}
opts
=
{
"stdout-subscriber"
,
0
,
"
\n
"
,
2
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
};
void
getopts
(
int
argc
,
char
**
argv
);
int
main
(
int
argc
,
char
**
argv
)
...
...
@@ -120,9 +125,9 @@ int main(int argc, char** argv)
topic
=
argv
[
1
];
if
(
strchr
(
topic
,
'#'
)
||
strchr
(
topic
,
'+'
))
if
(
strchr
(
topic
,
'#'
)
||
strchr
(
topic
,
'+'
))
opts
.
showtopics
=
1
;
if
(
opts
.
showtopics
)
if
(
opts
.
showtopics
)
printf
(
"topic is %s
\n
"
,
topic
);
getopts
(
argc
,
argv
);
...
...
@@ -133,7 +138,7 @@ int main(int argc, char** argv)
signal
(
SIGINT
,
cfinish
);
signal
(
SIGTERM
,
cfinish
);
conn_opts
.
keepAliveInterval
=
10
;
conn_opts
.
keepAliveInterval
=
opts
.
keepalive
;
conn_opts
.
reliable
=
0
;
conn_opts
.
cleansession
=
1
;
conn_opts
.
username
=
opts
.
username
;
...
...
@@ -154,7 +159,7 @@ int main(int argc, char** argv)
{
if
(
opts
.
showtopics
)
printf
(
"%s
\t
"
,
topicName
);
if
(
opts
.
nodelimiter
)
if
(
opts
.
nodelimiter
)
printf
(
"%.*s"
,
message
->
payloadlen
,
(
char
*
)
message
->
payload
);
else
printf
(
"%.*s%s"
,
message
->
payloadlen
,
(
char
*
)
message
->
payload
,
opts
.
delimiter
);
...
...
@@ -170,9 +175,9 @@ int main(int argc, char** argv)
MQTTClient_disconnect
(
client
,
0
);
MQTTClient_destroy
(
&
client
);
MQTTClient_destroy
(
&
client
);
return
0
;
return
EXIT_SUCCESS
;
}
void
getopts
(
int
argc
,
char
**
argv
)
...
...
@@ -253,6 +258,13 @@ void getopts(int argc, char** argv)
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--keepalive"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
keepalive
=
atoi
(
argv
[
count
]);
else
usage
();
}
count
++
;
}
...
...
src/samples/stdoutsuba.c
View file @
a3162cc7
...
...
@@ -13,6 +13,7 @@
* Contributors:
* Ian Craggs - initial contribution
* Ian Craggs - fix for bug 413429 - connectionLost not called
* Guilherme Maciel Ferreira - add keep alive option
*******************************************************************************/
/*
...
...
@@ -29,7 +30,9 @@
--port 1883
--qos 2
--delimiter \n
--clientid stdout_subscriber
--clientid stdout-subscriber-async
--showtopics off
--keepalive 10
--userid none
--password none
...
...
@@ -70,33 +73,35 @@ void cfinish(int sig)
struct
{
char
*
clientid
;
int
nodelimiter
;
int
nodelimiter
;
char
delimiter
;
int
qos
;
char
*
username
;
char
*
password
;
char
*
host
;
char
*
port
;
int
showtopics
;
int
showtopics
;
int
keepalive
;
}
opts
=
{
"stdout-subscriber
"
,
1
,
'\n'
,
2
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
"stdout-subscriber
-async"
,
1
,
'\n'
,
2
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
,
1
0
};
void
usage
()
void
usage
(
void
)
{
printf
(
"MQTT stdout subscriber
\n
"
);
printf
(
"Usage: stdoutsub topicname <options>, where options are:
\n
"
);
printf
(
" --host <hostname> (default is
localhost)
\n
"
);
printf
(
" --port <port> (default is
1883)
\n
"
);
printf
(
" --qos <qos> (default is
2)
\n
"
);
printf
(
" --host <hostname> (default is
%s)
\n
"
,
opts
.
host
);
printf
(
" --port <port> (default is
%s)
\n
"
,
opts
.
port
);
printf
(
" --qos <qos> (default is
%d)
\n
"
,
opts
.
qos
);
printf
(
" --delimiter <delim> (default is no delimiter)
\n
"
);
printf
(
" --clientid <clientid> (default is
hostname+timestamp)
\n
"
);
printf
(
" --clientid <clientid> (default is
%s)
\n
"
,
opts
.
clientid
);
printf
(
" --username none
\n
"
);
printf
(
" --password none
\n
"
);
printf
(
" --showtopics <on or off> (default is on if the topic has a wildcard, else off)
\n
"
);
exit
(
-
1
);
printf
(
" --keepalive <seconds> (default is 10 seconds)
\n
"
);
exit
(
EXIT_FAILURE
);
}
...
...
@@ -184,6 +189,13 @@ void getopts(int argc, char** argv)
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--keepalive"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
keepalive
=
atoi
(
argv
[
count
]);
else
usage
();
}
count
++
;
}
...
...
@@ -201,7 +213,7 @@ int messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_messa
fflush
(
stdout
);
MQTTAsync_freeMessage
(
&
message
);
MQTTAsync_free
(
topicName
);
return
1
;
return
1
;
}
...
...
@@ -247,7 +259,7 @@ void onConnect(void* context, MQTTAsync_successData* response)
if
((
rc
=
MQTTAsync_subscribe
(
client
,
topic
,
opts
.
qos
,
&
ropts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start subscribe, return code %d
\n
"
,
rc
);
finished
=
1
;
finished
=
1
;
}
}
...
...
@@ -296,7 +308,7 @@ int main(int argc, char** argv)
signal
(
SIGINT
,
cfinish
);
signal
(
SIGTERM
,
cfinish
);
conn_opts
.
keepAliveInterval
=
10
;
conn_opts
.
keepAliveInterval
=
opts
.
keepalive
;
conn_opts
.
cleansession
=
1
;
conn_opts
.
username
=
opts
.
username
;
conn_opts
.
password
=
opts
.
password
;
...
...
@@ -306,7 +318,7 @@ int main(int argc, char** argv)
if
((
rc
=
MQTTAsync_connect
(
client
,
&
conn_opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
while
(
!
subscribed
)
...
...
@@ -330,10 +342,10 @@ int main(int argc, char** argv)
if
((
rc
=
MQTTAsync_disconnect
(
client
,
&
disc_opts
))
!=
MQTTASYNC_SUCCESS
)
{
printf
(
"Failed to start disconnect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
while
(
!
disconnected
)
while
(
!
disconnected
)
#if defined(WIN32)
Sleep
(
100
);
#else
...
...
@@ -343,7 +355,7 @@ int main(int argc, char** argv)
exit:
MQTTAsync_destroy
(
&
client
);
return
0
;
return
EXIT_SUCCESS
;
}
src/samples/subasync.c
View file @
a3162cc7
...
...
@@ -77,7 +77,7 @@ int main(int argc, char* argv[])
if
((
rc
=
MQTTClient_connect
(
client
,
&
conn_opts
))
!=
MQTTCLIENT_SUCCESS
)
{
printf
(
"Failed to connect, return code %d
\n
"
,
rc
);
exit
(
-
1
);
exit
(
EXIT_FAILURE
);
}
printf
(
"Subscribing to topic %s
\n
for client %s using QoS%d
\n\n
"
"Press Q<Enter> to quit
\n\n
"
,
TOPIC
,
CLIENTID
,
QOS
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment