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