version 1.1.2.30, 2012/01/24 10:18:45
|
version 1.2.2.3, 2012/01/30 08:29:55
|
Line 2
|
Line 2
|
#include "rtlm.h" |
#include "rtlm.h" |
#include "utils.h" |
#include "utils.h" |
#include "mqttd.h" |
#include "mqttd.h" |
|
#include "mqttd_calls.h" |
|
|
|
|
static void *startSession(sched_task_t *task); |
static void *startSession(sched_task_t *task); |
Line 26 initSession(int sock, ait_val_t * __restrict v)
|
Line 27 initSession(int sock, ait_val_t * __restrict v)
|
} else |
} else |
memset(sess, 0, sizeof(struct tagSession)); |
memset(sess, 0, sizeof(struct tagSession)); |
|
|
|
TAILQ_INIT(&sess->sess_sndqueue); |
|
|
str = (const char*) cfg_GetAttribute(&cfg, CFG("mqttd"), CFG("retry")); |
str = (const char*) cfg_GetAttribute(&cfg, CFG("mqttd"), CFG("retry")); |
if (!str) |
if (!str) |
sess->sess_retry = DEFAULT_RETRY; |
sess->sess_retry = DEFAULT_RETRY; |
Line 40 initSession(int sock, ait_val_t * __restrict v)
|
Line 43 initSession(int sock, ait_val_t * __restrict v)
|
return NULL; |
return NULL; |
} |
} |
|
|
|
sess->sess_srv = mqtt_srv_Init(sock, sess->sess_buf); |
|
if (!sess->sess_srv) { |
|
ioDEBUG(3, "Error:: in srv_Init #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
mqtt_msgFree(&sess->sess_buf, 42); |
|
free(sess); |
|
io_freeVar(v); |
|
return NULL; |
|
} else { |
|
mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_CONNECT, cmdCONNECT); |
|
mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBLISH, cmdPUBLISH); |
|
mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBREL, cmdPUBREL); |
|
mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_SUBSCRIBE, cmdSUBSCRIBE); |
|
mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_UNSUBSCRIBE, cmdUNSUBSCRIBE); |
|
mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PINGREQ, cmdPINGREQ); |
|
mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_DISCONNECT, cmdDISCONNECT); |
|
} |
|
|
sess->sess_sock = sock; |
sess->sess_sock = sock; |
strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr); |
strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr); |
io_freeVar(v); |
io_freeVar(v); |
Line 59 finiSession(struct tagSession *sess, int preservSock)
|
Line 79 finiSession(struct tagSession *sess, int preservSock)
|
if (call.FiniSessPUB) |
if (call.FiniSessPUB) |
call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); |
call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); |
|
|
while ((store = SLIST_FIRST(&sess->sess_sndqueue))) { | while ((store = TAILQ_FIRST(&sess->sess_sndqueue))) { |
SLIST_REMOVE_HEAD(&sess->sess_sndqueue, st_node); | TAILQ_REMOVE(&sess->sess_sndqueue, store, st_node); |
| |
| if (store->st_subscr.sub_topic.msg_base) |
| free(store->st_subscr.sub_topic.msg_base); |
| if (store->st_subscr.sub_value.msg_base) |
| free(store->st_subscr.sub_value.msg_base); |
| |
free(store); |
free(store); |
} |
} |
|
|
Line 72 finiSession(struct tagSession *sess, int preservSock)
|
Line 98 finiSession(struct tagSession *sess, int preservSock)
|
if (sess->sess_sock > STDERR_FILENO && !preservSock) |
if (sess->sess_sock > STDERR_FILENO && !preservSock) |
srv_Close(sess->sess_sock); |
srv_Close(sess->sess_sock); |
|
|
|
mqtt_srv_Fini(&sess->sess_srv); |
mqtt_msgFree(&sess->sess_buf, 42); |
mqtt_msgFree(&sess->sess_buf, 42); |
|
|
free(sess); |
free(sess); |
Line 156 KASession(struct tagSession *sess)
|
Line 183 KASession(struct tagSession *sess)
|
static void * |
static void * |
thrSession(struct tagSession *sess) |
thrSession(struct tagSession *sess) |
{ |
{ |
mqtt_msg_t msg = { NULL, 0 }; |
|
int ret, locKill = 42; |
int ret, locKill = 42; |
struct pollfd pfd; |
struct pollfd pfd; |
struct mqtthdr *hdr; |
struct mqtthdr *hdr; |
ait_val_t *v; |
ait_val_t *v; |
struct tagStore *store; |
|
|
|
pthread_cleanup_push((void(*)(void*)) stopSession, sess); |
pthread_cleanup_push((void(*)(void*)) stopSession, sess); |
ioTRACE(2); |
ioTRACE(2); |
Line 189 thrSession(struct tagSession *sess)
|
Line 214 thrSession(struct tagSession *sess)
|
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
|
|
/* dispatch message type */ |
/* dispatch message type */ |
|
if (mqtt_srv_Dispatch(sess->sess_srv, sess)) |
|
ioLIBERR(mqtt); |
switch (hdr->mqtt_msg.type) { |
switch (hdr->mqtt_msg.type) { |
case MQTT_TYPE_CONNECT: |
case MQTT_TYPE_CONNECT: |
ioDEBUG(5, "Exec CONNECT session"); |
ioDEBUG(5, "Exec CONNECT session"); |
pthread_mutex_lock(&mtx_sess); |
|
TAILQ_REMOVE(&Sessions, sess, sess_node); |
|
pthread_mutex_unlock(&mtx_sess); |
|
|
|
if (call.FiniSessPUB) |
|
call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); |
|
|
|
while ((store = SLIST_FIRST(&sess->sess_sndqueue))) { |
|
SLIST_REMOVE_HEAD(&sess->sess_sndqueue, st_node); |
|
free(store); |
|
} |
|
|
|
if (sess->sess_will.msg) |
|
free(sess->sess_will.msg); |
|
if (sess->sess_will.topic) |
|
free(sess->sess_will.topic); |
|
|
|
if ((v = io_allocVar())) { |
if ((v = io_allocVar())) { |
AIT_SET_STR(v, sess->sess_addr); |
AIT_SET_STR(v, sess->sess_addr); |
if (!schedEvent(root, startSession, v, (u_long) sess->sess_sock, sess, ret)) |
if (!schedEvent(root, startSession, v, (u_long) sess->sess_sock, sess, ret)) |
io_freeVar(v); |
io_freeVar(v); |
} else |
} else |
ioLIBERR(mqtt); |
ioLIBERR(mqtt); |
|
|
locKill ^= locKill; |
locKill ^= locKill; |
continue; |
continue; |
case MQTT_TYPE_DISCONNECT: |
case MQTT_TYPE_DISCONNECT: |
ioDEBUG(5, "Exec DISCONNECT session"); |
ioDEBUG(5, "Exec DISCONNECT session"); |
pthread_mutex_lock(&mtx_sess); |
|
TAILQ_REMOVE(&Sessions, sess, sess_node); |
|
pthread_mutex_unlock(&mtx_sess); |
|
|
|
finiSession(sess, 0); |
finiSession(sess, 0); |
locKill ^= locKill; |
locKill ^= locKill; |
continue; |
continue; |
case MQTT_TYPE_PUBLISH: |
case MQTT_TYPE_PUBLISH: |
ioDEBUG(5, "Work in progress ..."); | ioDEBUG(5, "Exec PUBLISH topic QoS=%d", hdr->mqtt_msg.qos); |
| /* |
| if (cmdPUBLISH(sess)) |
| locKill ^= locKill; |
| */ |
break; |
break; |
case MQTT_TYPE_PUBREL: |
case MQTT_TYPE_PUBREL: |
break; |
break; |
Line 264 startSession(sched_task_t *task)
|
Line 273 startSession(sched_task_t *task)
|
|
|
assert(task); |
assert(task); |
|
|
ioDEBUG(3, "task_Data=%p", TASK_DATA(task)); |
|
if (!TASK_DATA(task)) { |
if (!TASK_DATA(task)) { |
sess = initSession(TASK_FD(task), TASK_ARG(task)); |
sess = initSession(TASK_FD(task), TASK_ARG(task)); |
if (!sess) { |
if (!sess) { |
Line 282 startSession(sched_task_t *task)
|
Line 290 startSession(sched_task_t *task)
|
} else { |
} else { |
sess = TASK_DATA(task); |
sess = TASK_DATA(task); |
buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task); |
buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task); |
ioDEBUG(3, "debug:: sock=%d s=%p sbuf=%p sbl=%d ret=%d\n", sess->sess_sock, sess->sess_buf, sess->sess_buf->msg_base, (int) sess->sess_buf->msg_len, (int) TASK_DATLEN(task)); |
|
memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len); |
memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len); |
} |
} |
|
|
Line 378 static void *
|
Line 385 static void *
|
thrSched(void *arg __unused) |
thrSched(void *arg __unused) |
{ |
{ |
struct tagSession *sess; |
struct tagSession *sess; |
|
struct timespec pl = { 0, 10000000 }; |
|
|
ioTRACE(1); |
ioTRACE(1); |
|
|
|
schedPolling(root, &pl, NULL); |
schedRun(root, &Kill); |
schedRun(root, &Kill); |
|
|
TAILQ_FOREACH(sess, &Sessions, sess_node) |
TAILQ_FOREACH(sess, &Sessions, sess_node) |