#include <dlog.h>
#undef LOG_TAG
#define LOG_TAG "MSF_API"
-#define MSF_DBG(format, args...) SLOGD(format, ##args)
-#define MSF_ERR(format, args...) SLOGE(format, ##args)
+#define MSF_DBG(format, args...) (SLOGD(format, ##args))
+#define MSF_ERR(format, args...) (SLOGE(format, ##args))
#endif
#ifdef LINUX
#endif
#endif
-
result = result1;
int ret = curl_application_calling(uri);
if (ret == -1) {
- if (result1 != NULL)
+ if (result1 != NULL) {
result1->onError(Error::create("Failed to get info"));
+ }
}
}
{
Clients *clientstemp = Channel::getclients();
- if (clientstemp->getHost() == NULL)
+ if (clientstemp->getHost() == NULL) {
waitForOnReady = true;
+ }
map<string, string> params = getparams();
size_t Application::read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
- if (ptr != NULL)
+ if (ptr != NULL) {
static_cast<Application*>(stream)->curl_install_data.append(static_cast<const char*>(ptr), size*nmemb);
+ }
return size*nmemb;
}
curl_install_data.clear();
curl_easy_cleanup(curl);
- if (res != CURLE_OK)
+ if (res != CURLE_OK) {
return -1;
- else
+ } else {
return 0;
+ }
}
return -1;
curl_global_cleanup();
}
- if (res != CURLE_OK)
+ if (res != CURLE_OK) {
return -1;
- else
+ } else {
return 0;
+ }
}
void Application::setonInstallListener(OnInstallListener * listener)
Channel *Channel::create(Service *service, string uri) {
MSF_DBG("Channel::create()");
- if ((service == NULL) || uri.length() <= 0)
+ if ((service == NULL) || uri.length() <= 0) {
return NULL;
+ }
Channel *channel = new Channel(service, uri);
return channel;
}
err.append(p->errMsg);
err.append(" code:");
err.append(to_string(p->errcode));
- if (temp1)
+ if (temp1) {
temp1->onError(Error::create(err));
- if (p->onErrorListener)
+ }
+ if (p->onErrorListener) {
p->onErrorListener->onError();
+ }
}
}
}
}
Clients *Channel::getclients() {
- if (clients->size())
+ if (clients->size()) {
return clients;
- else
+ } else {
return NULL;
+ }
}
void Channel::setConnectionTimeout(long timeout) {
- if (timeout < 0)
+ if (timeout < 0) {
return;
- else if (timeout == 0)
+ } else if (timeout == 0) {
connectionHandler->stopPing();
- else
- {
+ } else {
connectionHandler->setPingTimeout(timeout);
- if (isWebSocketOpen())
+ if (isWebSocketOpen()) {
connectionHandler->startPing(this);
+ }
}
}
stringstream ss;
ss << code;
string str = ss.str();
- if (result1 != NULL)
+ if (result1 != NULL) {
result1->onError(Error::create(str));
+ }
} else {
if (resultobj != NULL) {
// string json=json_object_to_json_string(resultobj);
if (temp.first != NULL) {
if (temp.second == Result_Client) {
Result_Base *temp1 = (Result_Base *)(temp.first);
- if (temp1)
+ if (temp1) {
temp1->onError(err);
+ }
} else if (temp.second == Result_bool) {
Result_Base *temp1 = (Result_Base *)(temp.first);
- if (temp1)
+ if (temp1) {
temp1->onError(err);
+ }
}
- if (onErrorListener)
+ if (onErrorListener) {
onErrorListener->onError();
+ }
}
}
}
int status = pthread_create(&connect_thread, NULL, pt_startConnect,
(void *)pt_user_data);
- if (status)
+ if (status) {
return true;
- else
+ } else {
return false;
+ }
}
void Channel::disconnect() {
registerCallback(randID, (void *)result1, Result_Client);
string message = "";
- if (!isWebSocketOpen())
+ if (!isWebSocketOpen()) {
message = "Already Disconnected";
+ }
- if (disconnecting)
+ if (disconnecting) {
message = "Already Disconnecting";
+ }
if (message != "") {
handleError(UID, Error::create(message));
getcallback(randID);
while (1) {
- if (mirror_lifetime > 0)
+ if (mirror_lifetime > 0) {
mirror_lifetime--;
+ }
if (!mirror_lifetime) {
break;
onReadyCallbacks.erase(res);
doApplicationCallback(res);
}
- if (onConnectListener)
+ if (onConnectListener) {
onConnectListener->onConnect(clients->me());
+ }
- if (onReadyListener != NULL)
+ if (onReadyListener != NULL) {
onReadyListener->onReady();
+ }
}
void Channel::handleClientConnectMessage() {
MSF_DBG("handleClientConnectMessage");
- if (client.isHost())
+ if (client.isHost()) {
connected = true;
+ }
clients->add(client);
- if (onClientConnectListener != NULL)
+ if (onClientConnectListener != NULL) {
onClientConnectListener->onClientConnect(client);
+ }
}
void Channel::handleClientDisconnectMessage() {
temp = Channel::getcallback(UID);
if (temp.second == Result_Client) {
Result_Base *temp1 = (Result_Base *)(temp.first);
- if (temp1)
+ if (temp1) {
temp1->onSuccess(clienttemp);
+ }
} else if (temp.second == Result_bool) {
Result_Base *temp1 = (Result_Base *)(temp.first);
- if (temp1)
+ if (temp1) {
temp1->onSuccess(true);
+ }
}
me = clients->me();
- if (onConnectListener)
+ if (onConnectListener) {
onConnectListener->onConnect(clienttemp);
+ }
// To start channnel heath check
if (isWebSocketOpen()) {
// connectionHandler->startPing(this);
wsi_mirror = NULL;
connected = false;
was_closed = true;
- if ( Context )
+ if (Context) {
lws_cancel_service(Context);
+ }
- if (disconnecting)
+ if (disconnecting) {
disconnecting = false;
+ }
callbacks.clear();
clients->reset();
}
// loop until socket closed
while (n >= 0 && !was_closed) {
- if (was_closed)
+ if (was_closed) {
break;
+ }
if (wsi_mirror == NULL) {
wsi_mirror = lws_client_connect_via_info(&connect_info);
n = lws_service(Context, 0x0FFFFFFF);
- if (n < 0)
+ if (n < 0) {
break;
+ }
}
MSF_DBG("create_websocket destroy context");
void Channel::addOnMessageListener(string event,
OnMessageListener *onMessageListener) {
- if (event.length() == 0 || onMessageListener == NULL)
+ if (event.length() == 0 || onMessageListener == NULL) {
return;
+ }
if (messageListeners.find(event) == messageListeners.end()) {
messageListeners.insert({event, list<OnMessageListener *>()});
void Channel::removeAllMessageListener() { everyMessageListener = NULL; }
void Channel::removeOnMessageListeners(string event) {
- if (event.length() == 0)
+ if (event.length() == 0) {
return;
+ }
if (messageListeners.find(event) == messageListeners.end()) {
return;
void Channel::removeOnMessageListener(string event,
OnMessageListener *onMessageListener) {
- if ((event.length() == 0) || (onMessageListener != NULL))
+ if ((event.length() == 0) || (onMessageListener != NULL)) {
return;
+ }
if (messageListeners.find(event) == messageListeners.end()) {
return;
{
if (!isCommunicated) {
was_closed = true;
- if ( Context )
+ if (Context) {
lws_cancel_service(Context);
+ }
MSF_ERR("wsi destroyed with no communication");
if (onErrorListener) {
void Clients::reset()
{
clients.clear();
- if (host != NULL)
+ if (host != NULL) {
delete host;
+ }
host = NULL;
myClientId = "";
}
void MSFDSearchProvider::processReceivedMsg(char *buf, int buflen)
{
- if (buf == NULL)
+ if (buf == NULL) {
return;
+ }
if (buflen < 10) {
return;
void Result_Base::onSuccess(ApplicationInfo arg)
{
- if (appinfo_result_cb)
+ if (appinfo_result_cb) {
appinfo_result_cb(arg, user_data);
+ }
}
void Result_Base::onSuccess(Client arg)
{
- if (client_result_cb)
- client_result_cb(arg, user_data);
+ if (client_result_cb) {
+ client_result_cb(arg, user_data);
+ }
}
void Result_Base::onSuccess(Device arg)
{
- if (device_result_cb)
- device_result_cb(arg, user_data);
+ if (device_result_cb) {
+ device_result_cb(arg, user_data);
+ }
}
void Result_Base::onSuccess(bool arg)
{
- if (bool_result_cb)
- bool_result_cb(arg, user_data);
+ if (bool_result_cb) {
+ bool_result_cb(arg, user_data);
+ }
}
void Result_Base::onSuccess(Service arg)
{
- if (service_result_cb)
- service_result_cb(arg, user_data);
+ if (service_result_cb) {
+ service_result_cb(arg, user_data);
+ }
}
void Result_Base::onError(Error er)
{
- if (error_result_cb)
+ if (error_result_cb) {
error_result_cb(er, user_data);
+ }
}
if (search_ref_count == 0) {
stopDiscovery();
} else {
- if (searchListener != NULL)
+ if (searchListener != NULL) {
searchListener->onStop();
+ }
}
}
provider_mdns.start();
ret = pthread_create(&threads[MSFD_THREAD_NUMBER], NULL, pt_startMSFD, NULL);
- if (ret == -1)
+ if (ret == -1) {
MSF_DBG("MSFD thread create failed");
+ }
ret = pthread_create(&threads[UPDATE_THREAD_NUMBER], NULL, pt_update_alivemap, NULL);
- if (ret == -1)
+ if (ret == -1) {
MSF_DBG("update thread create failed");
+ }
onStart();
}
stop = provider_msfd.stop();
pt_update_start = false;
- if (stop)
+ if (stop) {
onStop();
+ }
pthread_join(threads[MSFD_THREAD_NUMBER], NULL);
ttl_info::ttl_info(long ttl, int service_type):msfd_ttl(0), mdns_ttl(0)
{
- if (service_type == MSFD)
+ if (service_type == MSFD) {
msfd_ttl = ttl;
- else if (service_type == MDNS)
+ } else if (service_type == MDNS) {
mdns_ttl = ttl;
+ }
}
ttl_info::ttl_info():msfd_ttl(0), mdns_ttl(0)
long ttl_info:: get_ttl(int service_type)
{
- if (service_type == MSFD)
+ if (service_type == MSFD) {
return msfd_ttl;
- else if (service_type == MDNS)
+ } else if (service_type == MDNS) {
return mdns_ttl;
- else
+ } else {
return -1;
+ }
}
void ttl_info::update_ttl(long ttl, int service_type)
MSF_DBG("msfd_remain_ttl = %d mdns_remain_ttl = %d", msfd_ttl - now, mdns_ttl - now);
- if (msfd_ttl < now && mdns_ttl < now)
+ if (msfd_ttl < now && mdns_ttl < now) {
return true;
- else
+ } else {
return false;
+ }
}
SearchProvider::SearchProvider()
bool SearchProvider::isSearching()
{
- if (this->searching)
+ if (this->searching) {
return true;
- else
+ } else {
return false;
+ }
}
void SearchProvider::push_in_alivemap(long ttl, string id, int service_type)
std::string SearchProvider::getIP(std::string url)
{
- if (url.empty())
+ if (url.empty()) {
return "";
+ }
std::string::size_type pos1 = 7;
std::string::size_type pos2 = url.find(":", 6);
}
if (service.getId().length() == 0) {
- if (result != NULL)
+ if (result != NULL) {
result->onError(Error::create("Service Not Found"));
+ }
} else {
- if (result != NULL)
+ if (result != NULL) {
result->onSuccess(service);
+ }
}
}
if ((res != CURLE_OK) || (read_data == NULL)) {
MSF_DBG("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
createdata_process("", dev_result_ptr);
- if (read_data)
+ if (read_data) {
free(read_data);
+ }
} else {
createdata_process(string(read_data), dev_result_ptr);
free(read_data);
*/
#include "URIparser.h"
+#include "Debug.h"
#include <stdio.h>
#include <cstdlib>
#include <cstring>
char c = *it++;
if (c == '%') {
if (it == end) {
- if (_flags2 & 0x1)
- printf("ERROR: URI encoding: no hex digit following percent sign.\n");
+ if (_flags2 & 0x1) {
+ MSF_ERR("ERROR: URI encoding: no hex digit following percent sign.\n");
+ }
return;
}
char hi = *it++;
if (it == end) {
- if (_flags2 & 0x1)
- printf("ERROR: URI encoding: two hex digits must follow percent sign.\n");
+ if (_flags2 & 0x1) {
+ MSF_ERR("ERROR: URI encoding: two hex digits must follow percent sign.\n");
+ }
return;
}
} else if (hi >= 'a' && hi <= 'f') {
c = hi - 'a' + 10;
} else {
- if (_flags2 & 0x1)
- printf("ERROR: URI encoding: not a hex digit.\n");
+ if (_flags2 & 0x1) {
+ MSF_ERR("ERROR: URI encoding: not a hex digit.\n");
+ }
return;
}
c *= 16;
} else if (lo >= 'a' && lo <= 'f') {
c += lo - 'a' + 10;
} else {
- if (_flags2 & 0x1)
- printf("ERROR: URI encoding: not a hex digit.\n");
+ if (_flags2 & 0x1) {
+ MSF_ERR("ERROR: URI encoding: not a hex digit.\n");
+ }
return;
}
}
unsigned short URIParse::getWellKnownPortforURI() const
{
- if (_URIscheme == "ftp")
+ if (_URIscheme == "ftp") {
return 21;
- else if (_URIscheme == "ssh")
+ } else if (_URIscheme == "ssh") {
return 22;
- else if (_URIscheme == "telnet")
+ } else if (_URIscheme == "telnet") {
return 23;
- else if (_URIscheme == "http")
+ } else if (_URIscheme == "http") {
return 80;
- else if (_URIscheme == "nntp")
+ } else if (_URIscheme == "nntp") {
return 119;
- else if (_URIscheme == "ldap")
+ } else if (_URIscheme == "ldap") {
return 389;
- else if (_URIscheme == "https")
+ } else if (_URIscheme == "https") {
return 443;
- else if (_URIscheme == "rtsp")
+ } else if (_URIscheme == "rtsp") {
return 554;
- else if (_URIscheme == "sip")
+ } else if (_URIscheme == "sip") {
return 5060;
- else if (_URIscheme == "sips")
+ } else if (_URIscheme == "sips") {
return 5061;
- else if (_URIscheme == "xmpp")
+ } else if (_URIscheme == "xmpp") {
return 5222;
- else
+ } else {
return 0;
+ }
}
void URIParse::parseURI(const std::string& uri)
{
std::string::const_iterator it = uri.begin();
std::string::const_iterator end = uri.end();
- if (it == end) return;
+ if (it == end) {
+ return;
+ }
if (*it != '/' && *it != '.' && *it != '?' && *it != '#') {
std::string scheme;
- while (it != end && *it != ':' && *it != '?' && *it != '#' && *it != '/') scheme += *it++;
+ while (it != end && *it != ':' && *it != '?' && *it != '#' && *it != '/') {
+ scheme += *it++;
+ }
if (it != end && *it == ':') {
++it;
if (it == end) {
- if (_URIflags & 0x1)
- printf("ERROR: URI scheme must be followed by authority or path.\n");
+ if (_URIflags & 0x1) {
+ MSF_ERR("ERROR: URI scheme must be followed by authority or path.\n");
+ }
return;
}
setURIScheme(scheme);
void URIParse::URIparseHostAndPort(std::string::const_iterator& it, const std::string::const_iterator& end)
{
- if (it == end) return;
+ if (it == end) {
+ return;
+ }
std::string host;
if (*it == '[') {
++it;
- while (it != end && *it != ']') host += *it++;
+ while (it != end && *it != ']') {
+ host += *it++;
+ }
if (it == end) {
- if (_URIflags & 0x1)
- printf("ERROR: unterminated IPv6 address.\n");
+ if (_URIflags & 0x1) {
+ MSF_ERR("ERROR: unterminated IPv6 address.\n");
+ }
return;
}
++it;
} else {
- while (it != end && *it != ':') host += *it++;
+ while (it != end && *it != ':') {
+ host += *it++;
+ }
}
if (it != end && *it == ':') {
++it;
std::string port;
- while (it != end) port += *it++;
+ while (it != end) {
+ port += *it++;
+ }
if (!port.empty()) {
int nport = 0;
void URIParse::URIparsePath(std::string::const_iterator& it, const std::string::const_iterator& end)
{
std::string path;
- while (it != end && *it != '?' && *it != '#') path += *it++;
+ while (it != end && *it != '?' && *it != '#') {
+ path += *it++;
+ }
decodeURI(path, _URIpath, _URIflags);
}
void URIParse::URIparsePathEtc(std::string::const_iterator& it, const std::string::const_iterator& end)
{
- if (it == end) return;
+ if (it == end) {
+ return;
+ }
- if (*it != '?' && *it != '#')
+ if (*it != '?' && *it != '#') {
URIparsePath(it, end);
+ }
if (it != end && *it == '?') {
++it;
void URIParse::URIparseQuery(std::string::const_iterator& it, const std::string::const_iterator& end)
{
_URIquery.clear();
- while (it != end && *it != '#') _URIquery += *it++;
+ while (it != end && *it != '#') {
+ _URIquery += *it++;
+ }
}
void URIParse::URIparseFragment(std::string::const_iterator& it, const std::string::const_iterator& end)
{
std::string fragment;
- while (it != end) fragment += *it++;
+ while (it != end) {
+ fragment += *it++;
+ }
decodeURI(fragment, _URIfragment, _URIflags);
}
const char *ptr = strrchr(str2, (int)'.');
if (ptr && *ptr == '.') {
++ptr;
- if (strchr("0123456789", *ptr))
+ if (strchr("0123456789", *ptr)) {
return 1;
+ }
}
return 0;
}
char *type = NULL;
rv = dnssd_service_get_name(remote_service, &name);
- if (rv == DNSSD_ERROR_NONE && name != NULL)
+ if (rv == DNSSD_ERROR_NONE && name != NULL) {
MSF_DBG("Service Name : %s", name);
+ }
MSF_DBG("State : ");
switch (service_state) {
- case DNSSD_SERVICE_STATE_AVAILABLE:
- MSF_DBG("Available");
- break;
- case DNSSD_SERVICE_STATE_UNAVAILABLE:
- {
- char *ip_v4_address = NULL;
- char *ip_v6_address = NULL;
-
- MSF_DBG("Un-Available");
- dnssd_service_get_ip(remote_service, &ip_v4_address, &ip_v6_address);
- if (ip_v4_address) {
- MSF_DBG("-> [%s]", ip_v4_address);
- provider->updateAlive(0, ip_v4_address, MDNS);
- provider->reapServices();
+ case DNSSD_SERVICE_STATE_AVAILABLE:
+ MSF_DBG("Available");
+ break;
+ case DNSSD_SERVICE_STATE_UNAVAILABLE:
+ {
+ char *ip_v4_address = NULL;
+ char *ip_v6_address = NULL;
- free(ip_v4_address);
- free(ip_v6_address);
- }
- return;
+ MSF_DBG("Un-Available");
+ dnssd_service_get_ip(remote_service, &ip_v4_address, &ip_v6_address);
+ if (ip_v4_address) {
+ MSF_DBG("-> [%s]", ip_v4_address);
+ provider->updateAlive(0, ip_v4_address, MDNS);
+ provider->reapServices();
+
+ free(ip_v4_address);
+ free(ip_v6_address);
}
- case DNSSD_SERVICE_STATE_NAME_LOOKUP_FAILED:
- MSF_DBG("Lookup failure for service name");
- break;
- case DNSSD_SERVICE_STATE_HOST_NAME_LOOKUP_FAILED:
- MSF_DBG("Lookup failure for host name and port number");
- break;
- case DNSSD_SERVICE_STATE_ADDRESS_LOOKUP_FAILED:
- MSF_DBG("Lookup failure for IP address");
- break;
- default:
- MSF_DBG("Unknown Browse State");
- break;
+ return;
+ }
+ case DNSSD_SERVICE_STATE_NAME_LOOKUP_FAILED:
+ MSF_DBG("Lookup failure for service name");
+ break;
+ case DNSSD_SERVICE_STATE_HOST_NAME_LOOKUP_FAILED:
+ MSF_DBG("Lookup failure for host name and port number");
+ break;
+ case DNSSD_SERVICE_STATE_ADDRESS_LOOKUP_FAILED:
+ MSF_DBG("Lookup failure for IP address");
+ break;
+ default:
+ MSF_DBG("Unknown Browse State");
+ break;
}
rv = dnssd_service_get_type(remote_service, &type);
- if (rv == DNSSD_ERROR_NONE && type != NULL)
+ if (rv == DNSSD_ERROR_NONE && type != NULL) {
MSF_DBG("Service Type : %s", type);
+ }
if (service_state == DNSSD_SERVICE_STATE_AVAILABLE) {
char *ip_v4_address = NULL;
rv = dnssd_service_get_ip(remote_service, &ip_v4_address, &ip_v6_address);
if (rv == DNSSD_ERROR_NONE) {
- if (ip_v4_address)
+ if (ip_v4_address) {
MSF_DBG("IPv4 Address : %s", ip_v4_address);
- if (ip_v6_address)
+ }
+ if (ip_v6_address) {
MSF_DBG("IPv6 Address : %s", ip_v6_address);
+ }
}
rv = dnssd_service_get_port(remote_service, &port);
void mDNSSearchProvider::start()
{
- if (!service_cb)
+ if (!service_cb) {
service_cb = new MDNSServiceCallback(this);
+ }
if (g_service != 0) {
dnssd_stop_browsing_service(g_service);
return true;
}
- if (rv >= 1)
+ if (rv >= 1) {
printf("\nOperation Succeeded!\n");
+ }
return TRUE;
}