1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
32 #include "OCResource.h"
33 #include "OCPlatform.h"
36 #include "utility.hpp"
38 #include "demo_client.hpp"
39 #include "light_resource.hpp"
41 namespace Intel { namespace OCDemo {
43 auto make_description()
44 -> boost::program_options::options_description
48 namespace po = boost::program_options;
50 po::options_description desc("Monoprocess Client/Server options");
53 ("nres", po::value<unsigned long>()->default_value(1), "number of resources to use for testing")
54 ("host_ip", po::value<string>()->default_value("0.0.0.0"), "IP of host")
55 ("host_port", po::value<uint16_t>()->default_value(0), "port of host")
56 ("interface", po::value<string>()->default_value("eth0"), "network interface name")
57 ("uri", po::value<vector<string>>(), "resource URI")
63 /* Unfortunately, our target compiler may not support std::async correctly, so this
64 leverages RAII to save us from having to use the double-join-check pattern: */
65 struct simple_join_guard
70 simple_join_guard(FnT&& fn)
83 const boost::program_options::variables_map m_vm;
85 atomic<bool>& quit_flag;
87 vector<string> resource_URIs;
90 client_t(const boost::program_options::variables_map vm,
91 atomic<bool>& quit_flag_)
99 if(0 != m_vm.count("uri"))
101 const vector<string>& input_URIs = m_vm["uri"].as< vector<string> >();
102 copy(begin(input_URIs), end(input_URIs), back_inserter(resource_URIs));
105 resource_URIs.push_back("coap://224.0.1.187/oc/core");
110 std::cout << "Requesting " << resource_URIs.size() << " URIs:\n";
112 for(const auto& resource_URI : resource_URIs)
113 cout << resource_URI << '\n';
115 Intel::OCDemo::client::resource_handler resources(resource_URIs);
117 // Register callbacks and wait for resources:
118 resources.find_resources();
120 // Allow the client to receive events from the server:
123 std::this_thread::sleep_for(std::chrono::seconds(1));
130 const boost::program_options::variables_map m_vm;
132 atomic<bool>& quit_flag;
134 unsigned long m_nresources;
136 vector<string> resource_URIs;
138 vector<shared_ptr<Intel::OCDemo::LightResource>> lights;
141 server_t(const boost::program_options::variables_map& vm,
142 atomic<bool>& quit_flag_)
144 quit_flag(quit_flag_)
146 m_nresources = vm["nres"].as<unsigned long>();
156 std::this_thread::sleep_for(std::chrono::seconds(5));
161 void server_t::init()
163 lights.resize(m_nresources);
165 for(unsigned int resource_number = 1; m_nresources >= resource_number; resource_number++)
167 cout << "Registering resource " << resource_number << ": " << std::flush;
169 auto lr = make_shared<Intel::OCDemo::LightResource>();
171 lr->createResource(resource_number);
172 lr->addType("core.brightlight");
173 lr->addInterface("oc.mi.ll");
175 lights.push_back(lr);
177 cout << "Ok." << std::endl;
181 int exec(const boost::program_options::variables_map& vm)
185 std::cout << "Starting platform: " << std::flush;
187 OC::OCPlatform::Configure(OC::PlatformConfig {
188 OC::ServiceType::InProc,
189 OC::ModeType::Both, // run in client/server mode
190 vm["host_ip"].as<string>(), // host
191 vm["host_port"].as<uint16_t>(), // port
192 OC::QualityOfService::LowQos
194 std::cout << "Ok." << std::endl;
196 std::atomic<bool> quit_flag;
198 server_t server(vm, quit_flag);
199 client_t client(vm, quit_flag);
201 std::cout << "JFW: TODO: don't need to separate these any more\n";
205 // Run the server and client:
207 simple_join_guard server_guard(server);
208 simple_join_guard client_guard(client);
210 // Note that at present nothing makes this true:
214 std::this_thread::sleep_for(std::chrono::seconds(2));
221 }} // namespace Intel::OCDemo