1 //******************************************************************
3 // Copyright 2014 Intel Corporation.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "WrapResource.h"
22 #include "RestInput.h"
23 #include "LineInput.h"
24 #include "OICMiddle.h"
30 static bool enableDebug = false; // set to true to print debug messages
32 void printDebugMessage(std::string message)
35 cout << "RestInput: " << message << endl;
39 RestInput::RestInput(LineInput *lineInput) : m_lineInput(lineInput)
41 m_data = (char*)malloc(BUFLEN);
45 RestInput::~RestInput()
51 bool RestInput::init()
53 m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
55 cerr << "Failed to open socket. Exiting" << endl;
58 m_port = 1441; //listening on port 1441
60 m_serverAddr.sin_family = AF_INET;
61 m_serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
62 m_serverAddr.sin_port = htons(m_port);
64 if (::bind(m_sockfd, (struct sockaddr*)&m_serverAddr, sizeof m_serverAddr) < 0) {
65 cerr << "Failed to bind. Exiting " << endl;
69 listen(m_sockfd, MAX_CONNS);
70 startAccept(m_sockfd);
74 // accept incoming connection(s)
75 void RestInput::startAccept(int &sockfd)
77 if (m_threadCount >= MAX_CONNS) {
78 cerr << " Max # of connections reached. Skipping " << endl;
82 int connfd = accept(sockfd, (struct sockaddr *)NULL, NULL);
84 cerr << " Failed to accept incoming connection " << endl;
87 int n = read(connfd, m_data, BUFLEN);
90 cerr << "Failed to read from socket" << endl;
98 // start client thread
99 void RestInput::startThread()
101 std::thread t(&RestInput::processClient, this);
102 m_thread[m_threadCount] = std::move(t);
103 m_thread[m_threadCount++].join();
106 // process read commands for the client
107 void RestInput::processClient(void)
109 std::string restCmd = m_data;
110 std::size_t found = restCmd.find('\n');
111 if (found != std::string::npos) {
112 restCmd = restCmd.substr(0, found-1);
117 void RestInput::handleRead(std::string& restCmd)
119 parseString(restCmd);
120 if (restCmd.find("exit") == 0) {
121 std::thread::id id = std::this_thread::get_id();
122 for(int i = 0; i < m_threadCount; ++i) {
123 if (id == m_thread[i].get_id()) {
124 m_thread[i].detach();
126 cout << "Exiting thread " << id << endl;
133 std::string msg = "command sent to LineInput is: " + restCmd;
134 printDebugMessage(msg);
135 m_lineInput->processLine(restCmd, ss, cb);
136 if (restCmd.find("show") != string::npos) {
137 // if command is show, we want to list out the details of each resource
142 void RestInput::handleShow(stringstream &ss, observecb_t &cb) {
143 std::string temp = ss.str();
144 size_t n = std::count(temp.begin(), temp.end(), '\n'); // number of resources found
145 std::stringstream sstm;
146 std::string lineInputData;
148 for (size_t i = 0; i < n; ++i) {
150 sstm << "details " << i;
151 lineInputData = sstm.str();
152 std::string msg = "Details: " + lineInputData;
153 printDebugMessage(msg);
154 m_lineInput->processLine(lineInputData, ss, cb);
157 lineInputData = sstm.str();
158 msg = "Get: " + lineInputData;
159 printDebugMessage(msg);
160 m_lineInput->processLine(lineInputData, ss, cb);
164 void RestInput::parseString(std::string &toParse)
166 std::size_t pos = toParse.find("HTTP"); // split on HTTP
167 toParse = toParse.substr(0, pos);
168 pos = toParse.find("/"); // find 1st occurance of /
169 toParse = toParse.substr(pos + 1, toParse.size() - 1);
170 std::replace(toParse.begin(), toParse.end(), '/', ' '); // replace all '/' with ' '