// 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)
{
try
{
- if(eCode == OC_STACK_OK)
+ if(eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER)
{
- std::cout << "OBSERVE RESULT:"<<std::endl;
- std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
+ if(sequenceNumber == OC_OBSERVE_REGISTER)
+ {
+ std::cout << "Observe registration action is successful" << std::endl;
+ }
+ 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 << "\tpower: " << mylight.m_power << std::endl;
std::cout << "\tname: " << mylight.m_name << std::endl;
- if(observe_count() > 30)
+ if(observe_count() == 11)
{
std::cout<<"Cancelling Observe..."<<std::endl;
OCStackResult result = curResource->cancelObserve();
}
else
{
- std::cout << "onObserve Response error: " << eCode << std::endl;
- std::exit(-1);
+ 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);
+ }
}
}
catch(std::exception& e)
}
-void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPost2(const HeaderOptions& /*headerOptions*/,
+ const OCRepresentation& rep, const int eCode)
{
try
{
- if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
+ || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "POST request was successful" << std::endl;
}
if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
- std::cout << endl << "Observe is used." << endl << endl;
+ std::cout << std::endl << "Observe is used." << std::endl << std::endl;
else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
- std::cout << endl << "ObserveAll is used." << endl << endl;
+ std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
}
-void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPost(const HeaderOptions& /*headerOptions*/,
+ const OCRepresentation& rep, const int eCode)
{
try
{
- if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
+ || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "POST request was successful" << 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)
{
try
{
- if(eCode == OC_STACK_OK)
+ if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
{
std::cout << "PUT request was successful" << 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)
{
try
{
// Callback to found resources
void foundResource(std::shared_ptr<OCResource> resource)
{
- std::lock_guard<std::mutex> lock(curResourceLock);
- if(curResource)
- {
- std::cout << "Found another resource, ignoring"<<std::endl;
- return;
- }
-
+ 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
{
- PrintUsage();
+ 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
+ {
+ 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
- OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource);
+ 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;
}
+