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