From 1576b49f8636a869d77183daa640f46cf3618690 Mon Sep 17 00:00:00 2001 From: Jesse Williamson Date: Fri, 29 Aug 2014 16:53:30 -0700 Subject: [PATCH] Checkpoint: monoprocess bug fixed-- need to improve CLI handling. Change-Id: I73ad330df3716cda2f5907a62dc09478bc03fe5e --- OCLib/OCPlatform.cpp | 3 +- OCLib/OCProperties.cpp | 2 + examples/ocicuc/Makefile | 33 +++--- examples/ocicuc/monoprocess.cpp | 234 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+), 21 deletions(-) create mode 100644 examples/ocicuc/monoprocess.cpp diff --git a/OCLib/OCPlatform.cpp b/OCLib/OCPlatform.cpp index 1007fa6..e40c40d 100644 --- a/OCLib/OCPlatform.cpp +++ b/OCLib/OCPlatform.cpp @@ -43,9 +43,8 @@ namespace OC init(m_cfg); } - OCPlatform::~OCPlatform(void) + OCPlatform::~OCPlatform() { - std::cout << "platform destructor called" << std::endl; } OCStackResult OCPlatform::notifyObservers(OCResourceHandle resourceHandle) diff --git a/OCLib/OCProperties.cpp b/OCLib/OCProperties.cpp index c792271..e69a29d 100644 --- a/OCLib/OCProperties.cpp +++ b/OCLib/OCProperties.cpp @@ -23,6 +23,7 @@ using OC::OCReflect::property_type; +// Prettyprinters: namespace OC { namespace OCReflect { std::ostream& operator<<(std::ostream& os, const property_type& pt) @@ -165,6 +166,7 @@ OC::OCReflect::tagged_property tag_rep(const property_type pt, const SeqIterT& b }}}} // namespace OC::OCReflect::to_property::detail +// Convert concrete types into tagged properties (a representation with a typetag): namespace OC { namespace OCReflect { namespace to_property { OC::OCReflect::tagged_property convert(const bool& in) diff --git a/examples/ocicuc/Makefile b/examples/ocicuc/Makefile index bc79d11..aaf0fbc 100644 --- a/examples/ocicuc/Makefile +++ b/examples/ocicuc/Makefile @@ -1,18 +1,8 @@ -# override with `make BUILD=release` -# default to release build -BUILD := release -CXX := g++ -#CXX := clang -OUT_DIR := $(PWD)/$(BUILD) - -CXX_FLAGS.debug := -O0 -g3 -std=c++0x -Wall -pthread - -CXX_FLAGS.release := -O3 -std=c++0x -Wall -pthread # Please note that many of these settings are unused at the moment on account of the rush to get this out the door-- look carefully -# at the actual commands to see which are relevant (this makefile needs to be rewritten): +# at the actual commands to see which are relevant: -OC_CSDK=../../ccf-resource/csdk +OC_CSDK=../../../ccf-resource/csdk OC_STACK=$(OC_CSDK)/stack OC_OCCOAP=$(OC_CSDK)/occoap OC_SOCKET=$(OC_CSDK)/ocsocket @@ -21,14 +11,13 @@ OC_LOGGER=$(OC_CSDK)/logger OC_LIB=$(OC_CSDK)/liboctbstack.a OCLIB=../.. -OCLIB_LIB=$(OCLIB)/$(BUILD)/obj/OCLib.a #$(OCLIB)/release/obj/OCLib.a +OCLIB_LIB=../../$(BUILD)/obj/OCLib.a #$(OCLIB)/release/obj/OCLib.a BOOST_BASE=/usr/local/boost BOOST_INC=$(BOOST_BASE)/include BOOST_LIB=$(BOOST_BASE)/lib -CXX_CC=g++ -std=c++0x -O3 -#CXX_CC=g++ -std=c++0x -O0 -ggdb # debug +CXX_CC=g++ -std=c++0x -O0 -ggdb #CXX_CC=clang -std=c++0x -lstdc++ CXX_FLAGS=-Wall @@ -42,7 +31,8 @@ CXX=$(CXX_CC) $(CXX_FLAGS) $(CXX_INC) $(CXX_LIBS) .PHONY: init driver resources client server -all: init driver resources client server +all: init driver resources client server monoprocess + @echo Remember to \"export LD_LIBRARY_PATH=$(BOOST_LIB)\:\$$LD_LIBRARY_PATH\" init: rm -f *.o ./client ./server @@ -56,12 +46,17 @@ resources: client: $(CXX_CC) -pthread -c client.cpp -I../../include/ -I../../csdk/stack/include -I../../csdk/ocsocket/include -I../../csdk/ocrandom/include -I../../csdk/logger/include -I/usr/local/include/boost/ -I/usr/local/include $(CXX_INC) $(CXX_FLAGS) - $(CXX_CC) -std=c++0x -Wall -L/usr/local/boost/lib/ -I/usr/local/boost/include -pthread -o client driver.o utility.o client.o ../../$(BUILD)/obj/OCLib.a ../../csdk/$(BUILD)/liboctbstack.a -lboost_program_options $(CXX_INC) $(CXX_FLAGS) + $(CXX_CC) -std=c++0x -Wall -L/usr/local/boost/lib/ -I/usr/local/boost/include -pthread -o client driver.o utility.o client.o ../../$(BUILD)/obj/OCLib.a ../../csdk/release/liboctbstack.a -lboost_program_options $(CXX_INC) $(CXX_FLAGS) server: resources $(CXX_CC) -pthread -c server.cpp -I../../include/ -I../../csdk/stack/include -I../../csdk/ocsocket/include -I../../csdk/ocrandom/include -I../../csdk/logger/include -I/usr/local/include/boost/ -I/usr/local/include $(CXX_INC) - $(CXX_CC) -std=c++0x -Wall -L/usr/local/boost/lib/ -I/usr/local/boost/include -pthread -o server driver.o utility.o server.o light_resource.o ../../$(BUILD)/obj/OCLib.a ../../csdk/$(BUILD)/liboctbstack.a -lboost_program_options $(CXX_INC) + $(CXX_CC) -std=c++0x -Wall -L/usr/local/boost/lib/ -I/usr/local/boost/include -pthread -o server driver.o utility.o server.o light_resource.o ../../$(BUILD)/obj/OCLib.a ../../csdk/release/liboctbstack.a -lboost_program_options $(CXX_INC) + +monoprocess: driver resources + $(CXX_CC) -pthread -c monoprocess.cpp -I../../include/ -I../../csdk/stack/include -I../../csdk/ocsocket/include -I../../csdk/ocrandom/include -I../../csdk/logger/include -I/usr/local/include/boost/ -I/usr/local/include $(CXX_INC) + + $(CXX_CC) -std=c++0x -Wall -L/usr/local/boost/lib/ -I/usr/local/boost/include -pthread -o monoprocess driver.o utility.o monoprocess.o light_resource.o ../../$(BUILD)/obj/OCLib.a ../../csdk/release/liboctbstack.a -lboost_program_options $(CXX_INC) clean: - rm -f server.o client.o driver.o utility.o light_resource.o server client + rm -f server.o client.o driver.o monoprocess.o server client monoprocess diff --git a/examples/ocicuc/monoprocess.cpp b/examples/ocicuc/monoprocess.cpp new file mode 100644 index 0000000..b101cf2 --- /dev/null +++ b/examples/ocicuc/monoprocess.cpp @@ -0,0 +1,234 @@ +//****************************************************************** +// +// Copyright 2014 Intel Corporation All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "OCApi.h" +#include "OCResource.h" +#include "OCPlatform.h" + +#include "exec.hpp" +#include "utility.hpp" + +#include "demo_client.hpp" +#include "light_resource.hpp" + +namespace Intel { namespace OCDemo { + +auto make_description() + -> boost::program_options::options_description +{ + using std::string; + + namespace po = boost::program_options; + + po::options_description desc("Monoprocess Client/Server options"); + +std::cout << "JFW: TODO: separate IP/port/etc. for server and client!\n"; + desc.add_options() + ("nres", po::value()->default_value(1), "number of resources to use for testing") + ("host_ip", po::value()->default_value("134.134.161.33"), "IP of host") + ("host_port", po::value()->default_value(5683), "port of host") + ("interface", po::value()->default_value("eth0"), "network interface name") + ("uri", po::value>(), "resource URI") + ; + + return desc; +} + +/* Unfortunately, our target compiler may not support std::async correctly, so this +leverages RAII to save us from having to use the double-join-check pattern: */ +struct simple_join_guard +{ + thread t; + + template + simple_join_guard(FnT&& fn) + : t(fn) + {} + + ~simple_join_guard() + { + if(t.joinable()) + t.join(); + } +}; + +struct client_t +{ + const boost::program_options::variables_map m_vm; + std::shared_ptr m_platform_ptr; + OC::OCPlatform& m_platform; // reference to *m_platform_ptr + + atomic& quit_flag; + + vector resource_URIs; + + public: + client_t(const boost::program_options::variables_map vm, + std::shared_ptr platform_ptr, + atomic& quit_flag_) + : m_vm(vm), + m_platform_ptr(platform_ptr), + m_platform(*m_platform_ptr), + quit_flag(quit_flag_) + {} + + public: + void init() + { + if(0 != m_vm.count("uri")) + { + const vector& input_URIs = m_vm["uri"].as< vector >(); + copy(begin(input_URIs), end(input_URIs), back_inserter(resource_URIs)); + } + else + resource_URIs.push_back("coap://224.0.1.187/oc/core"); + } + + void operator()() + { + std::cout << "Requesting " << resource_URIs.size() << " URIs:\n"; + + for(const auto& resource_URI : resource_URIs) + cout << resource_URI << '\n'; + + Intel::OCDemo::client::resource_handler resources(m_platform, resource_URIs); + + // Register callbacks and wait for resources: + resources.find_resources(); + + // Allow the client to receive events from the server: + while(!quit_flag) + { + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + } +}; + +struct server_t +{ + const boost::program_options::variables_map m_vm; + std::shared_ptr m_platform_ptr; + OC::OCPlatform& m_platform; + + atomic& quit_flag; + + unsigned long m_nresources; + + vector resource_URIs; + + vector> lights; + + public: + server_t(const boost::program_options::variables_map& vm, + std::shared_ptr platform, + atomic& quit_flag_) + : m_vm(vm), + m_platform_ptr(platform), + m_platform(*m_platform_ptr), + quit_flag(quit_flag_) + { + m_nresources = vm["nres"].as(); + } + + public: + void init(); + + void operator()() + { + while(!quit_flag) + { + std::this_thread::sleep_for(std::chrono::seconds(5)); + } + } +}; + +void server_t::init() +{ + lights.resize(m_nresources); + + for(unsigned int resource_number = 1; m_nresources >= resource_number; resource_number++) + { + cout << "Registering resource " << resource_number << ": " << std::flush; + + auto lr = make_shared(); + + lr->createResource(m_platform, resource_number); + lr->addType(m_platform, "core.brightlight"); + lr->addInterface(m_platform, "oc.mi.ll"); + + lights.push_back(lr); + + cout << "Ok." << std::endl; + } +} + +int exec(const boost::program_options::variables_map& vm) +{ + using namespace std; + + std::cout << "Starting platform: " << std::flush; + + auto platform = make_shared(OC::PlatformConfig { + OC::ServiceType::InProc, // in-process server + OC::ModeType::Both, // run in client/server mode + vm["host_ip"].as(), // host + vm["host_port"].as(), // port + OC::QualityOfService::NonConfirmable + }); + + std::cout << "Ok." << std::endl; + + std::atomic quit_flag; + + server_t server(vm, platform, quit_flag); + client_t client(vm, platform, quit_flag); + +std::cout << "JFW: TODO: don't need to separate these any more\n"; + server.init(); + client.init(); + + // Run the server and client: + { + simple_join_guard server_guard(server); + simple_join_guard client_guard(client); + + // Note that at present nothing makes this true: + while(!quit_flag) + { + // Perform app tasks + std::this_thread::sleep_for(std::chrono::seconds(2)); + } + } + + return 1; +} + +}} // namespace Intel::OCDemo + -- 2.7.4