Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / resource-container / 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 #if defined(__linux__)
22 #include <unistd.h>
23 #endif
24
25 #include "RCSResourceContainer.h"
26 #include "RCSBundleInfo.h"
27 #include "oc_logger.hpp"
28 #include <iostream>
29
30 using namespace std;
31 using namespace OIC::Service;
32 using OC::oc_log_stream;
33
34 #define MAX_PATH 2048
35
36 /* Another way to create a context: */
37 auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
38 {
39     static OC::oc_log_stream ols(oc_make_ostream_logger);
40     static boost::iostreams::stream<OC::oc_log_stream> os(ols);
41     return os;
42 };
43
44 void getCurrentPath(std::string *pPath)
45 {
46     char buffer[MAX_PATH];
47
48 #if defined(__linux__)
49     char *strPath = NULL;
50     int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1);
51
52     if (length != -1)
53     {
54         buffer[length] = '\0';
55         strPath = strrchr(buffer, '/');
56
57         if (strPath != NULL)
58             *strPath = '\0';
59     }
60 #endif
61     pPath->append(buffer);
62 }
63
64 int main()
65 {
66     info_logger()->set_module("ContainerTest");
67     info_logger()->set_level(OC_LOG_INFO);
68
69     info_logger() << "Starting container test." << std::flush;
70
71     std::string strConfigPath;
72     getCurrentPath(&strConfigPath);
73     strConfigPath.append("/examples/ResourceContainerConfig.xml");
74
75     RCSResourceContainer *container = RCSResourceContainer::getInstance();
76     container->startContainer(strConfigPath);
77
78     /*so bundle add test*/
79     cout << "++++++++++++++++++++++++++" << endl;
80     cout << "+++ Test for SO Bundle +++" << endl;
81     cout << "++++++++++++++++++++++++++" << endl;
82     cout << "Press enter to add SO bundle " << endl;
83     getchar();
84     std::map<string, string> bundleParams;
85     container->addBundle("oic.bundle.hueSample", "", "libHueBundle.so", "huesample", bundleParams);
86
87     std::list<unique_ptr<RCSBundleInfo>> bundles = container->listBundles();
88     std::list<unique_ptr<RCSBundleInfo>>::iterator bundleIt;
89
90     cout << "\t>>> bundle list size : " << bundles.size() << endl;
91     for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
92     {
93         cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
94     }
95
96     cout << "\nPress enter to start SO bundle " << endl;
97     getchar();
98     container->startBundle("oic.bundle.hueSample");
99
100     std::map<string, string> resourceParams;
101     cout << "Press enter to add SO bundle resource " << endl;
102     getchar();
103     resourceParams["resourceType"] = "oic.r.light";
104     resourceParams["address"] = "http://192.168.0.2/api/newdeveloper/lights/1";
105     container->addResourceConfig("oic.bundle.hueSample", "", resourceParams);
106     container->addResourceConfig("oic.bundle.hueSample", "", resourceParams);
107
108     std::list<string> resources = container->listBundleResources("oic.bundle.hueSample");
109     std::list<string>::iterator resourceIt;
110     cout << "\t>>> resource list size : " << resources.size() << endl;
111     for (resourceIt = resources.begin(); resourceIt != resources.end(); resourceIt++)
112     {
113         cout << "\t>>> resource uri : " << (*resourceIt).c_str() << endl;
114     }
115
116     cout << "\nPress enter to remove SO bundle resource " << endl;
117     getchar();
118     container->removeResourceConfig("oic.bundle.hueSample", "/hue/light/1");
119
120     resources = container->listBundleResources("oic.bundle.hueSample");
121     cout << "\t>>> resource list size : " << resources.size() << endl;
122     for (resourceIt = resources.begin(); resourceIt != resources.end(); resourceIt++)
123     {
124         cout << "\t>>> resource uri : " << (*resourceIt).c_str() << endl;
125     }
126
127     cout << "\nPress enter to stop SO Bundle " << endl;
128     getchar();
129     container->stopBundle("oic.bundle.hueSample");
130
131     cout << "Press enter to test remove SO Bundle " << endl;
132     getchar();
133     container->removeBundle("oic.bundle.hueSample");
134
135     bundles = container->listBundles();
136     cout << "\t>>> bundle list size : " << bundles.size() << endl;
137     for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
138     {
139         cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
140     }
141
142 #if(JAVA_SUPPORT)
143     /*java bundle add test*/
144     cout << "\n++++++++++++++++++++++++++++" << endl;
145     cout << "+++ Test for JAVA Bundle +++" << endl;
146     cout << "++++++++++++++++++++++++++++" << endl;
147     cout << "Press enter to add java bundle " << endl;
148     getchar();
149     bundleParams["libraryPath"] = ".";
150     std::string activator = "org.iotivity.bundle.hue.HueBundleActivator";
151     container->addBundle("oic.bundle.hueJavaSample2", "/hueJava",
152                  "../../../../../../service/resource-container/" \
153                  "examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar",
154                  activator,
155                  bundleParams);
156
157     bundles = container->listBundles();
158     cout << "\t>>> bundle list size : " << bundles.size() << endl;
159     for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
160     {
161         cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
162     }
163
164     cout << "\nPress enter to start java bundle " << endl;
165     getchar();
166     container->startBundle("oic.bundle.hueJavaSample2");
167
168     cout << "Press enter to stop java Bundle " << endl;
169     getchar();
170     container->stopBundle("oic.bundle.hueJavaSample2");
171
172     cout << "Press enter to test remove java Bundle " << endl;
173     getchar();
174     container->removeBundle("oic.bundle.hueJavaSample2");
175
176     bundles = container->listBundles();
177     cout << "\t>>> bundle list size : " << bundles.size() << endl;
178     for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
179     {
180         cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
181     }
182 #endif
183
184     cout << "\nPress enter to stop container " << endl;
185     getchar();
186     container->stopContainer();
187
188     cout << "Container stopped. Bye" << endl;
189 }