Development of CoAP-HTTP Proxy
[platform/upstream/iotivity.git] / service / coap-http-proxy / samples / proxy_main.c
1 //******************************************************************
2 //
3 // Copyright 2016 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 #include "CoapHttpHandler.h"
21
22 #include <signal.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 int g_quitFlag = 0;
28
29 void handleSigInt(int signum);
30
31 /*
32 * This method is an entry point of CoAP-HTTP Proxy.
33 */
34
35 int main()
36 {
37     printf("CoAP-HTTP proxy is starting..\n");
38     OCStackResult result = OCInit(NULL, 0, OC_SERVER);
39     if (result != OC_STACK_OK)
40     {
41         printf("Failed starting proxy\n");
42         return 0;
43     }
44
45     if (CHPInitialize() != OC_STACK_OK)
46     {
47         printf("Failed to start proxy.\n");
48         OCStop();
49         return 0;
50     }
51
52     printf("Proxy started successfully.\n");
53
54     signal(SIGINT, handleSigInt);
55     while (!g_quitFlag)
56     {
57         if (OCProcess() != OC_STACK_OK)
58         {
59             CHPTerminate();
60             OCStop();
61             printf("OCStack process error\n");
62             return 0;
63         }
64     }
65
66     if (CHPTerminate() != OC_STACK_OK)
67     {
68         printf("CHPTerminate failed.\n");
69     }
70     else
71     {
72         printf("CHPTerminate success.\n");
73     }
74
75     OCStop();
76     printf("Exiting proxy main loop...\n");
77     return 0;
78
79 }
80
81 /*
82 * This is a signal handling function for SIGINT(CTRL+C).
83 * A Resource Directory handle the SIGINT signal for safe exit.
84 *
85 * @param[in] signal
86 *                 signal number of caught signal.
87 */
88 void handleSigInt(int signum)
89 {
90     if (signum == SIGINT)
91     {
92         g_quitFlag = 1;
93     }
94 }
95