43fcc466b78d347df192783c7ec34e9ef20a63f0
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / TelnetServer.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file TelnetServer.cpp
7  *
8  * \date 20-Oct-2011 3:42:04 PM
9  * \author Christian Mueller (christian.ei.mueller@bmw.de)
10  *
11  * \section License
12  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13  * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
14  *
15  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19  * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20  * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21  * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22  *
23  */
24
25 #include "TelnetServer.h"
26 #include <assert.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <sys/ioctl.h>
30 #include <string.h>
31 #include <netdb.h>
32 #include <dlt/dlt.h>
33 #include <config.h>
34 #include <errno.h>
35 #include <sstream>
36 #include <istream>
37 #include <iostream>
38 #include <iterator>
39 #include "DatabaseHandler.h"
40 #include "RoutingSender.h"
41
42 using namespace am;
43
44 DLT_IMPORT_CONTEXT(AudioManager)
45
46 TelnetServer* TelnetServer::instance = NULL;
47
48 #define PRINT_BOOL(var) var ? output+="true\t\t" : output+="false\t\t";
49
50 TelnetServer::TelnetServer(SocketHandler *iSocketHandler, CommandSender *iCommandSender, CommandReceiver *iCommandReceiver, RoutingSender *iRoutingSender, RoutingReceiver *iRoutingReceiver, ControlSender *iControlSender, ControlReceiver *iControlReceiver, DatabaseHandler *iDatabasehandler, unsigned int servPort, unsigned int maxConnections) :
51         telnetConnectFiredCB(this, &TelnetServer::connectSocket), //
52         telnetReceiveFiredCB(this, &TelnetServer::receiveData), //
53         telnetDispatchCB(this, &TelnetServer::dispatchData), //
54         telnetCheckCB(this, &TelnetServer::check), //
55         mSocketHandler(iSocketHandler), //
56         mCommandSender(iCommandSender), //
57         mCommandReceiver(iCommandReceiver), //
58         mRoutingSender(iRoutingSender), //
59         mRoutingReceiver(iRoutingReceiver), //
60         mControlSender(iControlSender), //
61         mControlReceiver(iControlReceiver), //
62         mDatabasehandler(iDatabasehandler), //
63         mConnecthandle(), //
64         msgList(), //
65         mListConnections(), //
66         mConnectFD(NULL), //
67         mServerPort(servPort), //
68         mMaxConnections(maxConnections), //
69         mMapCommands(createCommandMap())
70 {
71     assert(mSocketHandler!=NULL);
72     assert(mCommandReceiver!=NULL);
73     assert(mCommandSender!=NULL);
74     assert(mControlSender!=NULL);
75     assert(mControlReceiver!=NULL);
76     assert(mRoutingSender!=NULL);
77     assert(mRoutingReceiver!=NULL);
78     assert(mDatabasehandler!=NULL);
79     assert(servPort!=0);
80     assert(mMaxConnections!=0);
81
82     instance = this;
83
84     int yes = 1;
85     struct sockaddr_in servAddr;
86
87     //setup the port Listener
88     mConnectFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
89     setsockopt(mConnectFD, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
90     memset(&servAddr, 0, sizeof(servAddr));
91     servAddr.sin_family = AF_INET;
92     servAddr.sin_addr.s_addr = INADDR_ANY;
93     servAddr.sin_port = htons(servPort);
94     bind(mConnectFD, (struct sockaddr *) &servAddr, sizeof(servAddr));
95
96     if (listen(mConnectFD, mMaxConnections) < 0)
97     {
98         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("TelnetServer::TelnetServerk cannot listen "), DLT_INT(errno));
99     }DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("TelnetServer::TelnetServerk started listening on port"), DLT_INT(mServerPort));
100
101     int a = 1;
102     ioctl(mConnectFD, FIONBIO, (char *) &a);
103     setsockopt(mConnectFD, SOL_SOCKET, SO_KEEPALIVE, (char *) &a, sizeof(a));
104
105     short events = 0;
106     events |= POLLIN;
107     mSocketHandler->addFDPoll(mConnectFD, events, NULL, &telnetConnectFiredCB, NULL, NULL, NULL, mConnecthandle);
108 }
109
110 TelnetServer::~TelnetServer()
111 {
112 }
113
114 void TelnetServer::connectSocket(const pollfd pfd, const sh_pollHandle_t handle, void *userData)
115 {
116     (void) handle;
117     (void) userData;
118     //first, accept the connection, create a new filedescriptor
119     struct sockaddr answer;
120     socklen_t len = sizeof(answer);
121     connection_s connection;
122     connection.filedescriptor = accept(pfd.fd, (struct sockaddr*) &answer, &len);
123
124     mListConnections.push_back(connection);
125
126     //set the correct event:
127     short event = 0;
128     event |= POLLIN;
129
130     //aded the filedescriptor to the sockethandler and register the callbacks for receiving the data
131     mSocketHandler->addFDPoll(mListConnections.back().filedescriptor, event, NULL, &telnetReceiveFiredCB, &telnetCheckCB, &telnetDispatchCB, NULL, mListConnections.back().handle);
132 }
133
134 void TelnetServer::receiveData(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
135 {
136     (void)handle;
137     (void)userData;
138     //initialize buffer
139     char buffer[100];
140     //read until buffer is full or no more data is there
141     int read = recv(pollfd.fd, buffer, 100, NULL);
142     if (read > 1)
143     {
144         //read the message and store it in a queue - its a telnet connection so data will be sent on enter !
145         std::string msg = std::string(buffer, read);
146         msgList.push(msg);
147     }
148 }
149
150 bool TelnetServer::dispatchData(const sh_pollHandle_t handle, void *userData)
151 {
152     (void)handle;
153     (void)userData;
154     std::vector<connection_s>::iterator iterator = mListConnections.begin();
155     for (; iterator != mListConnections.end(); ++iterator)
156     {
157         if (iterator->handle == handle) break;
158     }
159     if (iterator == mListConnections.end()) return false;
160
161     std::string command;
162     std::vector<std::string> msg;
163     sliceCommand(msgList.front(), command, msg);
164     msgList.pop();
165     mMapCommand_t::iterator commandIter = mMapCommands.find(command);
166     if (commandIter == mMapCommands.end())
167     {
168         send(iterator->filedescriptor, "Command not found!\n", 20, 0);
169     }
170     else
171     {
172         (*commandIter).second(msg, iterator->filedescriptor);
173     }
174
175     //remove the message from the queue and return false if there is no more message to read.
176     if (msgList.size() != 0) return true;
177     return false;
178 }
179
180 bool TelnetServer::check(const sh_pollHandle_t handle, void *userData)
181 {
182     (void)handle;
183     (void)userData;
184     if (msgList.size() != 0) return true;
185     return false;
186 }
187
188 void TelnetServer::listCommand(std::vector<std::string>& msg, int filedescriptor)
189 {
190     instance->listCommandShadow(msg, filedescriptor);
191 }
192
193 void TelnetServer::listCommandShadow(std::vector<std::string> & msg, int filedescriptor)
194 {
195     std::string output;
196     if (msg.empty())
197     {
198         output += "No second parameter given after list, please enter\n";
199     }
200     else if (msg.front().compare("plugins") == 0)
201     {
202         std::vector<std::string> plugins;
203         mRoutingSender->getListPlugins(plugins);
204         std::vector<std::string>::iterator it = plugins.begin();
205         output = "\n\nrouting plugins:\n-------------------------\n";
206         for (; it != plugins.end(); ++it)
207         {
208             output += *it + "\n";
209         }
210     }
211     else if (msg.front().compare("domains") == 0)
212     {
213         output = "domainID\t\tdomainName\t\tbusName\t\tnodeName\t\tearly\t\tstate\t\tcomplete\n";
214         output += "-------------------------------------------------------------------------------------\n";
215         std::vector<am_Domain_s> domainList;
216         mDatabasehandler->getListDomains(domainList);
217         std::vector<am_Domain_s>::iterator it = domainList.begin();
218         for (; it != domainList.end(); ++it)
219         {
220             output += it->domainID + "\t\t";
221             output += it->name + "\t\t";
222             output += it->busname + "\t\t";
223             output += it->nodename + "\t\t";
224             PRINT_BOOL(it->early);
225             switch (it->state)
226             {
227             case DS_CONTROLLED:
228                 output += "DS_CONTROLLED\t\t";
229                 break;
230             case DS_INDEPENDENT_STARTUP:
231                 output += "DS_INDEPENDENT_STARTUP\t\t";
232                 break;
233             case DS_INDEPENDENT_RUNDOWN:
234                 output += "DS_INDEPENDENT_RUNDOWN\t\t";
235                 break;
236             default:
237                 output += "undefined\t\t";
238                 break;
239             }PRINT_BOOL(it->complete);
240         }
241     }
242     else
243     {
244         output = "did not recognize parameter: " + msg.front() + "\n";
245     }
246     send(filedescriptor, output.c_str(), output.size(), 0);
247 }
248
249 void am::TelnetServer::sliceCommand(const std::string & string, std::string & command, std::vector<std::string> & msg)
250 {
251     std::stringstream stream(string);
252     std::istream_iterator<std::string> begin(stream);
253     std::istream_iterator<std::string> end;
254     command = *begin++;
255     msg = std::vector<std::string>(begin, end);
256 }
257
258 TelnetServer::mMapCommand_t TelnetServer::createCommandMap()
259 {
260     mMapCommand_t commands;
261     commands.insert(std::make_pair("list", &TelnetServer::listCommand));
262     return commands;
263 }
264