Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
81530a9b
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
81530a9b
编写于
5月 27, 2020
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify for mqtt start and cleanup
上级
3671647c
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
59 addition
and
55 deletion
+59
-55
src/plugins/mqtt/src/mqttSystem.c
src/plugins/mqtt/src/mqttSystem.c
+59
-55
未找到文件。
src/plugins/mqtt/src/mqttSystem.c
浏览文件 @
81530a9b
...
...
@@ -30,86 +30,90 @@
#include "tsocket.h"
#include "ttimer.h"
#include "mqttSystem.h"
struct
mqtt_client
client
;
pthread_t
client_daemon
;
void
*
mqtt_conn
;
struct
reconnect_state_t
recnt_status
;
char
*
topicPath
;
int
isStop
=
1
;
int32_t
mqttInitSystem
()
{
int
rc
=
0
;
return
rc
;
}
struct
mqtt_client
mqttClient
=
{
0
};
pthread_t
clientDaemonThread
=
{
0
};
void
*
mqttConnect
=
NULL
;
struct
reconnect_state_t
recntStatus
=
{
0
};
char
*
topicPath
=
NULL
;
int
mttIsRuning
=
1
;
int32_t
mqttStar
tSystem
()
{
int32_t
mqttIni
tSystem
()
{
int
rc
=
0
;
uint8_t
sendbuf
[
2048
];
uint8_t
recvbuf
[
1024
];
recnt
_s
tatus
.
sendbuf
=
sendbuf
;
recnt
_s
tatus
.
sendbufsz
=
sizeof
(
sendbuf
);
recnt
_s
tatus
.
recvbuf
=
recvbuf
;
recnt
_s
tatus
.
recvbufsz
=
sizeof
(
recvbuf
);
recnt
S
tatus
.
sendbuf
=
sendbuf
;
recnt
S
tatus
.
sendbufsz
=
sizeof
(
sendbuf
);
recnt
S
tatus
.
recvbuf
=
recvbuf
;
recnt
S
tatus
.
recvbufsz
=
sizeof
(
recvbuf
);
char
*
url
=
tsMqttBrokerAddress
;
recnt
_s
tatus
.
user_name
=
strstr
(
url
,
"@"
)
!=
NULL
?
strbetween
(
url
,
"//"
,
":"
)
:
NULL
;
recnt
_status
.
password
=
strstr
(
url
,
"@"
)
!=
NULL
?
strbetween
(
strstr
(
url
,
recnt_s
tatus
.
user_name
),
":"
,
"@"
)
:
NULL
;
recnt
S
tatus
.
user_name
=
strstr
(
url
,
"@"
)
!=
NULL
?
strbetween
(
url
,
"//"
,
":"
)
:
NULL
;
recnt
Status
.
password
=
strstr
(
url
,
"@"
)
!=
NULL
?
strbetween
(
strstr
(
url
,
recntS
tatus
.
user_name
),
":"
,
"@"
)
:
NULL
;
if
(
strstr
(
url
,
"@"
)
!=
NULL
)
{
recnt
_s
tatus
.
hostname
=
strbetween
(
url
,
"@"
,
":"
);
recnt
S
tatus
.
hostname
=
strbetween
(
url
,
"@"
,
":"
);
}
else
if
(
strstr
(
strstr
(
url
,
"://"
)
+
3
,
":"
)
!=
NULL
)
{
recnt
_s
tatus
.
hostname
=
strbetween
(
url
,
"//"
,
":"
);
recnt
S
tatus
.
hostname
=
strbetween
(
url
,
"//"
,
":"
);
}
else
{
recnt
_s
tatus
.
hostname
=
strbetween
(
url
,
"//"
,
"/"
);
recnt
S
tatus
.
hostname
=
strbetween
(
url
,
"//"
,
"/"
);
}
char
*
_begin_hostname
=
strstr
(
url
,
recnt
_s
tatus
.
hostname
);
char
*
_begin_hostname
=
strstr
(
url
,
recnt
S
tatus
.
hostname
);
if
(
strstr
(
_begin_hostname
,
":"
)
!=
NULL
)
{
recnt
_s
tatus
.
port
=
strbetween
(
_begin_hostname
,
":"
,
"/"
);
recnt
S
tatus
.
port
=
strbetween
(
_begin_hostname
,
":"
,
"/"
);
}
else
{
recnt
_s
tatus
.
port
=
strbetween
(
"'1883'"
,
"'"
,
"'"
);
recnt
S
tatus
.
port
=
strbetween
(
"'1883'"
,
"'"
,
"'"
);
}
topicPath
=
strbetween
(
strstr
(
url
,
strstr
(
_begin_hostname
,
":"
)
!=
NULL
?
recnt
_status
.
port
:
recnt_s
tatus
.
hostname
),
topicPath
=
strbetween
(
strstr
(
url
,
strstr
(
_begin_hostname
,
":"
)
!=
NULL
?
recnt
Status
.
port
:
recntS
tatus
.
hostname
),
"/"
,
"/"
);
char
*
_topic
=
"+/+/+/"
;
int
_tpsize
=
strlen
(
topicPath
)
+
strlen
(
_topic
)
+
1
;
recnt_status
.
topic
=
calloc
(
1
,
_tpsize
);
sprintf
(
recnt_status
.
topic
,
"/%s/%s"
,
topicPath
,
_topic
);
recnt_status
.
client_id
=
strlen
(
tsMqttBrokerClientId
)
<
3
?
tsMqttBrokerClientId
:
"taos_mqtt"
;
taos_init
();
mqttPrint
(
"mqttInitSystem mqtt://%s:%s@%s:%s/%s/"
,
recnt_status
.
user_name
,
recnt_status
.
password
,
recnt_status
.
hostname
,
recnt_status
.
port
,
topicPath
);
mqtt_conn
=
NULL
;
mqtt_init_reconnect
(
&
client
,
mqttReconnectClient
,
&
recnt_status
,
mqtt_PublishCallback
);
if
(
pthread_create
(
&
client_daemon
,
NULL
,
mqttClientRefresher
,
&
client
))
{
recntStatus
.
topic
=
calloc
(
1
,
_tpsize
);
sprintf
(
recntStatus
.
topic
,
"/%s/%s"
,
topicPath
,
_topic
);
recntStatus
.
client_id
=
strlen
(
tsMqttBrokerClientId
)
<
3
?
tsMqttBrokerClientId
:
"taos_mqtt"
;
mqttConnect
=
NULL
;
return
rc
;
}
int32_t
mqttStartSystem
()
{
int
rc
=
0
;
if
(
recntStatus
.
user_name
!=
NULL
&&
recntStatus
.
password
!=
NULL
)
{
mqttPrint
(
"connecting to mqtt://%s:%s@%s:%s/%s/"
,
recntStatus
.
user_name
,
recntStatus
.
password
,
recntStatus
.
hostname
,
recntStatus
.
port
,
topicPath
);
}
else
if
(
recntStatus
.
user_name
!=
NULL
&&
recntStatus
.
password
==
NULL
)
{
mqttPrint
(
"connecting to mqtt://%s@%s:%s/%s/"
,
recntStatus
.
user_name
,
recntStatus
.
hostname
,
recntStatus
.
port
,
topicPath
);
}
mqtt_init_reconnect
(
&
mqttClient
,
mqttReconnectClient
,
&
recntStatus
,
mqtt_PublishCallback
);
if
(
pthread_create
(
&
clientDaemonThread
,
NULL
,
mqttClientRefresher
,
&
mqttClient
))
{
mqttError
(
"Failed to start client daemon."
);
mqttCleanup
(
EXIT_FAILURE
,
-
1
,
NULL
);
rc
=
-
1
;
}
else
{
mqttPrint
(
"listening for '%s' messages."
,
recntStatus
.
topic
);
}
mqttPrint
(
"listening for '%s' messages."
,
recnt_status
.
topic
);
return
rc
;
}
void
mqttStopSystem
()
{
mqttError
(
"Injecting error:
\"
MQTT_ERROR_SOCKET_ERROR
\"
"
);
client
.
error
=
MQTT_ERROR_SOCKET_ERROR
;
isStop
=
0
;
mqttClient
.
error
=
MQTT_ERROR_SOCKET_ERROR
;
mttIsRuning
=
0
;
usleep
(
300000U
);
mqttCleanup
(
EXIT_SUCCESS
,
mqttClient
.
socketfd
,
&
clientDaemonThread
);
mqttPrint
(
"mqtt is stoped"
);
}
void
mqttCleanUpSystem
()
{
mqttPrint
(
"starting to clean up mqtt"
);
mqttCleanup
(
EXIT_SUCCESS
,
client
.
socketfd
,
&
client_daemon
);
taos_cleanup
(
mqtt_conn
);
free
(
recnt_status
.
user_name
);
free
(
recnt_status
.
password
);
free
(
recnt_status
.
hostname
);
free
(
recnt_status
.
port
);
free
(
recnt_status
.
topic
);
free
(
recntStatus
.
user_name
);
free
(
recntStatus
.
password
);
free
(
recntStatus
.
hostname
);
free
(
recntStatus
.
port
);
free
(
recntStatus
.
topic
);
free
(
topicPath
);
mqttPrint
(
"mqtt is cleaned up"
);
}
...
...
@@ -123,9 +127,9 @@ void mqtt_PublishCallback(void** unused, struct mqtt_response_publish* published
char
_token
[
128
]
=
{
0
};
char
_dbname
[
128
]
=
{
0
};
char
_tablename
[
128
]
=
{
0
};
if
(
mqtt
_conn
==
NULL
)
{
if
(
mqtt
Connect
==
NULL
)
{
mqttPrint
(
"connect database"
);
taos_connect_a
(
NULL
,
"_root"
,
tsInternalPass
,
""
,
0
,
mqttInitConnCb
,
&
client
,
&
mqtt_conn
);
taos_connect_a
(
NULL
,
"_root"
,
tsInternalPass
,
""
,
0
,
mqttInitConnCb
,
&
mqttClient
,
&
mqttConnect
);
}
if
(
topic_name
[
1
]
==
'/'
&&
strncmp
((
char
*
)
&
topic_name
[
1
],
topicPath
,
strlen
(
topicPath
))
==
0
)
{
char
*
p_p_cmd_part
[
5
]
=
{
0
};
...
...
@@ -141,10 +145,10 @@ void mqtt_PublishCallback(void** unused, struct mqtt_response_publish* published
mqttPrint
(
"part count=%d,access token:%s,database name:%s, table name:%s"
,
part_index
,
_token
,
_dbname
,
_tablename
);
if
(
mqtt
_conn
!=
NULL
)
{
if
(
mqtt
Connect
!=
NULL
)
{
char
*
_sql
=
converJsonToSql
((
char
*
)
published
->
application_message
,
_dbname
,
_tablename
);
mqttPrint
(
"query:%s"
,
_sql
);
taos_query_a
(
mqtt
_conn
,
_sql
,
mqttQueryInsertCallback
,
&
c
lient
);
taos_query_a
(
mqtt
Connect
,
_sql
,
mqttQueryInsertCallback
,
&
mqttC
lient
);
mqttPrint
(
"free sql:%s"
,
_sql
);
free
(
_sql
);
}
...
...
@@ -154,7 +158,7 @@ void mqtt_PublishCallback(void** unused, struct mqtt_response_publish* published
}
void
*
mqttClientRefresher
(
void
*
client
)
{
while
(
isStop
)
{
while
(
mttIsRuning
)
{
mqtt_sync
((
struct
mqtt_client
*
)
client
);
usleep
(
100000U
);
}
...
...
@@ -171,8 +175,8 @@ void mqttCleanup(int status, int sockfd, pthread_t* client_daemon) {
void
mqttInitConnCb
(
void
*
param
,
TAOS_RES
*
result
,
int32_t
code
)
{
if
(
code
<
0
)
{
mqttError
(
"mqtt:%d, connect to database failed, reason:%s"
,
code
,
tstrerror
(
code
));
taos_close
(
mqtt
_conn
);
mqtt
_conn
=
NULL
;
taos_close
(
mqtt
Connect
);
mqtt
Connect
=
NULL
;
return
;
}
mqttTrace
(
"mqtt:%d, connect to database success, reason:%s"
,
code
,
tstrerror
(
code
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录