// OCClient.cpp : Defines the entry point for the console application.
//
+#include "iotivity_config.h"
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+#ifdef HAVE_WINDOWS_H
+#include <Windows.h>
+#endif
#include <string>
+#include <map>
#include <cstdlib>
-#include <pthread.h>
#include <mutex>
#include <condition_variable>
-
#include "OCPlatform.h"
#include "OCApi.h"
using namespace OC;
+static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat";
+typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
+
+DiscoveredResourceMap discoveredResources;
std::shared_ptr<OCResource> curResource;
static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+static OCConnectivityType TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP;
+std::mutex curResourceLock;
class Light
{
return ++oc;
}
-void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
+void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
const int& eCode, const int& sequenceNumber)
{
- if(eCode == OC_STACK_OK)
+ try
{
- std::cout << "OBSERVE RESULT:"<<std::endl;
- std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
+ if(eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER)
+ {
+ if(sequenceNumber == OC_OBSERVE_REGISTER)
+ {
+ std::cout << "Observe registration action is successful" << std::endl;
+ }
- rep.getValue("state", mylight.m_state);
- rep.getValue("power", mylight.m_power);
- rep.getValue("name", mylight.m_name);
+ std::cout << "OBSERVE RESULT:"<<std::endl;
+ std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- std::cout << "\tstate: " << mylight.m_state << std::endl;
- std::cout << "\tpower: " << mylight.m_power << std::endl;
- std::cout << "\tname: " << mylight.m_name << std::endl;
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
- if(observe_count() > 30)
- {
- std::cout<<"Cancelling Observe..."<<std::endl;
- OCStackResult result = curResource->cancelObserve();
+ if(observe_count() == 11)
+ {
+ std::cout<<"Cancelling Observe..."<<std::endl;
+ OCStackResult result = curResource->cancelObserve();
- std::cout << "Cancel result: "<< result <<std::endl;
- sleep(10);
- std::cout << "DONE"<<std::endl;
- std::exit(0);
+ std::cout << "Cancel result: "<< result <<std::endl;
+ sleep(10);
+ std::cout << "DONE"<<std::endl;
+ std::exit(0);
+ }
+ }
+ else
+ {
+ if(eCode == OC_STACK_OK)
+ {
+ std::cout << "No observe option header is returned in the response." << std::endl;
+ std::cout << "For a registration request, it means the registration failed"
+ << std::endl;
+ }
+ else
+ {
+ std::cout << "onObserve Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
}
}
- else
+ catch(std::exception& e)
{
- std::cout << "onObserve Response error: " << eCode << std::endl;
- std::exit(-1);
+ std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
}
+
}
-void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPost2(const HeaderOptions& /*headerOptions*/,
+ const OCRepresentation& rep, const int eCode)
{
- if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+ try
{
- std::cout << "POST request was successful" << std::endl;
-
- if(rep.hasAttribute("createduri"))
- {
- std::cout << "\tUri of the created resource: "
- << rep.getValue<std::string>("createduri") << std::endl;
- }
- else
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
+ || eCode == OC_STACK_RESOURCE_CHANGED)
{
- rep.getValue("state", mylight.m_state);
- rep.getValue("power", mylight.m_power);
- rep.getValue("name", mylight.m_name);
+ std::cout << "POST request was successful" << std::endl;
- std::cout << "\tstate: " << mylight.m_state << std::endl;
- std::cout << "\tpower: " << mylight.m_power << std::endl;
- std::cout << "\tname: " << mylight.m_name << std::endl;
- }
+ if(rep.hasAttribute("createduri"))
+ {
+ std::cout << "\tUri of the created resource: "
+ << rep.getValue<std::string>("createduri") << std::endl;
+ }
+ else
+ {
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
- std::cout << endl << "Observe is used." << endl << endl;
- else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
- std::cout << endl << "ObserveAll is used." << endl << endl;
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
+ }
+
+ if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
+ std::cout << std::endl << "Observe is used." << std::endl << std::endl;
+ else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
+ std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
- curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
+ curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
+ }
+ else
+ {
+ std::cout << "onPost2 Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
}
- else
+ catch(std::exception& e)
{
- std::cout << "onPost2 Response error: " << eCode << std::endl;
- std::exit(-1);
+ std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
}
+
}
-void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPost(const HeaderOptions& /*headerOptions*/,
+ const OCRepresentation& rep, const int eCode)
{
- if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+ try
{
- std::cout << "POST request was successful" << std::endl;
-
- if(rep.hasAttribute("createduri"))
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
+ || eCode == OC_STACK_RESOURCE_CHANGED)
{
- std::cout << "\tUri of the created resource: "
- << rep.getValue<std::string>("createduri") << std::endl;
- }
- else
- {
- rep.getValue("state", mylight.m_state);
- rep.getValue("power", mylight.m_power);
- rep.getValue("name", mylight.m_name);
+ std::cout << "POST request was successful" << std::endl;
- std::cout << "\tstate: " << mylight.m_state << std::endl;
- std::cout << "\tpower: " << mylight.m_power << std::endl;
- std::cout << "\tname: " << mylight.m_name << std::endl;
- }
+ if(rep.hasAttribute("createduri"))
+ {
+ std::cout << "\tUri of the created resource: "
+ << rep.getValue<std::string>("createduri") << std::endl;
+ }
+ else
+ {
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- OCRepresentation rep2;
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
+ }
- std::cout << "Posting light representation..."<<std::endl;
+ OCRepresentation rep2;
- mylight.m_state = true;
- mylight.m_power = 55;
+ std::cout << "Posting light representation..."<<std::endl;
+
+ mylight.m_state = true;
+ mylight.m_power = 55;
- rep2.setValue("state", mylight.m_state);
- rep2.setValue("power", mylight.m_power);
+ rep2.setValue("state", mylight.m_state);
+ rep2.setValue("power", mylight.m_power);
- curResource->post(rep2, QueryParamsMap(), &onPost2);
+ curResource->post(rep2, QueryParamsMap(), &onPost2);
+ }
+ else
+ {
+ std::cout << "onPost Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
}
- else
+ catch(std::exception& e)
{
- std::cout << "onPost Response error: " << eCode << std::endl;
- std::exit(-1);
+ std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
}
}
}
// callback handler on PUT request
-void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
{
- if(eCode == OC_STACK_OK)
+ try
{
- std::cout << "PUT request was successful" << std::endl;
+ if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
+ {
+ std::cout << "PUT request was successful" << std::endl;
- rep.getValue("state", mylight.m_state);
- rep.getValue("power", mylight.m_power);
- rep.getValue("name", mylight.m_name);
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- std::cout << "\tstate: " << mylight.m_state << std::endl;
- std::cout << "\tpower: " << mylight.m_power << std::endl;
- std::cout << "\tname: " << mylight.m_name << std::endl;
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
- postLightRepresentation(curResource);
+ postLightRepresentation(curResource);
+ }
+ else
+ {
+ std::cout << "onPut Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
}
- else
+ catch(std::exception& e)
{
- std::cout << "onPut Response error: " << eCode << std::endl;
- std::exit(-1);
+ std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
}
}
}
// Callback handler on GET request
-void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
{
- if(eCode == OC_STACK_OK)
+ try
{
- std::cout << "GET request was successful" << std::endl;
- std::cout << "Resource URI: " << rep.getUri() << std::endl;
+ if(eCode == OC_STACK_OK)
+ {
+ std::cout << "GET request was successful" << std::endl;
+ std::cout << "Resource URI: " << rep.getUri() << std::endl;
- rep.getValue("state", mylight.m_state);
- rep.getValue("power", mylight.m_power);
- rep.getValue("name", mylight.m_name);
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- std::cout << "\tstate: " << mylight.m_state << std::endl;
- std::cout << "\tpower: " << mylight.m_power << std::endl;
- std::cout << "\tname: " << mylight.m_name << std::endl;
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
- putLightRepresentation(curResource);
+ putLightRepresentation(curResource);
+ }
+ else
+ {
+ std::cout << "onGET Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
}
- else
+ catch(std::exception& e)
{
- std::cout << "onGET Response error: " << eCode << std::endl;
- std::exit(-1);
+ std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
}
}
// Callback to found resources
void foundResource(std::shared_ptr<OCResource> resource)
{
- if(curResource)
- {
- std::cout << "Found another resource, ignoring"<<std::endl;
- }
-
+ std::cout << "In foundResource\n";
std::string resourceURI;
std::string hostAddress;
try
{
+ {
+ std::lock_guard<std::mutex> lock(curResourceLock);
+ if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
+ {
+ std::cout << "Found resource " << resource->uniqueIdentifier() <<
+ " for the first time on server with ID: "<< resource->sid()<<std::endl;
+ discoveredResources[resource->uniqueIdentifier()] = resource;
+ }
+ else
+ {
+ std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
+ }
+
+ if(curResource)
+ {
+ std::cout << "Found another resource, ignoring"<<std::endl;
+ return;
+ }
+ }
+
// Do some operations with resource object.
if(resource)
{
if(resourceURI == "/a/light")
{
- curResource = resource;
- // Call a local function which will internally invoke get API on the resource pointer
- getLightRepresentation(resource);
+ if (resource->connectivityType() & TRANSPORT_TYPE_TO_USE)
+ {
+ curResource = resource;
+ // Get the resource host address
+ std::cout << "\tAddress of selected resource: " << resource->host() << std::endl;
+
+ // Call a local function which will internally invoke get API on the resource pointer
+ getLightRepresentation(resource);
+ }
}
}
else
}
catch(std::exception& e)
{
- //log(e.what());
+ std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
}
}
-void PrintUsage()
+void printUsage()
{
std::cout << std::endl;
- std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
+ std::cout << "---------------------------------------------------------------------\n";
+ std::cout << "Usage : simpleclient <ObserveType> <TransportType>" << std::endl;
std::cout << " ObserveType : 1 - Observe" << std::endl;
std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
+ std::cout << " TransportType : 1 - IP" << std::endl;
+ std::cout << " TransportType : 2 - TCP" << std::endl;
+ std::cout << "---------------------------------------------------------------------\n\n";
}
-int main(int argc, char* argv[]) {
- if (argc == 1)
+void checkObserverValue(int value)
+{
+ if (value == 1)
{
OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+ std::cout << "<===Setting ObserveType to Observe===>\n\n";
}
- else if (argc == 2)
+ else if (value == 2)
{
- int value = atoi(argv[1]);
- if (value == 1)
- OBSERVE_TYPE_TO_USE = ObserveType::Observe;
- else if (value == 2)
- OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
- else
- OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+ OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
+ std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
+ }
+ else
+ {
+ std::cout << "<===Invalid ObserveType selected."
+ <<" Setting ObserveType to Observe===>\n\n";
+ }
+}
+
+void checkTransportValue(int value)
+{
+ if (1 == value)
+ {
+ TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP;
+ std::cout << "<===Setting TransportType to IP===>\n\n";
+ }
+ else if (2 == value)
+ {
+ TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_TCP;
+ std::cout << "<===Setting TransportType to TCP===>\n\n";
}
else
{
- PrintUsage();
+ std::cout << "<===Invalid TransportType selected."
+ <<" Setting TransportType to IP===>\n\n";
+ }
+}
+
+static FILE* client_open(const char* /*path*/, const char *mode)
+{
+ return fopen(SVR_DB_FILE_NAME, mode);
+}
+
+int main(int argc, char* argv[]) {
+
+ std::ostringstream requestURI;
+ OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink, NULL, NULL};
+ try
+ {
+ printUsage();
+ if (argc == 1)
+ {
+ std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
+ }
+ else if (argc == 2)
+ {
+ checkObserverValue(std::stoi(argv[1]));
+ }
+ else if (argc == 3)
+ {
+ checkObserverValue(std::stoi(argv[1]));
+ checkTransportValue(std::stoi(argv[2]));
+ }
+ else
+ {
+ std::cout << "<===Invalid number of command line arguments===>\n\n";
+ return -1;
+ }
+ }
+ catch(std::exception& )
+ {
+ std::cout << "<===Invalid input arguments===>\n\n";
return -1;
}
// Create PlatformConfig object
PlatformConfig cfg {
OC::ServiceType::InProc,
- OC::ModeType::Client,
- "0.0.0.0",
- 0,
- OC::QualityOfService::LowQos
+ OC::ModeType::Both,
+ OCConnectivityType::CT_ADAPTER_IP,
+ OCConnectivityType::CT_ADAPTER_IP,
+ (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP),
+ OC::QualityOfService::HighQos,
+ &ps
};
OCPlatform::Configure(cfg);
// makes it so that all boolean values are printed as 'true/false' in this stream
std::cout.setf(std::ios::boolalpha);
// Find all resources
-#ifdef CA_INT
- OCConnectivityType connectivityType = OC_WIFI;
- OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light",
- connectivityType, &foundResource);
-#else
- OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource);
-#endif
+ requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
+
+ OCPlatform::findResource("", requestURI.str(),
+ CT_DEFAULT, &foundResource);
std::cout<< "Finding Resource... " <<std::endl;
+ // Find resource is done twice so that we discover the original resources a second time.
+ // These resources will have the same uniqueidentifier (yet be different objects), so that
+ // we can verify/show the duplicate-checking code in foundResource(above);
+ OCPlatform::findResource("", requestURI.str(),
+ CT_DEFAULT, &foundResource);
+ std::cout<< "Finding Resource for second time..." << std::endl;
+
// A condition variable will free the mutex it is given, then do a non-
// intensive block until 'notify' is called on it. In this case, since we
// don't ever call cv.notify, this should be a non-processor intensive version
}catch(OCException& e)
{
- //log(e.what());
+ oclog() << "Exception in main: "<<e.what();
}
return 0;
}
+