iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / examples / ocicuc / client.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 // This contains the Boost MPL defines required for the boost_variant
22 // serialization, so it must go before the boost/program_options
23 #include "OCApi.h"
24
25 #include <map>
26 #include <string>
27 #include <memory>
28 #include <utility>
29 #include <ostream>
30 #include <sstream>
31 #include <iostream>
32
33 #include <boost/program_options.hpp>
34
35 #include "OCResource.h"
36 #include "OCPlatform.h"
37
38 #include "exec.hpp"
39 #include "utility.hpp"
40
41 #include "demo_client.hpp"
42
43 namespace Intel { namespace OCDemo {
44
45 auto make_description()
46     -> boost::program_options::options_description
47 {
48  using std::string;
49  using std::vector;
50
51  namespace po = boost::program_options;
52
53  po::options_description desc("Client options");
54
55  desc.add_options()
56     ("nres",        po::value<unsigned long>()->default_value(1),           "number of resources to use for testing")
57     ("host_ip",     po::value<string>()->default_value("0.0.0.0"),   "IP of host")
58     ("host_port",   po::value<uint16_t>()->default_value(0),             "port of host")
59     ("interface",   po::value<string>()->default_value("eth0"),             "network interface name")
60     ("uri",         po::value<vector<string>>(),                            "remote resource URI")
61     ;
62
63  return desc;
64 }
65
66 int exec(const boost::program_options::variables_map& vm)
67 {
68  using namespace std;
69
70  OC::OCPlatform::Configure({
71                           OC::ServiceType::InProc,              // in-process server
72                           OC::ModeType::Client,                 // client mode
73                           vm["host_ip"].as<string>(),           // host
74                           vm["host_port"].as<uint16_t>(),       // port
75                           OC::QualityOfService::LowQos
76                         });
77
78  vector<string> resource_URIs;
79
80  if(0 == vm.count("uri"))
81   {
82     std::cout << "No URI specified, looking for everything in \"core\".\n";
83
84     // Find all resources:
85     resource_URIs.push_back("coap://224.0.1.187/oc/core");
86
87     /* Example of finding specific resources:
88     const auto& nprops = vm["nres"].as<unsigned long>();
89
90     for(unsigned long instance_number = 1;
91         instance_number <= nprops;
92         instance_number++)
93      {
94         ostringstream uri;
95
96
97         uri << "coap://" << vm["host_ip"].as<string>() << "/oc/core?rt=core.light" << '_' << instance_number;
98         resource_URIs.push_back(uri.str()); // ie. "coap://224.0.1.187/oc/core?rt=core.light_1");
99      }
100     */
101   }
102  else
103   {
104     const vector<string>& input_URIs = vm["uri"].as< vector<string> >();
105     copy(begin(input_URIs), end(input_URIs), back_inserter(resource_URIs));
106   }
107
108  std::cout << "Requesting " << resource_URIs.size() << " URIs:\n";
109
110  for(const auto& resource_URI : resource_URIs)
111   cout << resource_URI << '\n';
112
113  Intel::OCDemo::client::resource_handler resources(resource_URIs);
114
115  // Register callbacks and wait for resources:
116  resources.find_resources();
117
118  // Allow the client to receive events from the server:
119  for(;;)
120   ;
121
122  return 0;
123 }
124
125 }} // namespace Intel::OCDemo
126