Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / examples / ContainerSample.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 #include "RCSResourceContainer.h"
22 #include "RCSBundleInfo.h"
23 #include "oc_logger.hpp"
24 #include <iostream>
25
26 using namespace std;
27 using namespace OIC::Service;
28 using OC::oc_log_stream;
29
30 /* Annother way to create a context: */
31 auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
32 {
33     static OC::oc_log_stream ols(oc_make_ostream_logger);
34     static boost::iostreams::stream<OC::oc_log_stream> os(ols);
35
36     return os;
37 };
38
39 int main()
40 {
41     info_logger()->set_module("ContainerTest");
42     info_logger()->set_level(OC_LOG_INFO);
43
44     info_logger() << "Starting container test." << std::flush;
45
46     RCSResourceContainer *container = RCSResourceContainer::getInstance();
47     container->startContainer("examples/ResourceContainerConfig.xml");
48
49     /*so bundle add test*/
50     cout << "++++++++++++++++++++++++++" << endl;
51     cout << "+++ Test for SO Bundle +++" << endl;
52     cout << "++++++++++++++++++++++++++" << endl;
53     cout << "Press enter to add SO bundle " << endl;
54     getchar();
55     std::map<string, string> bundleParams;
56     container->addBundle("oic.bundle.hueSample", "", "libHueBundle.so", bundleParams);
57
58     std::list<RCSBundleInfo *> bundles = container->listBundles();
59     std::list<RCSBundleInfo *>::iterator bundleIt;
60
61     cout << "\t>>> bundle list size : " << bundles.size() << endl;
62     for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
63     {
64         cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
65     }
66
67     cout << "\nPress enter to start SO bundle " << endl;
68     getchar();
69     container->startBundle("oic.bundle.hueSample");
70
71     std::map<string, string> resourceParams;
72     cout << "Press enter to add SO bundle resource " << endl;
73     getchar();
74     resourceParams["resourceType"] = "oic.light.control";
75     resourceParams["address"] = "http://192.168.0.2/api/newdeveloper/lights/1";
76     container->addResourceConfig("oic.bundle.hueSample", "", resourceParams);
77     container->addResourceConfig("oic.bundle.hueSample", "", resourceParams);
78
79     std::list<string> resources = container->listBundleResources("oic.bundle.hueSample");
80     std::list<string>::iterator resourceIt;
81     cout << "\t>>> resource list size : " << resources.size() << endl;
82     for (resourceIt = resources.begin(); resourceIt != resources.end(); resourceIt++)
83     {
84         cout << "\t>>> resource uri : " << (*resourceIt).c_str() << endl;
85     }
86
87     cout << "\nPress enter to remove SO bundle resource " << endl;
88     getchar();
89     container->removeResourceConfig("oic.bundle.hueSample", "/hue/light/1");
90
91     resources = container->listBundleResources("oic.bundle.hueSample");
92     cout << "\t>>> resource list size : " << resources.size() << endl;
93     for (resourceIt = resources.begin(); resourceIt != resources.end(); resourceIt++)
94     {
95         cout << "\t>>> resource uri : " << (*resourceIt).c_str() << endl;
96     }
97
98     cout << "\nPress enter to stop SO Bundle " << endl;
99     getchar();
100     container->stopBundle("oic.bundle.hueSample");
101
102     cout << "Press enter to test remove SO Bundle " << endl;
103     getchar();
104     container->removeBundle("oic.bundle.hueSample");
105
106     bundles = container->listBundles();
107     cout << "\t>>> bundle list size : " << bundles.size() << endl;
108     for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
109     {
110         cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
111     }
112
113 #if(JAVA_SUPPORT)
114     /*java bundle add test*/
115     cout << "\n++++++++++++++++++++++++++++" << endl;
116     cout << "+++ Test for JAVA Bundle +++" << endl;
117     cout << "++++++++++++++++++++++++++++" << endl;
118     cout << "Press enter to add java bundle " << endl;
119     getchar();
120     bundleParams["libraryPath"] = ".";
121     bundleParams["activator"] = "org.iotivity.bundle.hue.HueBundleActivator";
122     container->addBundle("oic.bundle.hueJavaSample", "/hueJava",
123                          "../../../../../../../../service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar",
124                          bundleParams);
125
126     bundles = container->listBundles();
127     cout << "\t>>> bundle list size : " << bundles.size() << endl;
128     for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
129     {
130         cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
131     }
132
133     cout << "\nPress enter to start java bundle " << endl;
134     getchar();
135     container->startBundle("oic.bundle.hueJavaSample");
136
137     cout << "Press enter to stop java Bundle " << endl;
138     getchar();
139     container->stopBundle("oic.bundle.hueJavaSample");
140
141     cout << "Press enter to test remove java Bundle " << endl;
142     getchar();
143     container->removeBundle("oic.bundle.hueJavaSample");
144
145     bundles = container->listBundles();
146     cout << "\t>>> bundle list size : " << bundles.size() << endl;
147     for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
148     {
149         cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
150     }
151 #endif
152
153     cout << "\nPress enter to stop container " << endl;
154     getchar();
155     container->stopContainer();
156
157     cout << "Container stopped. Bye" << endl;
158 }