1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NotificationManager.h"
23 NotificationManager *NotificationManager::s_instance = NULL;
24 mutex NotificationManager::s_mutexForCreation;
26 NotificationManager::NotificationManager()
31 NotificationManager::NotificationManager(HostingConfig cfg)
36 NotificationManager::~NotificationManager()
41 void NotificationManager::initialize()
43 // find local ip address
44 std::string ipAddress;
45 NotificationManager::getInstance()->scanAndGetNetworkInterface(ipAddress);
48 OICPlatformConfig::getInstance()->setIP(ipAddress);
50 // initialize hosting handler
51 HostingHandler::initialize();
54 void NotificationManager::initialize(HostingConfig cfg)
56 // find local ip address
57 std::string ipAddress;
58 NotificationManager::getInstance()->scanAndGetNetworkInterface(ipAddress);
61 OICPlatformConfig::getInstance()->setIP(ipAddress);
63 // initialize hosting handler
64 HostingHandler::initialize(cfg);
67 void NotificationManager::registerHostingEventListener()
69 // TODO : Initial HostingEventListener (v1.0)
72 NotificationManager *NotificationManager::getInstance()
76 s_mutexForCreation.lock();
79 s_instance = new NotificationManager();
81 s_mutexForCreation.unlock();
87 int NotificationManager::getNetInfo(IN int& sck, IN struct ifreq* item, OUT std::string& ip_addres)
89 struct ifreq temp_ifr;
90 memset(&temp_ifr, 0, sizeof(temp_ifr));
91 strcpy(temp_ifr.ifr_name, item->ifr_name);
93 if (ioctl(sck, SIOCGIFFLAGS, &temp_ifr))
98 if (!((temp_ifr.ifr_flags & IFF_UP) && (temp_ifr.ifr_flags & IFF_RUNNING)))
103 std::string ip(inet_ntoa(((struct sockaddr_in *) &item->ifr_addr)->sin_addr));
109 if (ip.find("127.0.0") == 0)
118 bool NotificationManager::scanAndGetNetworkInterface(OUT std::string& ip_addres)
122 char buf[1024] = { 0, };
129 sck = socket(AF_INET, SOCK_DGRAM, 0);
136 ifc.ifc_len = sizeof(buf);
138 if (ioctl(sck, SIOCGIFCONF, &ifc) < 0)
140 printf( "SIOCGIFCONF Failed ");
147 interfaces = ifc.ifc_len / sizeof(struct ifreq);
149 for (i = 0; i < interfaces; i++)
151 if( getNetInfo(sck, &ifr[i], ip_addres) == 0 )