Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / examples / OICMiddle / OICMiddle.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation.
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 //
22 // OICMiddle.cpp : OIC demo application for Minnowboard
23 //
24
25 #include <unistd.h>
26
27 #include "OICMiddle.h"
28 #include "WrapResource.h"
29 #include "Client.h"
30 #include "Server.h"
31 #include "LineInput.h"
32 #include "RestInput.h"
33
34 class Middle middle;    // one and only
35
36 Middle::Middle() :
37     m_appType(AT_None),
38     m_useLineInput(false),
39     m_useRestInput(false),
40     m_client(nullptr),
41     m_server(nullptr),
42     m_lineInput(nullptr),
43     m_restInput(nullptr)
44 {
45 }
46
47 Middle::~Middle()
48 {
49     delete m_client;
50     delete m_server;
51     delete m_lineInput;
52     delete m_restInput;
53 }
54
55 void Middle::init()
56 {
57
58 }
59
60 void Middle::run(int argc, char* argv[])
61 {
62     parseCommandLineOptions(argc, argv);
63
64     startPlatform();
65
66     if (m_appType & AT_Client) {
67         m_client = new MiddleClient();
68         m_client->init();
69     }
70
71     m_lineInput = new LineInput(m_client);
72
73     if (m_appType & AT_Server) {
74         m_server = new MiddleServer();
75         m_server->init();
76     }
77     if (m_useRestInput) {
78         if (!m_server) {
79             m_server = new MiddleServer();
80             m_server->init();
81         }
82         m_restInput = new RestInput(m_lineInput);
83         m_restInput->init();
84     }
85     if (m_useLineInput) {
86         if (m_server) {
87             m_lineInput->setServer(m_server);
88         }
89         m_lineInput->run();
90     } else {
91         while (true)
92             sleep(1);
93     }
94 }
95
96 void Middle::startPlatform()
97 {
98     uint16_t port = 0;
99     //std::string ipaddr = INADDR_ANY;
100     std::string ipaddr = "0.0.0.0";
101
102     PlatformConfig cfg { ServiceType::InProc, ModeType::Both,
103                   ipaddr, port, QualityOfService::LowQos};
104
105     OC::OCPlatform::Configure(cfg);
106 }
107
108 void Middle::provideHelp()
109 {
110     static const char usage[] = "\nUsage:  IOCMiddle args\n"
111                 "    where args may include any of these:\n"
112                 "\t-client      Run OIC client\n"
113                 "\t-server      Run OIC server\n"
114                 "\t-both        Run OIC client and server\n"
115                 "\t-console     Run console line interpreter\n"
116                 "\t-rest        Run ReST server\n"
117                 "\t-hue addr    Enable Hue resources on bridge at addr\n"
118                 "\t-help        Show Usage again\n"
119                 "Any combination of the above is okay.\n\n";
120     cout << usage;
121 }
122
123 bool Middle::parseCommandLineOptions(int argc, char *argv[])
124 {
125     bool any = false;
126
127     for (int i = 1; i < argc; i++) {
128         if (argv[i] == string("-server")) {
129             middle.m_appType = AT_Server; any = true;
130         } else if (argv[i] == string("-client")) {
131             middle.m_appType = AT_Client; any = true;
132         } else if (argv[i] == string("-both")) {
133             middle.m_appType = AT_Both; any = true;
134         } else if (argv[i] == string("-console")) {
135             middle.m_useLineInput = true; any = true;
136         } else if (argv[i] == string("-rest")) {
137             middle.m_useRestInput = true; any = true;
138         } else if (argv[i] == string("-hue")) {
139             if (i + 1 < argc && argv[i + 1][0] != '-') {
140                 m_hueAddr = argv[++i];
141                 any = true;
142             }
143         } else if (argv[i] == string("-help")) {
144                 any = false;
145             break;
146         } else {
147             std::cerr << "Not enough or invalid arguments, please try again.\n";
148             exit(1);
149         }
150     }
151     if (!any)
152             provideHelp();
153     return true;
154 }
155
156 int main(int argc, char* argv[])
157 {
158     middle.run(argc, argv);
159     return 0;
160 }