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