enum lws_callback_reasons reason, void *user,
void *in, size_t len);
static void *pt_startConnect(void *arg);
+ static void get_ip_port_from_uri(string uri, string* dest_ip, int* dest_port);
Clients *clients; //=new Clients(this );
void disconnect(Result_Base *result1);
void registerCallback(string, void *, int);
void handleError(string, Error);
- void get_ip_port_from_uri(string uri);
string getapifromUri(string uri);
string getUID();
#ifndef __MSF_SEARCH_H__
#define __MSF_SEARCH_H__
-//#include"Service.h"
#include <list>
#include"SearchProvider.h"
#include <list>
#include <pthread.h>
#include <string>
#include"mDNSSearchProvider.h"
-//#include"MSFDSearchProvider.h"
-#define NUM_OF_THREADS 2
+#define NUM_OF_THREADS 3
#define MDNS_THREAD_NUMBER 0
#define MSFD_THREAD_NUMBER 1
+#define UPDATE_THREAD_NUMBER 2
using namespace std;
bool searching_now;
static mDNSSearchProvider provider1;
static MSFDSearchProvider provider2;
- //OnStartListener *onStartListener;
- //OnStopListener *onStopListener;
- //OnServiceFoundListener *onServiceFoundListener;
- //OnServiceLostListener *onServiceLostListener;
public:
+ static bool pt_update_start;
static pthread_t threads[NUM_OF_THREADS];
static int onStartNotified;
//static Search* getInstance();
void startDiscovery();
void stopDiscovery();
static bool addService(Service);
- bool removeService(Service);
+ static bool removeService(Service);
void removeAndNotify(Service);
void validateService(Service);
- //void setOnStartListener(OnStartListener *);
- //void setOnStopListener(OnStopListener);
- //void setOnServiceFoundListener(OnServiceFoundListener);
- //void setOnServiceLostListener(OnServiceLostListener);
Service getServiceById(string id);
void setSearchListener(SearchListener*);
void releaseSearchListener();
static void *pt_startMDNS(void *);
static void *pt_startMSFD(void *);
+ static void *pt_update_alivemap(void *arg);
};
#endif
bool isSearching();
void setSearchListener(Search *obj);
std::string getIP(std::string url);
+ void addService(Service service);
+ void push_in_alivemap(long ttl, string id , int service_type);
+ void updateAlive(long ttl, string id , int service_type);
+ static void reapServices();
protected:
bool searching;
void setServices(list<Service>);
- void addService(Service service);
- void removeService(Service service);
- void removeServiceAndNotify(Service service);
+ static void removeService(Service service);
+ static void removeServiceAndNotify(Service service);
void clearServices();
Service getServiceById(string id);
- Service getServiceByIp(string ip);
+ static Service getServiceByIp(string ip);
static map<string, ttl_info> aliveMap;
- void updateAlive(long ttl, string id , int service_type);
- void reapServices();
};
static int json_parse_service(const char *in, void *);
static Service create(ServiceInfo);
static Service create(map<string, string>);
- static void foreach_json_object(JsonObject *object, const gchar *key, JsonNode *node, gpointer user_data);
Channel *createChannel(string uri);
Application createApplication(string uri);
Application createApplication(string uri, string channelId);
// return 1;
}
n = 0;
- get_ip_port_from_uri(uri);
+ get_ip_port_from_uri(uri, &server_ip_address, &server_port);
string api = getapifromUri(uri);
api.append("channels/").append(ChannelID);
wsi_mirror = NULL;
}
-void Channel::get_ip_port_from_uri(string uri) {
+void Channel::get_ip_port_from_uri(string uri, string* dest_ip, int* dest_port) {
MSF_DBG("parsing uri : %s", uri.c_str());
unsigned int http_index = uri.find("http");
unsigned int ip_index = 0;
int itr = ip_index;
- server_ip_address.clear();
+ (*dest_ip).clear();
string port = "";
char now_c = uri.at(itr);
while ((now_c > 47 && now_c < 58) || (now_c == '.')) {
- server_ip_address.push_back(now_c);
+ if (dest_ip != NULL) {
+ (*dest_ip).push_back(now_c);
+ }
now_c = uri.at(++itr);
}
- MSF_DBG("set server_ip_address as : %s", server_ip_address.c_str());
-
if (now_c == ':') {
now_c = uri.at(++itr);
now_c = uri.at(++itr);
}
- server_port = atoi(port.c_str());
+ if (dest_port != NULL) {
+ *dest_port = atoi(port.c_str());
+ }
} else {
// there is no ':'.
// It means that server use default port.
if (is_https) {
//default https server port is 443.
- server_port = 443;
+ *dest_port = 443;
} else {
//default http server port is 80"
- server_port = 80;
+ *dest_port = 80;
}
}
-
- MSF_DBG("set server_port as : %d", server_port);
}
string Channel::getapifromUri(string uri) {
#include "Debug.h"
#include "Error.h"
#include "Result.h"
+#include "Channel.h"
#define MSGBUFSIZE 1000
#define MULTICAST_PORT 8001
#define MULTICAST_GROUP "224.0.0.7"
int MSFDSearchProvider::flag = 0;
long MSFDSearchProvider::ttl = 0;
+class ResultMSFDServiceCallback : public Result_Base
+{
+ public:
+ MSFDSearchProvider* MSFDSearchProvider_pointer = NULL;
+ long ttl;
+ string ip_id;
+ int provider_type;
+
+ void onSuccess(Service abc)
+ {
+ MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
+ MSFDSearchProvider_pointer->push_in_alivemap(ttl, ip_id, provider_type);
+ MSFDSearchProvider_pointer->addService(abc);
+ }
+
+ void onError(Error)
+ {
+ MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
+ }
+};
+
+
MSFDSearchProvider::MSFDSearchProvider()
{
MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
clearServices();
- receive = false;
createMSFD();
}
while (1) {
addrlen = sizeof(msf_server_addr);
if ((nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *) &msf_server_addr, &addrlen)) < 0) {
- receive = false;
- reapServices();
if (fd < 1)
return;
} else {
msgbuf[nbytes] = '\0';
- receive = true;
processReceivedMsg(msgbuf, nbytes);
}
}
void MSFDSearchProvider::processReceivedMsg(char *buf, int buflen)
{
- //dlog_print(DLOG_INFO, "MSF", "MSFD processReceivedMsg start");
- if (buf != NULL) {
- }
+ if (buf == NULL)
+ return;
- while(receive) {
- //dlog_print(DLOG_INFO, "MSF", "MSFD processReceivedMsg 1");
- reapServices();
- //dlog_print(DLOG_INFO, "MSF", "MSFD processReceivedMsg 2");
- string data;
- data.append(buf, buflen);
- json_parse(data.c_str());
+ dlog_print(DLOG_INFO, "MSF", "msfd = %s", buf);
- //dlog_print(DLOG_INFO, "MSF", "MSFD processReceivedMsg after parse");
+ json_parse(buf);
- if (state == TYPE_DISCOVER) {
- //dlog_print(DLOG_INFO, "MSF", "MSFD processReceivedMsg state = discover");
- continue;
- }
+ string ip;
+ int port = 0;
- if (id.length() > 0) {
- //dlog_print(DLOG_INFO, "MSF", "MSFD processReceivedMsg id length > 0");
- Service serv = getServiceById(id);
- Service *serv1 = &serv;
+ Channel::get_ip_port_from_uri(url, &ip, &port);
+ map<string,ttl_info>::iterator itr = aliveMap.find(ip);
- if (serv.getId() != "") {
- receive = false;
- }
+ if (state == TYPE_DISCOVER) {
+ dlog_print(DLOG_INFO, "MSF", "discover packet.");
+ return;
+ } else if (state == STATE_ALIVE || state == STATE_UP) {
+
+ if (itr == aliveMap.end()) {
+ ResultMSFDServiceCallback *rService = new ResultMSFDServiceCallback();
+ rService->MSFDSearchProvider_pointer = this;
+ rService->ttl = ttl;
+ rService->provider_type = MSFD;
+ rService->ip_id = ip;
+ dlog_print(DLOG_INFO, "MSF", "there is no ip %s in aliveMap. call getByURI", ip.c_str());
+ Service::getByURI(url, SERVICE_CHECK_TIMEOUT, rService);
+ } else {
+ dlog_print(DLOG_INFO, "MSF", "already exist. call updateAlive");
+ updateAlive(ttl, ip, MSFD);
+ }
+ } else if (state == STATE_DOWN) {
+ if (itr != aliveMap.end()) {
+ aliveMap.erase(ip);
+ }
- string ip;
- if (!url.empty()) {
- ip = getIP(url);
- }
+ Service serv = getServiceById(id);
- if (state == STATE_ALIVE || state == STATE_UP) {
- map<string,ttl_info>::iterator i=aliveMap.find(id);
- if (((serv.getId()).length() == 0)) {//&&(i==aliveMap.end()))
- static string tempid = getIP(url);
- updateAlive(ttl, tempid, MSFD);
- static map<string, ttl_info>* tempaliveMap=&aliveMap;
- static MSFDSearchProvider *MSFDSearchProvider_pointer = this;
-
- class ResultMSFDServiceCallback : public Result_Base
- {
- public:
- void onSuccess(Service abc)
- {
- MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
- MSFDSearchProvider_pointer->updateAlive(ttl, tempid, MSFD);
- //(*tempaliveMap)[tempid]=tempttl;
- MSFDSearchProvider_pointer->addService(abc);
- }
-
- void onError(Error)
- {
- MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
- tempaliveMap->erase(tempid);
- //aliveMap.erase(id);
- }
- };
- Result_Base *rService = NULL;
- ResultMSFDServiceCallback *r1Service = new ResultMSFDServiceCallback();
- rService = r1Service;
- Service::getByURI(url, SERVICE_CHECK_TIMEOUT, rService);
-
- // ........ critical bug
- //delete r1Service;
- //r1Service = rService = NULL;
- } else {
- updateAlive(ttl, ip, MSFD);
- }
- } else if ((serv1->getId() != "") && (state == STATE_DOWN)) {
- aliveMap.erase(id);
- removeServiceAndNotify(serv);
- }
- } else {
- receive = false;
+ if (serv.getId().length() != 0) {
+ removeServiceAndNotify(serv);
}
}
- //dlog_print(DLOG_INFO, "MSF", "MSFD processReceivedMsg end");
}
void MSFDSearchProvider::foreach_json_object(JsonObject *object, const gchar *key, JsonNode *node, gpointer user_data)
//if (!searching) {
// return false;
//}
- receive = false;
shutdown(fd, SHUT_RDWR);
close(fd);
fd = 0;
list<Search*> Search::search_list;
list<Service> Search::services;
int Search::onStartNotified;
+bool Search::pt_update_start = false;
int Search::numRunning;
void Search::st_onFound(Service service)
{
- dlog_print(DLOG_INFO, "MSF", "Search::onFound()");
+ dlog_print(DLOG_INFO, "MSF", "Search::st_onFound()");
MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
addService(service);
list<Search*>::iterator itr;
- dlog_print(DLOG_INFO, "MSF", "Search::onFound() list size = %d", search_list.size());
+ dlog_print(DLOG_INFO, "MSF", "Search::st_onFound() list size = %d", search_list.size());
for (itr = search_list.begin(); itr != search_list.end(); itr++) {
if ((*itr)->searchListener != NULL && (*itr)->searching_now) {
MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
//validateService(service);
+ removeService(service);
+
list<Search*>::iterator itr;
for (itr = search_list.begin(); itr != search_list.end(); itr++) {
return NULL;
}
+void *Search::pt_update_alivemap(void *arg)
+{
+ pt_update_start = true;
+
+ while (pt_update_start) {
+ sleep(5);
+ dlog_print(DLOG_INFO, "MSF", "call reap");
+ SearchProvider::reapServices();
+ }
+}
+
void Search::startDiscovery()
{
dlog_print(DLOG_INFO, "MSF", "Search::startDiscovery()");
ret = pthread_create(&threads[MSFD_THREAD_NUMBER], NULL, pt_startMSFD, NULL);
dlog_print(DLOG_INFO, "MSF", "MSFD thread created");
-
if (ret == -1)
cout << "Fail to create MSFD search provider\n";
+ ret = pthread_create(&threads[UPDATE_THREAD_NUMBER], NULL, pt_update_alivemap, NULL);
+ if (ret == -1)
+ cout << "Fail to create pthread_update_alivemap\n";
+
onStart();
}
bool stop = false;
stop = provider1.stop();
stop = provider2.stop();
+ pt_update_start = false;
if (stop)
onStop();
MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n", __FUNCTION__, __LINE__, __FILE__);
- pthread_cancel(threads[1]);
- pthread_join(threads[1], NULL);
+ pthread_cancel(threads[MSFD_THREAD_NUMBER]);
+ pthread_join(threads[MSFD_THREAD_NUMBER], NULL);
+
+ pthread_cancel(threads[UPDATE_THREAD_NUMBER]);
+ pthread_join(threads[UPDATE_THREAD_NUMBER], NULL);
- dlog_print(DLOG_INFO, "MSF", "MDNS MSFD thread joined");
+ dlog_print(DLOG_INFO, "MSF", "MSFD thread joined");
+ dlog_print(DLOG_INFO, "MSF", "update alivemap thread joined");
dlog_print(DLOG_INFO, "MSF", "Search::stop discovery end");
}
long now = time(0);
MSF_DBG("Expired => msfd : %d, mdns : %d", msfd_ttl < now, mdns_ttl < now);
+ dlog_print(DLOG_INFO, "MSF", "Expired => msfd : %d, mdns : %d", msfd_ttl < now, mdns_ttl < now);
if (msfd_ttl < now && mdns_ttl < now)
return true;
else
return false;
}
+void SearchProvider::push_in_alivemap(long ttl, string id, int service_type)
+{
+ MSF_DBG("updateAlive : ttl = %d, id = %s, service_type = %d", ttl, id.c_str(), service_type);
+
+ if (id.empty()) {
+ return;
+ }
+
+ if (aliveMap.find(id) == aliveMap.end()) {
+ ttl_info info;
+ info.update_ttl(time(0) + ttl, service_type);
+ aliveMap[id] = info;
+ } else {
+ updateAlive(ttl, id, service_type);
+ }
+}
+
void SearchProvider::updateAlive(long ttl, string id, int service_type)
{
MSF_DBG("updateAlive : ttl = %d, id = %s, service_type = %d", ttl, id.c_str(), service_type);
if (id.empty()) {
return;
}
- long _ttl=time(0) + ttl;
- ttl_info info = aliveMap[id];
- MSF_DBG("mdns ttl : %d , msfd ttl : %d", info.get_ttl(MDNS), info.get_ttl(MSFD));
- info.update_ttl(_ttl, service_type);
- aliveMap[id]=info;
+
+ if (aliveMap.find(id) == aliveMap.end()) {
+ } else {
+ long _ttl=time(0) + ttl;
+
+ ttl_info info = aliveMap[id];
+
+ MSF_DBG("mdns ttl : %d , msfd ttl : %d", info.get_ttl(MDNS), info.get_ttl(MSFD));
+ info.update_ttl(_ttl, service_type);
+ aliveMap[id]=info;
+ }
}
void SearchProvider::reapServices()
}
}
-void Service::foreach_json_object(JsonObject *object, const gchar *key, JsonNode *node, gpointer user_data)
-{
- if (json_node_get_node_type(node) == JSON_NODE_VALUE) {
- if (!strncmp(key, "id", 2)) {
- serviceval.infoId = json_node_get_string(node);
- dlog_print(DLOG_ERROR, "MSF", "infoId set as %s", serviceval.infoId.c_str());
- //cout<<"\nID : \t"<<serviceval.infoId <<"\n";
- } else if (!strncmp(key, "version", 4)) {
- serviceval.infoVersion = json_node_get_string(node);
- dlog_print(DLOG_ERROR, "MSF", "infoVersion set as %s", serviceval.infoVersion.c_str());
- } else if (!strncmp(key, "name", 7)) {
- serviceval.infoName = json_node_get_string(node);
- dlog_print(DLOG_ERROR, "MSF", "infoName set as %s", serviceval.infoName.c_str());
- } else if (!strncmp(key, "type", 7)) {
- serviceval.infotype = json_node_get_string(node);
- dlog_print(DLOG_ERROR, "MSF", "infotype set as %s", serviceval.infotype.c_str());
-
- } else if (!strncmp(key, "uri", 7)) {
- serviceval.infoURI = json_node_get_string(node);
- dlog_print(DLOG_ERROR, "MSF", "infoURI set as %s", serviceval.infoURI.c_str());
- }
- } else if (json_node_get_node_type(node) == JSON_NODE_OBJECT) {
- if (!strncmp(key, "device", 7)) {
- MSF_DBG("\n Debug Log: SERVICE FOUND THROUGH WITH URI [%s] [%d] in %s \n", __FUNCTION__, __LINE__, __FILE__);
- if (user_data != NULL) {
- Device dev = Device::create(node);
- (static_cast<Result_Base *>(user_data))->onSuccess(dev);
- }
- }
- }
-}
-
-
int Service::json_parse_service(const char *in, void *ptr)
{
MSF_DBG("\n Debug Log: SERVICE FOUND THROUGH WITH URI [%s] [%d] in %s \n", __FUNCTION__, __LINE__, __FILE__);
JsonParser *parser = json_parser_new();
if (json_parser_load_from_data(parser, in, -1, NULL)) {
- JsonNode *node = json_parser_get_root(parser);
-
- if (json_node_get_node_type(node) == JSON_NODE_OBJECT) {
- json_object_foreach_member(json_node_get_object(node), foreach_json_object, ptr);
- }
-
} else {
dlog_print(DLOG_ERROR, "MSF", "json_parsing error");
}
- /*
- dlog_print(DLOG_INFO, "MSF", "Service json_parse_service()");
- void *result_ptr = ptr;
- enum json_type typed;
- json_object_object_foreach(jobj, key, val) {
- typed = json_object_get_type(val);
- switch (typed) {
- case json_type_null:
- break;
-
- case json_type_boolean:
- break;
-
- case json_type_double:
- break;
-
- case json_type_int:
- break;
-
- case json_type_object:
- dlog_print(DLOG_INFO, "MSF", "Service json_parse_service() 2");
- if (!strncmp(key , "device", 7)) {
- dlog_print(DLOG_INFO, "MSF", "Service json_parse_service() 3");
-
- MSF_DBG("\n Debug Log: SERVICE FOUND THROUGH WITH URI [%s] [%d] in %s \n", __FUNCTION__, __LINE__, __FILE__);
- if (result_ptr != NULL) {
- dlog_print(DLOG_INFO, "MSF", "Service json_parse_service() 4");
- MSF_DBG("\n Debug Log: SERVICE FOUND THROUGH WITH URI [%s] [%d] in %s \n", __FUNCTION__, __LINE__, __FILE__);
-
- Device dev = Device::create(val);
- (static_cast<Result_Base *> (result_ptr))->onSuccess(dev);
- result_ptr=NULL;
- }
- }
- break;
-
- case json_type_array:
- break;
-
- case json_type_string:
- if (!strncmp(key , "id", 2)) {
- serviceval.infoId = json_object_get_string(val);
- //cout<<"\nID : \t"<<serviceval.infoId <<"\n";
- }
- else if (!strncmp(key , "version", 4)) {
- serviceval.infoVersion = json_object_get_string(val);
- }
- else if (!strncmp(key , "name", 7)) {
- serviceval.infoName = json_object_get_string(val);
- }
- else if (!strncmp(key , "type", 7)) {
- serviceval.infotype = json_object_get_string(val);
- }
- else if (!strncmp(key , "uri", 7)) {
- serviceval.infoURI = json_object_get_string(val);
- }
- break;
+ JsonObject *root = json_node_get_object(json_parser_get_root(parser));
+
+ if (json_object_has_member(root, "id")) {
+ serviceval.infoId = json_object_get_string_member(root, "id");
+ dlog_print(DLOG_ERROR, "MSF", "infoId set as %s", serviceval.infoId.c_str());
+ }
+
+ if (json_object_has_member(root, "version")) {
+ serviceval.infoVersion = json_object_get_string_member(root, "version");
+ dlog_print(DLOG_ERROR, "MSF", "infoVersion set as %s", serviceval.infoVersion.c_str());
+ }
+
+ if (json_object_has_member(root, "name")) {
+ serviceval.infoName = json_object_get_string_member(root, "name");
+ dlog_print(DLOG_ERROR, "MSF", "infoName set as %s", serviceval.infoName.c_str());
+ }
+
+ if (json_object_has_member(root, "type")) {
+ serviceval.infotype = json_object_get_string_member(root, "type");
+ dlog_print(DLOG_ERROR, "MSF", "infotype set as %s", serviceval.infotype.c_str());
+ }
+
+ if (json_object_has_member(root, "uri")) {
+ serviceval.infoURI = json_object_get_string_member(root, "uri");
+ dlog_print(DLOG_ERROR, "MSF", "infoURI set as %s", serviceval.infoURI.c_str());
+ }
+
+ if (json_object_has_member(root, "device")) {
+ if (ptr != NULL) {
+ Device dev = Device::create(json_object_get_member(root, "device"));
+ (static_cast<Result_Base *>(ptr))->onSuccess(dev);
}
}
- */
if ((Resulturi!= NULL)) {
dlog_print(DLOG_INFO, "MSF", "json_parse_service() call onSuccess()");
MSF_DBG("\n [MSF : API] Debug log Function : [%s] and line [%d] in file [%s] \n",__FUNCTION__ ,__LINE__,__FILE__);
provider->addService(service);
string ip = provider->getIP(service.getUri());
- provider->updateAlive(0x00ffffff, ip, MDNS);
+ provider->push_in_alivemap(0x00ffffff, ip, MDNS);
MSF_DBG("service : id( %s ) registerd.", ip.c_str());
}