iotivity 0.9.0
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-fan / lib / thread_mosq.c
1 /*
2 Copyright (c) 2011-2014 Roger Light <roger@atchoo.org>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9    this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14    contributors may be used to endorse or promote products derived from
15    this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "config.h"
31
32 #ifndef WIN32
33 #include <unistd.h>
34 #endif
35
36 #include "mosquitto_internal.h"
37 #include "net_mosq.h"
38
39 void *_mosquitto_thread_main(void *obj);
40
41 int mosquitto_loop_start(struct mosquitto *mosq)
42 {
43 #ifdef WITH_THREADING
44         if(!mosq || mosq->threaded) return MOSQ_ERR_INVAL;
45
46         mosq->threaded = true;
47         pthread_create(&mosq->thread_id, NULL, _mosquitto_thread_main, mosq);
48         return MOSQ_ERR_SUCCESS;
49 #else
50         return MOSQ_ERR_NOT_SUPPORTED;
51 #endif
52 }
53
54 int mosquitto_loop_stop(struct mosquitto *mosq, bool force)
55 {
56 #ifdef WITH_THREADING
57 #  ifndef WITH_BROKER
58         char sockpair_data = 0;
59 #  endif
60
61         if(!mosq || !mosq->threaded) return MOSQ_ERR_INVAL;
62
63
64         /* Write a single byte to sockpairW (connected to sockpairR) to break out
65          * of select() if in threaded mode. */
66         if(mosq->sockpairW != INVALID_SOCKET){
67 #ifndef WIN32
68                 if(write(mosq->sockpairW, &sockpair_data, 1)){
69                 }
70 #else
71                 send(mosq->sockpairW, &sockpair_data, 1, 0);
72 #endif
73         }
74         
75         if(force){
76                 pthread_cancel(mosq->thread_id);
77         }
78         pthread_join(mosq->thread_id, NULL);
79         mosq->thread_id = pthread_self();
80         mosq->threaded = false;
81
82         return MOSQ_ERR_SUCCESS;
83 #else
84         return MOSQ_ERR_NOT_SUPPORTED;
85 #endif
86 }
87
88 #ifdef WITH_THREADING
89 void *_mosquitto_thread_main(void *obj)
90 {
91         struct mosquitto *mosq = obj;
92
93         if(!mosq) return NULL;
94
95         pthread_mutex_lock(&mosq->state_mutex);
96         if(mosq->state == mosq_cs_connect_async){
97                 pthread_mutex_unlock(&mosq->state_mutex);
98                 mosquitto_reconnect(mosq);
99         }else{
100                 pthread_mutex_unlock(&mosq->state_mutex);
101         }
102
103         if(!mosq->keepalive){
104                 /* Sleep for a day if keepalive disabled. */
105                 mosquitto_loop_forever(mosq, mosq->keepalive*1000*86400, 1);
106         }else{
107                 /* Sleep for our keepalive value. publish() etc. will wake us up. */
108                 mosquitto_loop_forever(mosq, mosq->keepalive*1000, 1);
109         }
110
111         return obj;
112 }
113 #endif
114