* modify contructor of CAmTelnetMenuHelper
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / CAmTelnetMenuHelper.cpp
1 /**
2 * Copyright (C) 2012, BMW AG
3 *
4 * GeniviAudioMananger AudioManagerDaemon
5 *
6 * \file CAmTelnetMenuHelper.cpp
7 *
8 * \date 24-Jan-2012
9 * \author Frank Herchet (frank.fh.herchet@bmw.de)
10 *
11 * \section License
12 * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13 * Copyright (C) 2012, BMW AG Frank Herchet  frank.fh.herchet@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 "CAmTelnetMenuHelper.h"
26 #include "TelnetServer.h"
27 #include "DatabaseHandler.h"
28 #include "ControlSender.h"
29 #include "CommandSender.h"
30 #include "RoutingSender.h"
31 #include "RoutingReceiver.h"
32 #include "CommandReceiver.h"
33 #include "ControlReceiver.h"
34 #include "Router.h"
35 #include "config.h"
36 #include <cassert>
37
38 #define DEBUG_ON false
39
40 using namespace am;
41
42 CAmTelnetMenuHelper* CAmTelnetMenuHelper::instance = NULL;
43
44 /****************************************************************************/
45 CAmTelnetMenuHelper::CAmTelnetMenuHelper(SocketHandler *iSocketHandler,
46                                          CommandSender *iCommandSender,
47                                          CommandReceiver *iCommandReceiver,
48                                          RoutingSender *iRoutingSender,
49                                          RoutingReceiver *iRoutingReceiver,
50                                          ControlSender *iControlSender,
51                                          ControlReceiver *iControlReceiver,
52                                          DatabaseHandler *iDatabasehandler,
53                                          Router *iRouter,
54                                          TelnetServer *iTelnetServer)
55 /****************************************************************************/
56 : mTelenetServer(iTelnetServer)
57 , mSocketHandler(iSocketHandler)
58 , mCommandSender(iCommandSender)
59 , mCommandReceiver(iCommandReceiver)
60 , mRoutingSender(iRoutingSender)
61 , mRoutingReceiver(iRoutingReceiver)
62 , mControlSender(iControlSender)
63 , mControlReceiver(iControlReceiver)
64 , mDatabasehandler(iDatabasehandler)
65 , mRouter(iRouter)
66 {
67    instance = this;
68    createCommandMaps();
69 }
70
71 /****************************************************************************/
72 CAmTelnetMenuHelper::~CAmTelnetMenuHelper()
73 /****************************************************************************/
74 {
75 }
76
77 /****************************************************************************/
78 void CAmTelnetMenuHelper::createCommandMaps()
79 /****************************************************************************/
80 {
81    // ROOT commands
82
83    mRootCommands.clear();
84
85    mRootCommands.insert(std::make_pair("help",sCommandPrototypeInfo("show all possible commands",&CAmTelnetMenuHelper::helpCommand)));
86    mRootCommands.insert(std::make_pair("list",sCommandPrototypeInfo("Go into 'list'-submenu",&CAmTelnetMenuHelper::rootListCommand)));
87    mRootCommands.insert(std::make_pair("info",sCommandPrototypeInfo("Go into 'info'-submenu",&CAmTelnetMenuHelper::rootInfoCommand)));
88    mRootCommands.insert(std::make_pair("set",sCommandPrototypeInfo("Go into 'set'-submenu",&CAmTelnetMenuHelper::rootSetCommand)));
89    mRootCommands.insert(std::make_pair("get",sCommandPrototypeInfo("Go into 'get'-submenu",&CAmTelnetMenuHelper::rootGetCommand)));
90    mRootCommands.insert(std::make_pair("exit",sCommandPrototypeInfo("quit telnet session",&CAmTelnetMenuHelper::exitCommand)));
91
92    // List commands
93    mListCommands.insert(std::make_pair("help",sCommandPrototypeInfo(std::string("show all possible commands"),&CAmTelnetMenuHelper::helpCommand)));
94    mListCommands.insert(std::make_pair("conn",sCommandPrototypeInfo("list all connections",&CAmTelnetMenuHelper::listConnectionsCommand)));
95    mListCommands.insert(std::make_pair("sources",sCommandPrototypeInfo("list all available sources",&CAmTelnetMenuHelper::listSourcesCommand)));
96    mListCommands.insert(std::make_pair("sinks",sCommandPrototypeInfo("list all available sinks",&CAmTelnetMenuHelper::listSinksCommands)));
97    mListCommands.insert(std::make_pair("crfaders",sCommandPrototypeInfo("list all crossfaders",&CAmTelnetMenuHelper::listCrossfaders)));
98    mListCommands.insert(std::make_pair("domains",sCommandPrototypeInfo("list all domains",&CAmTelnetMenuHelper::listDomainsCommand)));
99    mListCommands.insert(std::make_pair("gws",sCommandPrototypeInfo("list all gateways",&CAmTelnetMenuHelper::listGatewaysCommand)));
100    mListCommands.insert(std::make_pair("..",sCommandPrototypeInfo("one step back in menu tree (back to root folder)",&CAmTelnetMenuHelper::oneStepBackCommand)));
101    mListCommands.insert(std::make_pair("exit",sCommandPrototypeInfo("close telnet session",&CAmTelnetMenuHelper::exitCommand)));
102
103    // Set commands
104    mSetCommands.insert(std::make_pair("help",sCommandPrototypeInfo(std::string("show all possible commands"),&CAmTelnetMenuHelper::helpCommand)));
105    mSetCommands.insert(std::make_pair("..",sCommandPrototypeInfo("one step back in menu tree (back to root folder)",&CAmTelnetMenuHelper::oneStepBackCommand)));
106    mSetCommands.insert(std::make_pair("exit",sCommandPrototypeInfo("close telnet session",&CAmTelnetMenuHelper::exitCommand)));
107    mSetCommands.insert(std::make_pair("conn",sCommandPrototypeInfo("use 'conn sourceId sinkId' to connect a source and a sink",&CAmTelnetMenuHelper::setConnection)));
108    mSetCommands.insert(std::make_pair("routing",sCommandPrototypeInfo("use 'routing sourceId sinkId' to get all\n\t  possible routes between a sourceID and a sinkID",&CAmTelnetMenuHelper::setRoutingCommand)));
109    mSetCommands.insert(std::make_pair("disc",sCommandPrototypeInfo("use 'disc connectionID' to disconnect \n\t  this connection",&CAmTelnetMenuHelper::setDisconnectConnId)));
110
111    // Get commands
112    mGetCommands.insert(std::make_pair("help",sCommandPrototypeInfo(std::string("show all possible commands"),&CAmTelnetMenuHelper::helpCommand)));
113    mGetCommands.insert(std::make_pair("routing",sCommandPrototypeInfo("show current routing",&CAmTelnetMenuHelper::getRoutingCommand)));
114    mGetCommands.insert(std::make_pair("sendv",sCommandPrototypeInfo("show senderversion",&CAmTelnetMenuHelper::getSenderversionCommand)));
115    mGetCommands.insert(std::make_pair("recv",sCommandPrototypeInfo("show receiverversion ",&CAmTelnetMenuHelper::getReceiverversionCommand)));
116    mGetCommands.insert(std::make_pair("..",sCommandPrototypeInfo("one step back in menu tree (back to root folder)",&CAmTelnetMenuHelper::oneStepBackCommand)));
117    mGetCommands.insert(std::make_pair("exit",sCommandPrototypeInfo("close telnet session",&CAmTelnetMenuHelper::exitCommand)));
118
119    // Info comands
120    mInfoCommands.insert(std::make_pair("help",sCommandPrototypeInfo(std::string("show all possible commands"),&CAmTelnetMenuHelper::helpCommand)));
121    mInfoCommands.insert(std::make_pair("sysprop",sCommandPrototypeInfo("show all systemproperties",&CAmTelnetMenuHelper::infoSystempropertiesCommand)));
122    mInfoCommands.insert(std::make_pair("..",sCommandPrototypeInfo("one step back in menu tree (back to root folder)",&CAmTelnetMenuHelper::oneStepBackCommand)));
123    mInfoCommands.insert(std::make_pair("exit",sCommandPrototypeInfo("close telnet session",&CAmTelnetMenuHelper::exitCommand)));
124 }
125
126 /****************************************************************************/
127 void CAmTelnetMenuHelper::setTelnetServer(TelnetServer* iTelnetServer)
128 /****************************************************************************/
129 {
130    mTelenetServer = iTelnetServer;
131 }
132
133 /****************************************************************************/
134 void CAmTelnetMenuHelper::newSocketConnection(int filedescriptor)
135 /****************************************************************************/
136 {
137    EMainState state = eRootState;
138    std::map<int,EMainState>::iterator it;
139    std::stringstream welcome;
140
141    it = mCurrentMainStateMap.find(filedescriptor);
142    if( it != mCurrentMainStateMap.end())
143    {
144       // socket connection already exists, delete entry and go back to root state
145       mCurrentMainStateMap.erase(it);
146    }
147
148    it = mCurrentMainStateMap.begin();
149
150    // insert new socket connection
151    mCurrentMainStateMap.insert(it,std::make_pair<int,EMainState>(filedescriptor,state));
152
153    // Send welcome message
154    welcome << "Welcome to GENIVI AudioManager " << DAEMONVERSION << "\n>";
155    send(filedescriptor,welcome.str().c_str(),welcome.str().size(),0);
156 }
157
158 /****************************************************************************/
159 void CAmTelnetMenuHelper::socketConnectionsClosed(int filedescriptor)
160 /****************************************************************************/
161 {
162    std::map<int,EMainState>::iterator it;
163
164    it = mCurrentMainStateMap.find(filedescriptor);
165    if( it != mCurrentMainStateMap.end())
166    {
167       mCurrentMainStateMap.erase(it);
168    }
169    else
170    {
171       // connection not found
172    }
173 }
174
175 /****************************************************************************/
176 void CAmTelnetMenuHelper::enterCmdQueue(std::queue<std::string> & CmdQueue, int & filedescriptor)
177 /****************************************************************************/
178 {
179    std::map<int,EMainState>::iterator it;
180    std::string cmd;
181    tCommandMap::iterator cmditer;
182
183    // find current filedescriptor to get the current state of the telnet session
184    it = mCurrentMainStateMap.find(filedescriptor);
185    while(!CmdQueue.empty())
186    {
187       cmd = CmdQueue.front();
188
189       // Now remove the first command, it's stored in 'cmd'
190       CmdQueue.pop();
191       // telnet session found. depending on the current state, different commands are available
192       switch(it->second)
193       {
194          case eRootState:
195             cmditer = mRootCommands.find(cmd);
196             if(mRootCommands.end() != cmditer)
197                cmditer->second.CommandPrototype(CmdQueue,filedescriptor);
198             else
199                sendError(filedescriptor,"Command not found\n");
200             break;
201          case eListState:
202             cmditer = mListCommands.find(cmd);
203             if(mListCommands.end() != cmditer)
204                cmditer->second.CommandPrototype(CmdQueue,filedescriptor);
205             else
206                sendError(filedescriptor,"Command not found\n");
207             break;
208          case eInfoState:
209             cmditer = mInfoCommands.find(cmd);
210             if(mInfoCommands.end() != cmditer)
211                cmditer->second.CommandPrototype(CmdQueue,filedescriptor);
212             else
213                sendError(filedescriptor,"Command not found\n");
214             break;
215          case eGetState:
216             cmditer = mGetCommands.find(cmd);
217             if(mGetCommands.end() != cmditer)
218                cmditer->second.CommandPrototype(CmdQueue,filedescriptor);
219             else
220                sendError(filedescriptor,"Command not found\n");
221             break;
222          case eSetState:
223             cmditer = mSetCommands.find(cmd);
224             if(mSetCommands.end() != cmditer)
225                cmditer->second.CommandPrototype(CmdQueue,filedescriptor);
226             else
227                sendError(filedescriptor,"Command not found\n");
228             break;
229          default:
230             break;
231       }
232    }
233
234    sendCurrentCmdPrompt(filedescriptor);
235 }
236
237 /****************************************************************************/
238 void CAmTelnetMenuHelper::sendError(int & filedescriptor, std::string error_string)
239 /****************************************************************************/
240 {
241    send(filedescriptor,error_string.c_str(),error_string.size(),0);
242 }
243
244 /****************************************************************************/
245 void CAmTelnetMenuHelper::sendTelnetLine(int & filedescriptor, std::stringstream &line)
246 /****************************************************************************/
247 {
248    send(filedescriptor,line.str().c_str(),line.str().size(),0);
249 }
250
251 /****************************************************************************/
252 void CAmTelnetMenuHelper::sendCurrentCmdPrompt(int & filedescriptor)
253 /****************************************************************************/
254 {
255    std::map<int,EMainState>::iterator it;
256    std::stringstream outputstream;
257    outputstream << std::endl;
258
259    it = mCurrentMainStateMap.find(filedescriptor);
260    if( it != mCurrentMainStateMap.end())
261    {
262       switch(it->second)
263       {
264          case eRootState:
265             outputstream  << "\\>";
266             break;
267          case eListState:
268             outputstream  << "\\List>";
269             break;
270          case eGetState:
271             outputstream  << "\\Get>";
272             break;
273          case eSetState:
274             outputstream  << "\\Set>";
275             break;
276          case eInfoState:
277             outputstream  << "\\Info>";
278             break;
279          default:
280             break;
281       }
282
283       send(filedescriptor,outputstream.str().c_str(),outputstream.str().size(),0);
284
285    }
286    else
287    {
288       // connection not found
289    }
290 }
291
292 /****************************************************************************/
293 void CAmTelnetMenuHelper::exitCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
294 /****************************************************************************/
295 {
296    instance->exitCommandExec(CmdQueue,filedescriptor);
297 }
298
299 /****************************************************************************/
300 void CAmTelnetMenuHelper::oneStepBackCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
301 /****************************************************************************/
302 {
303    std::map<int,EMainState>::iterator it;
304    it = mCurrentMainStateMap.find(filedescriptor);
305    if( it != mCurrentMainStateMap.end())
306    {
307       if(DEBUG_ON)std::cout << "old state: " << it->second;
308       switch(it->second)
309       {
310          case eRootState:
311             it->second = eRootState;
312             break;
313          case eListState:
314             it->second = eRootState;;
315             break;
316          case eGetState:
317             it->second = eRootState;;
318             break;
319          case eSetState:
320             it->second = eRootState;;
321             break;
322          case eInfoState:
323             it->second = eRootState;;
324             break;
325          default:
326             it->second = eRootState;
327             break;
328       }
329       if(DEBUG_ON)std::cout << "new state: " << it->second << std::endl;
330    }
331 }
332
333 /****************************************************************************/
334 void CAmTelnetMenuHelper::oneStepBackCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
335 /****************************************************************************/
336 {
337    instance->oneStepBackCommandExec(CmdQueue,filedescriptor);
338 }
339
340 /****************************************************************************/
341 void CAmTelnetMenuHelper::exitCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
342 /****************************************************************************/
343 {
344    std::map<int,EMainState>::iterator it;
345    std::stringstream line;
346    std::stringstream output;
347
348    // Sending a last message to the client
349    output << "Your wish is my command ... bye!" << std::endl;
350    sendTelnetLine(filedescriptor,output);
351
352
353    tCommandMap::iterator iter;
354    it = mCurrentMainStateMap.find(filedescriptor);
355    if( it != mCurrentMainStateMap.end())
356    {
357       if(DEBUG_ON)std::cout << "removing client connection " << filedescriptor << std::endl;
358
359       if(NULL != mTelenetServer)
360       {
361          mTelenetServer->disconnectClient(filedescriptor);
362          mCurrentMainStateMap.erase(it);
363       }
364       else
365       {
366          // ASSERT mTelenetServer == NULL
367          if(DEBUG_ON)std::cout << "mTelenetServer";
368       }
369    }
370 }
371
372 /****************************************************************************/
373 void CAmTelnetMenuHelper::helpCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
374 /****************************************************************************/
375 {
376    instance->helpCommandExec(CmdQueue,filedescriptor);
377 }
378
379 /****************************************************************************/
380 void CAmTelnetMenuHelper::helpCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
381 /****************************************************************************/
382 {
383    std::map<int,EMainState>::iterator it;
384    std::stringstream line;
385    tCommandMap::iterator cmdIter;
386    it = mCurrentMainStateMap.find(filedescriptor);
387    if( it != mCurrentMainStateMap.end())
388    {
389       line << "###################################################" << std::endl;
390       line << "###### The following commands are supported: ######"<< std::endl;
391       line << "###################################################" << std::endl << std::endl;
392       switch(it->second)
393       {
394          case eRootState:
395
396             cmdIter = mRootCommands.begin();
397             while(cmdIter != mRootCommands.end())
398             {
399                line << cmdIter->first << "\t- " << cmdIter->second.info << std::endl;
400                cmdIter++;
401             }
402             break;
403          case eListState:
404             cmdIter = mListCommands.begin();
405             while(cmdIter != mListCommands.end())
406             {
407                line << cmdIter->first << "\t- " << cmdIter->second.info << std::endl;
408                cmdIter++;
409             }
410             break;
411          case eGetState:
412             cmdIter = mGetCommands.begin();
413             while(cmdIter != mGetCommands.end())
414             {
415                line << cmdIter->first << "\t- " << cmdIter->second.info << std::endl;
416                cmdIter++;
417             }
418             break;
419          case eSetState:
420             cmdIter = mSetCommands.begin();
421             while(cmdIter != mSetCommands.end())
422             {
423                line << cmdIter->first << "\t- " << cmdIter->second.info << std::endl;
424                cmdIter++;
425             }
426             break;
427          case eInfoState:
428             cmdIter = mInfoCommands.begin();
429             while(cmdIter != mInfoCommands.end())
430             {
431                line << cmdIter->first << "\t- " << cmdIter->second.info << std::endl;
432                cmdIter++;
433             }
434             break;
435          default:
436             break;
437       }
438       sendTelnetLine(filedescriptor,line);
439    }
440 }
441
442 /****************************************************************************/
443 void CAmTelnetMenuHelper::rootGetCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
444 /****************************************************************************/
445 {
446    instance->rootGetCommandExec(CmdQueue,filedescriptor);
447 }
448
449 /****************************************************************************/
450 void CAmTelnetMenuHelper::rootGetCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
451 /****************************************************************************/
452 {
453    std::map<int,EMainState>::iterator it;
454    it = mCurrentMainStateMap.find(filedescriptor);
455    if( it != mCurrentMainStateMap.end())
456    {
457       it->second = eGetState;
458    }
459 }
460
461 /****************************************************************************/
462 void CAmTelnetMenuHelper::rootSetCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
463 /****************************************************************************/
464 {
465    instance->rootSetCommandExec(CmdQueue,filedescriptor);
466 }
467
468 /****************************************************************************/
469 void CAmTelnetMenuHelper::rootSetCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
470 /****************************************************************************/
471 {
472    std::map<int,EMainState>::iterator it;
473    it = mCurrentMainStateMap.find(filedescriptor);
474    if( it != mCurrentMainStateMap.end())
475    {
476       it->second = eSetState;
477    }
478 }
479
480 /****************************************************************************/
481 void CAmTelnetMenuHelper::rootListCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
482 /****************************************************************************/
483 {
484    instance->rootListCommandExec(CmdQueue,filedescriptor);
485 }
486
487 /****************************************************************************/
488 void CAmTelnetMenuHelper::rootListCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
489 /****************************************************************************/
490 {
491    std::map<int,EMainState>::iterator it;
492    it = mCurrentMainStateMap.find(filedescriptor);
493    if( it != mCurrentMainStateMap.end())
494    {
495       it->second = eListState;
496    }
497 }
498
499 /****************************************************************************/
500 void CAmTelnetMenuHelper::rootInfoCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
501 /****************************************************************************/
502 {
503    instance->rootInfoCommandExec(CmdQueue,filedescriptor);
504 }
505
506 /****************************************************************************/
507 void CAmTelnetMenuHelper::rootInfoCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
508 /****************************************************************************/
509 {
510    std::map<int,EMainState>::iterator it;
511    it = mCurrentMainStateMap.find(filedescriptor);
512    if( it != mCurrentMainStateMap.end())
513    {
514       it->second = eInfoState;
515    }
516 }
517
518 /****************************************************************************/
519 void CAmTelnetMenuHelper::listConnectionsCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
520 /****************************************************************************/
521 {
522    instance->listConnectionsCommandExec(CmdQueue,filedescriptor);
523 }
524
525 /****************************************************************************/
526 void CAmTelnetMenuHelper::listConnectionsCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
527 /****************************************************************************/
528 {
529    std::vector<am_Connection_s> listConnections;
530    std::stringstream line;
531
532    mDatabasehandler->getListConnections(listConnections);
533
534    line << "\tCurrent connections: " << listConnections.size() << std::endl;
535
536    sendTelnetLine(filedescriptor,line);
537
538    std::vector<am_Connection_s>::iterator it(listConnections.begin());
539    while(it != listConnections.end())
540    {
541       line.str("");
542       line << "\tID: "  << it->connectionID
543            << "\tSrcID: "  << it->sourceID
544            << "\tSinkID: " << it->sinkID
545            << "\tFormat: " << it->connectionFormat
546            << "\tdelay: "  << it->delay
547            << std::endl;
548
549       sendTelnetLine(filedescriptor,line);
550       it++;
551    }
552 }
553
554 /****************************************************************************/
555 void CAmTelnetMenuHelper::listSourcesCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
556 /****************************************************************************/
557 {
558    instance->listSourcesCommandExec(CmdQueue,filedescriptor);
559 }
560
561 /****************************************************************************/
562 void CAmTelnetMenuHelper::listSourcesCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
563 /****************************************************************************/
564 {
565    std::vector<am_Source_s> listSources;
566    std::stringstream line;
567
568    mDatabasehandler->getListSources(listSources);
569
570    line << "\tCurrent sources: " << listSources.size();
571    sendTelnetLine(filedescriptor,line);
572
573    std::vector<am_Source_s>::iterator it(listSources.begin());
574    while(it != listSources.end())
575    {
576       line.str("");
577       line << "\tID: "  << it->sourceID
578            << "\tDomainID: "  << it->domainID
579            << "\tName: " << it->name
580            << "\tState: " << it->sourceState
581            << "\tVolume: "  << it->volume
582            << std::endl;
583
584       sendTelnetLine(filedescriptor,line);
585       it++;
586    }
587 }
588
589 /****************************************************************************/
590 void CAmTelnetMenuHelper::listSinksCommands(std::queue<std::string> & CmdQueue, int & filedescriptor)
591 /****************************************************************************/
592 {
593    instance->listSinksCommandsExec(CmdQueue,filedescriptor);
594 }
595
596 /****************************************************************************/
597 void CAmTelnetMenuHelper::listSinksCommandsExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
598 /****************************************************************************/
599 {
600    std::vector<am_Sink_s> listSinks;
601    std::stringstream line;
602
603    mDatabasehandler->getListSinks(listSinks);
604
605    line << "\tCurrent sinks: " << listSinks.size() << std::endl;
606    sendTelnetLine(filedescriptor,line);
607
608    std::vector<am_Sink_s>::iterator it(listSinks.begin());
609    while(it != listSinks.end())
610    {
611       line.str("");
612       line << "\tID: "  << it->sinkID
613            << "\tDomainID: "  << it->domainID
614            << "\tName: " << it->name
615            << "\tAvailable: " << it->available.availability
616            << "\tVolume: "  << it->volume
617            << std::endl;
618
619       sendTelnetLine(filedescriptor,line);
620       it++;
621    }
622 }
623
624 /****************************************************************************/
625 void CAmTelnetMenuHelper::listCrossfaders(std::queue<std::string> & CmdQueue, int & filedescriptor)
626 /****************************************************************************/
627 {
628    instance->listCrossfadersExec(CmdQueue,filedescriptor);
629 }
630
631 /****************************************************************************/
632 void CAmTelnetMenuHelper::listCrossfadersExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
633 /****************************************************************************/
634 {
635    std::vector<am_Crossfader_s> listCrossfaders;
636    std::stringstream line;
637
638    mDatabasehandler->getListCrossfaders(listCrossfaders);
639
640    line << "\tCurrent crossfaders: " << listCrossfaders.size();
641    sendTelnetLine(filedescriptor,line);
642
643    std::vector<am_Crossfader_s>::iterator it(listCrossfaders.begin());
644    while(it != listCrossfaders.end())
645    {
646       line.str("");
647       line << "\tID: "  << it->crossfaderID
648            << "\tName: "  << it->name
649            << "\tSourceID: " << it->sourceID
650            << std::endl;
651
652       sendTelnetLine(filedescriptor,line);
653       it++;
654    }
655 }
656
657 /****************************************************************************/
658 void CAmTelnetMenuHelper::listDomainsCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
659 /****************************************************************************/
660 {
661    instance->listDomainsCommandExec(CmdQueue,filedescriptor);
662 }
663
664 /****************************************************************************/
665 void CAmTelnetMenuHelper::listDomainsCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
666 /****************************************************************************/
667 {
668    std::vector<am_Domain_s> listDomains;
669    std::stringstream line;
670
671    mDatabasehandler->getListDomains(listDomains);
672
673    line << "\tCurrent domains: " << listDomains.size()<<std::endl;
674    sendTelnetLine(filedescriptor,line);
675
676    std::vector<am_Domain_s>::iterator it(listDomains.begin());
677    while(it != listDomains.end())
678    {
679       line.str("");
680       line << "\tID: "  << it->domainID
681            << "\tName: "  << it->name
682            << "\tBusname: " << it->busname
683            << "\tNodename: " << it->nodename
684            << "\tState: " << static_cast<int>(it->state)
685            << std::endl;
686
687       sendTelnetLine(filedescriptor,line);
688       it++;
689    }
690 }
691
692 /****************************************************************************/
693 void CAmTelnetMenuHelper::listGatewaysCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
694 /****************************************************************************/
695 {
696    instance->listGatewaysCommandExec(CmdQueue,filedescriptor);
697 }
698
699 /****************************************************************************/
700 void CAmTelnetMenuHelper::listGatewaysCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
701 /****************************************************************************/
702 {
703    std::vector<am_Gateway_s> listGateways;
704    std::stringstream line;
705
706    mDatabasehandler->getListGateways(listGateways);
707
708    line << "\tCurrent gateways: " << listGateways.size();
709    sendTelnetLine(filedescriptor,line);
710
711    std::vector<am_Gateway_s>::iterator it(listGateways.begin());
712    while(it != listGateways.end())
713    {
714       line.str("");
715       line << "\tID: "  << it->gatewayID
716            << "\tName: "  << it->name
717            << "\tSourceID: " << it->sourceID
718            << "\tSinkID: " << it->sinkID
719            << std::endl;
720
721       sendTelnetLine(filedescriptor,line);
722       it++;
723    }
724 }
725
726 /****************************************************************************/
727 void CAmTelnetMenuHelper::getRoutingCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
728 /****************************************************************************/
729 {
730    instance->getRoutingCommandExec(CmdQueue,filedescriptor);
731 }
732
733 /****************************************************************************/
734 void CAmTelnetMenuHelper::getRoutingCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
735 /****************************************************************************/
736 {
737    //TODO: fill with function
738 }
739
740 /****************************************************************************/
741 void CAmTelnetMenuHelper::getSenderversionCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
742 /****************************************************************************/
743 {
744    instance->getSenderversionCommandExec(CmdQueue,filedescriptor);
745 }
746
747 /****************************************************************************/
748 void CAmTelnetMenuHelper::getSenderversionCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
749 /****************************************************************************/
750 {
751    std::stringstream line;
752
753    line << "\tSender versions:" << std::endl
754         << "\tCtrl: "     << mControlSender->getInterfaceVersion() << " | "
755         << "Cmd: "      << mCommandSender->getInterfaceVersion() << " | "
756         << "Routing: "  << mRoutingSender->getInterfaceVersion() << std::endl;
757
758    sendTelnetLine(filedescriptor,line);
759 }
760
761 /****************************************************************************/
762 void CAmTelnetMenuHelper::getReceiverversionCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
763 /****************************************************************************/
764 {
765    instance->getReceiverversionCommandExec(CmdQueue,filedescriptor);
766 }
767
768 /****************************************************************************/
769 void CAmTelnetMenuHelper::getReceiverversionCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
770 /****************************************************************************/
771 {
772    std::stringstream line;
773
774    line << "\tReceiver versions:" << std::endl
775         << "\tCtrl: "     << mControlReceiver->getInterfaceVersion() << " | "
776         << "Cmd: "      << mCommandReceiver->getInterfaceVersion() << " | "
777         << "Routing: "  << mRoutingReceiver->getInterfaceVersion() << std::endl;
778
779    sendTelnetLine(filedescriptor,line);
780
781 }
782
783 /****************************************************************************/
784 void CAmTelnetMenuHelper::infoSystempropertiesCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
785 /****************************************************************************/
786 {
787    instance->infoSystempropertiesCommandExec(CmdQueue,filedescriptor);
788 }
789
790 /****************************************************************************/
791 void CAmTelnetMenuHelper::infoSystempropertiesCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
792 /****************************************************************************/
793 {
794    std::vector<am_SystemProperty_s> listSystemProperties;
795    std::vector<am_SystemProperty_s>::iterator it;
796    std::stringstream line;
797
798    mDatabasehandler->getListSystemProperties(listSystemProperties);
799
800    line << "\tSystemproperties: ";
801    sendTelnetLine(filedescriptor,line);
802
803    for(it = listSystemProperties.begin(); it < listSystemProperties.end(); it++ )
804    {
805       line.str("");
806       line << "\tType: " <<  it->type << " Value: " << it->value << std::endl;
807       sendTelnetLine(filedescriptor,line);
808    }
809 }
810
811 /****************************************************************************/
812 void CAmTelnetMenuHelper::setRoutingCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
813 /****************************************************************************/
814 {
815    instance->setRoutingCommandExec(CmdQueue,filedescriptor);
816 }
817
818 /****************************************************************************/
819 void CAmTelnetMenuHelper::setRoutingCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
820 /****************************************************************************/
821 {
822    std::stringstream                   output;
823    std::vector<am_Route_s>             routingList;
824
825    am_sourceID_t                       sourceID = 0;
826    am_sinkID_t                         sinkID   = 0;
827
828    bool                                error = false;
829    am_Error_e                          rError = E_OK;
830
831
832    if(CmdQueue.size() >= 2)
833    {
834       std::istringstream istream_sourceID(CmdQueue.front());
835       CmdQueue.pop();
836       std::istringstream istream_sinkID(CmdQueue.front());
837       CmdQueue.pop();
838
839       if(!(istream_sourceID >> sourceID))
840          error = true;
841       if(!(istream_sinkID >> sinkID))
842          error = true;
843
844       if(error)
845       {
846          sendError(filedescriptor,"Error parsing sourcID and sinkID");
847          return;
848       }
849
850       if(DEBUG_ON)std::cout << "setRoutingCommandExec(sourceID: " << sourceID << ",sinkID: " << sinkID << ")" << std::endl;
851
852       rError = mRouter->getRoute(true,sourceID,sinkID,routingList);
853
854       if(E_OK == rError)
855       {
856          std::vector<am_Route_s>::iterator rlIter = routingList.begin();
857          for(int rlCnt = 1;rlIter < routingList.end();rlIter++)
858          {
859             output << "#" << rlCnt << " ";
860
861             std::vector<am_RoutingElement_s>::iterator reIter = rlIter->route.begin();
862             for(;reIter < rlIter->route.end();reIter++)
863             {
864                reIter->connectionFormat;
865                reIter->domainID;
866                output << ">(" << reIter->sourceID << ")->--[D:"<< reIter->domainID <<"][F:"<< reIter->connectionFormat <<"]-->-(" << reIter->sinkID<< ")" << std::endl;
867             }
868
869             rlCnt++;
870          }
871
872          sendTelnetLine(filedescriptor,output);
873       }
874       else
875       {
876          sendError(filedescriptor,"Error getting route");
877       }
878
879    }
880    else
881    {
882       CmdQueue.pop();
883       output << "Not enough arguments to set routing. Please enter sourceID and sinkID after command" << std::endl;
884       return;
885    }
886
887
888 }
889
890 /****************************************************************************/
891 void CAmTelnetMenuHelper::setConnection(std::queue<std::string> & CmdQueue, int & filedescriptor)
892 /****************************************************************************/
893 {
894    instance->setConnectionExec(CmdQueue,filedescriptor);
895 }
896
897 /****************************************************************************/
898 void CAmTelnetMenuHelper::setConnectionExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
899 /****************************************************************************/
900 {
901    std::stringstream                   output;
902
903    am_sourceID_t                       sourceID = 0;
904    am_sinkID_t                         sinkID   = 0;
905    am_mainConnectionID_t               connID   = 0;
906
907    bool                                error = false;
908    am_Error_e                          rError = E_OK;
909
910    if(CmdQueue.size() >= 2)
911    {
912       std::istringstream istream_sourceID(CmdQueue.front());
913       CmdQueue.pop();
914
915       std::istringstream istream_sinkID(CmdQueue.front());
916             CmdQueue.pop();
917
918       if(!(istream_sourceID >> sourceID))
919          error = true;
920
921       if(!(istream_sinkID >> sinkID))
922          error = true;
923
924       if(error)
925       {
926          sendError(filedescriptor,"Error parsing sinkID and/or sourceID");
927          return;
928       }
929
930       // Try to set up connection
931       rError = mCommandReceiver->connect(sourceID,sinkID,connID);
932
933       if(E_OK == rError)
934       {
935          output << "ConnID: " << connID << "\tSrc: " << sourceID << " ---> Sink: " << sinkID << std::endl;
936          sendTelnetLine(filedescriptor,output);
937       }
938       else
939       {
940          sendError(filedescriptor,"Error connecting sourceID and sinkID");
941       }
942
943    }
944    else
945    {
946       CmdQueue.pop();
947       sendError(filedescriptor,"Not enough arguments to set routing. Please enter sourceID and sinkID after command");
948       return;
949    }
950 }
951
952 /****************************************************************************/
953 void CAmTelnetMenuHelper::setDisconnectConnId(std::queue<std::string> & CmdQueue, int & filedescriptor)
954 /****************************************************************************/
955 {
956    instance->setDisconnectConnIdExec(CmdQueue,filedescriptor);
957 }
958
959 /****************************************************************************/
960 void CAmTelnetMenuHelper::setDisconnectConnIdExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
961 /****************************************************************************/
962 {
963    std::stringstream                   output;
964
965    am_mainConnectionID_t               connID   = 0;
966
967    bool                                error = false;
968    am_Error_e                          rError = E_OK;
969
970    if(CmdQueue.size() >= 1)
971    {
972       std::istringstream istream_connID(CmdQueue.front());
973       CmdQueue.pop();
974
975       if(!(istream_connID >> connID))
976          error = true;
977
978       if(error)
979       {
980          sendError(filedescriptor,"Error parsing connID");
981          return;
982       }
983
984       // Try to disconnect connection id
985       rError = mCommandReceiver->disconnect(connID);
986
987       if(E_OK == rError)
988       {
989          output << "ConnID " << connID << " closed successfully! " << std::endl;
990          sendTelnetLine(filedescriptor,output);
991       }
992       else
993       {
994          sendError(filedescriptor,"Error disconnecting connectionID");
995       }
996    }
997    else
998    {
999       sendError(filedescriptor,"Not enough arguments to disconnect a Main Connection, please enter 'connectionID' after command");
1000       return;
1001    }
1002 }
1003
1004 /****************************************************************************/
1005 void CAmTelnetMenuHelper::setSourceSoundProperties(std::queue<std::string> & CmdQueue, int & filedescriptor)
1006 /****************************************************************************/
1007 {
1008    instance->setConnectionExec(CmdQueue,filedescriptor);
1009 }
1010
1011 /****************************************************************************/
1012 void CAmTelnetMenuHelper::setSourceSoundPropertiesExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
1013 /****************************************************************************/
1014 {
1015    std::stringstream    output;
1016    am_sinkID_t          sourceID;
1017    am_MainSoundProperty_s soundProperty;
1018    unsigned int tmpType = 0;
1019    bool error = false;
1020
1021    if(CmdQueue.size() >= 3)
1022    {
1023       std::istringstream istream_sourceID(CmdQueue.front());
1024       CmdQueue.pop();
1025
1026       std::istringstream istream_type(CmdQueue.front());
1027       CmdQueue.pop();
1028
1029       std::istringstream istream_value(CmdQueue.front());
1030       CmdQueue.pop();
1031
1032       if(!(istream_type >> tmpType))
1033          error = true;
1034
1035       if(tmpType < MSP_MAX)
1036          soundProperty.type = static_cast<am_MainSoundPropertyType_e>(tmpType);
1037       else
1038          error = true;
1039
1040       if(!(istream_value >> soundProperty.value))
1041          error = true;
1042
1043       if(!(istream_sourceID >> sourceID))
1044          error = true;
1045
1046       if(error)
1047       {
1048          sendError(filedescriptor,"Error parsing MainSinkSoundProperty 'type', 'value' or 'sourceID'");
1049          return;
1050       }
1051
1052       if(E_OK == mCommandReceiver->setMainSourceSoundProperty(soundProperty,sourceID))
1053       {
1054          output << "MainSourceSoundProperty set: " << soundProperty.type << "->" << soundProperty.value << std::endl;
1055          sendTelnetLine(filedescriptor,output);
1056       }
1057       else
1058       {
1059          sendError(filedescriptor,"Error setMainSourceSoundProperty");
1060       }
1061    }
1062    else
1063    {
1064       sendError(filedescriptor,"Not enough arguments to set MainSourceSoundProperty, please enter 'sourceID', 'type' and 'value' after command");
1065       return;
1066    }
1067 }
1068
1069 /****************************************************************************/
1070 void CAmTelnetMenuHelper::setSinkSoundProperties(std::queue<std::string> & CmdQueue, int & filedescriptor)
1071 /****************************************************************************/
1072 {
1073    instance->setConnectionExec(CmdQueue,filedescriptor);
1074 }
1075
1076 /****************************************************************************/
1077 void CAmTelnetMenuHelper::setSinkSoundPropertiesExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
1078 /****************************************************************************/
1079 {
1080    std::stringstream    output;
1081    am_sinkID_t          sinkID;
1082    am_MainSoundProperty_s soundProperty;
1083    unsigned int tmpType = 0;
1084    bool error = false;
1085
1086    if(CmdQueue.size() >= 3)
1087    {
1088       std::istringstream istream_sinkID(CmdQueue.front());
1089       CmdQueue.pop();
1090
1091       std::istringstream istream_type(CmdQueue.front());
1092       CmdQueue.pop();
1093
1094       std::istringstream istream_value(CmdQueue.front());
1095       CmdQueue.pop();
1096
1097       if(!(istream_type >> tmpType))
1098          error = true;
1099
1100       if(tmpType < MSP_MAX)
1101          soundProperty.type = static_cast<am_MainSoundPropertyType_e>(tmpType);
1102       else
1103          error = true;
1104
1105       if(!(istream_value >> soundProperty.value))
1106          error = true;
1107
1108       if(!(istream_sinkID >> sinkID))
1109          error = true;
1110
1111       if(error)
1112       {
1113          sendError(filedescriptor,"Error parsing MainSinkSoundProperty 'type', 'value' or 'sinkID'");
1114          return;
1115       }
1116
1117       if(E_OK == mCommandReceiver->setMainSinkSoundProperty(soundProperty,sinkID))
1118       {
1119          output << "MainSinkSoundProperty set: " << soundProperty.type << "->" << soundProperty.value << std::endl;
1120          sendTelnetLine(filedescriptor,output);
1121       }
1122       else
1123       {
1124          sendError(filedescriptor,"Error setMainSinkSoundProperty");
1125       }
1126    }
1127    else
1128    {
1129       sendError(filedescriptor,"Not enough arguments to set MainSinkSoundProperty, please enter 'sinkID', 'type' and 'value' after command");
1130       return;
1131    }
1132 }
1133
1134
1135 /****************************************************************************/
1136 void CAmTelnetMenuHelper::listPluginsCommand(std::queue<std::string> & CmdQueue, int & filedescriptor)
1137 /****************************************************************************/
1138 {
1139    instance->listPluginsCommandExec(CmdQueue,filedescriptor);
1140 }
1141
1142 /****************************************************************************/
1143 void CAmTelnetMenuHelper::listPluginsCommandExec(std::queue<std::string> & CmdQueue, int & filedescriptor)
1144 /****************************************************************************/
1145 {
1146    std::vector<std::string> PlugInNames;
1147    std::vector<std::string>::iterator iter;
1148    std::stringstream output;
1149    am_Error_e rError = E_OK;
1150
1151
1152    rError = mCommandSender->getListPlugins(PlugInNames);
1153
1154    output << "CommandSender Plugins loaded: " << PlugInNames.size() << std::endl;
1155
1156    for(iter = PlugInNames.begin(); iter < PlugInNames.end(); iter++ )
1157    {
1158       output << iter->c_str() << std::endl;
1159    }
1160
1161    rError = mRoutingSender->getListPlugins(PlugInNames);
1162
1163    output << std::endl << "RoutingSender Plugins loaded: " << PlugInNames.size() << std::endl;
1164
1165    for(iter = PlugInNames.begin(); iter < PlugInNames.end(); iter++ )
1166    {
1167       output << iter->c_str() << std::endl;
1168    }
1169
1170    sendTelnetLine(filedescriptor,output);
1171 }
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182