Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / tizen / SimpleClientServer / ocrouting.cpp
1 /* ****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include <glib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <string>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <pthread.h>
29 #include <array>
30 #include <iostream>
31 #include "ocstack.h"
32 #include "logger.h"
33 #include "ocpayload.h"
34
35 using namespace std;
36
37 #define TAG "ocrouting"
38
39 int gQuitFlag = 0;
40 static GMainLoop *g_mainloop = NULL;
41 pthread_t g_thread;
42
43 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
44 void handleSigInt(int signum)
45 {
46     if (signum == SIGINT)
47     {
48         gQuitFlag = 1;
49     }
50 }
51
52 void *GMainLoopThread(void *param)
53 {
54
55     while (!gQuitFlag)
56     {
57         if (OCProcess() != OC_STACK_OK)
58         {
59             cout << "\nOCStack process error";
60             return NULL;
61         }
62 #ifndef ROUTING_GATEWAY
63         sleep(1);
64 #endif
65     }
66
67     if (g_mainloop)
68     {
69         g_main_loop_quit(g_mainloop);
70     }
71     return NULL;
72 }
73
74 int main()
75 {
76     cout << "\nOCRouting sample is starting...";
77     g_mainloop = g_main_loop_new(NULL, FALSE);
78     if(!g_mainloop)
79     {
80         printf("g_main_loop_new failed\n");
81         return 0;
82     }
83
84     if (OCInit(NULL, 0, OC_GATEWAY) != OC_STACK_OK)
85     {
86         cout << "\nOCStack init error";
87         return 0;
88     }
89
90     // Break from loop with Ctrl-C
91     cout << "\nEntering ocrouting main loop...";
92
93     signal(SIGINT, handleSigInt);
94
95
96     int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL);
97     if (result < 0)
98     {
99         printf("pthread_create failed in initialize\n");
100         return 0;
101     }
102
103     g_main_loop_run(g_mainloop);
104
105     cout << "\nExiting ocrouting main loop...";
106
107     if (OCStop() != OC_STACK_OK)
108     {
109         cout << "\nOCStack process error";
110     }
111
112     return 0;
113 }