There are Two modifications.
[platform/upstream/iotivity.git] / service / notification-manager / NotificationManager / src / NotificationManager.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 "NotificationManager.h"
22
23 NotificationManager *NotificationManager::s_instance = NULL;
24 mutex NotificationManager::s_mutexForCreation;
25
26 NotificationManager::NotificationManager()
27 {
28
29 }
30
31 NotificationManager::NotificationManager(HostingConfig cfg)
32 {
33
34 }
35
36 NotificationManager::~NotificationManager()
37 {
38
39 }
40
41 void NotificationManager::initialize()
42 {
43         // find local ip address
44     std::string ipAddress;
45     NotificationManager::getInstance()->scanAndGetNetworkInterface(ipAddress);
46
47     // set ip address
48     OICPlatformConfig::getInstance()->setIP(ipAddress);
49
50     // initialize hosting handler
51     HostingHandler::initialize();
52 }
53
54 void NotificationManager::initialize(HostingConfig cfg)
55 {
56         // find local ip address
57     std::string ipAddress;
58     NotificationManager::getInstance()->scanAndGetNetworkInterface(ipAddress);
59
60     // set ip address
61     OICPlatformConfig::getInstance()->setIP(ipAddress);
62
63     // initialize hosting handler
64     HostingHandler::initialize(cfg);
65 }
66
67 void NotificationManager::registerHostingEventListener()
68 {
69     // TODO : Initial HostingEventListener (v1.0)
70 }
71
72 NotificationManager *NotificationManager::getInstance()
73 {
74         if(!s_instance)
75         {
76                 s_mutexForCreation.lock();
77                 if(!s_instance)
78                 {
79                         s_instance = new NotificationManager();
80                 }
81                 s_mutexForCreation.unlock();
82         }
83
84     return s_instance;
85 }
86
87 int NotificationManager::getNetInfo(IN int& sck, IN struct ifreq* item, OUT std::string& ip_addres)
88 {
89         struct ifreq temp_ifr;
90         memset(&temp_ifr, 0, sizeof(temp_ifr));
91         strcpy(temp_ifr.ifr_name, item->ifr_name);
92
93         if (ioctl(sck, SIOCGIFFLAGS, &temp_ifr))
94         {
95                 return -1;
96         }
97
98         if (!((temp_ifr.ifr_flags & IFF_UP) && (temp_ifr.ifr_flags & IFF_RUNNING)))
99         {
100                 return -1;
101         }
102
103         std::string ip(inet_ntoa(((struct sockaddr_in *) &item->ifr_addr)->sin_addr));
104         if (ip.empty())
105         {
106                 return -1;
107         }
108
109         if (ip.find("127.0.0") == 0)
110         {
111                 return -1;
112         }
113
114         ip_addres = ip;
115         return 0;
116 }
117
118 bool NotificationManager::scanAndGetNetworkInterface(OUT std::string& ip_addres)
119 {
120         while(1)
121         {
122                 char buf[1024] =        { 0, };
123                 struct ifconf ifc;
124                 struct ifreq *ifr;
125                 int sck;
126                 int interfaces;
127                 int i;
128
129                 sck = socket(AF_INET, SOCK_DGRAM, 0);
130                 if (sck < 0)
131                 {
132                         usleep(10);
133                         continue;
134                 }
135
136                 ifc.ifc_len = sizeof(buf);
137                 ifc.ifc_buf = buf;
138                 if (ioctl(sck, SIOCGIFCONF, &ifc) < 0)
139                 {
140                         printf( "SIOCGIFCONF Failed ");
141                         close(sck);
142                         usleep(10);
143                         continue;
144                 }
145
146                 ifr = ifc.ifc_req;
147                 interfaces = ifc.ifc_len / sizeof(struct ifreq);
148
149                 for (i = 0; i < interfaces; i++)
150                 {
151                         if(  getNetInfo(sck, &ifr[i], ip_addres) == 0 )
152                         {
153                                 return 0;
154                         }
155                         continue;
156                 }
157                 close(sck);
158                 usleep(10);
159         }
160
161         return 0;
162 }