MNE-CPP  beta 1.0
commandserver.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "commandserver.h"
42 #include "commandthread.h"
43 
44 #include "mne_rt_server.h"
45 
46 #include "fiffstreamserver.h"
47 #include "fiffstreamthread.h"
48 #include "mne_rt_server.h"
49 #include "connectormanager.h"
50 
51 
52 //*************************************************************************************************************
53 //=============================================================================================================
54 // STL INCLUDES
55 //=============================================================================================================
56 
57 #include <stdlib.h>
58 #include <iostream>
59 
60 
61 //*************************************************************************************************************
62 //=============================================================================================================
63 // USED NAMESPACES
64 //=============================================================================================================
65 
66 using namespace RTSERVER;
67 
68 
69 //*************************************************************************************************************
70 //=============================================================================================================
71 // DEFINE MEMBER METHODS
72 //=============================================================================================================
73 
75 : QTcpServer(parent)
76 , m_iThreadCount(0)
77 {
78  QObject::connect(&m_commandParser, &CommandParser::response, this, &CommandServer::prepareReply);
79 }
80 
81 
82 //*************************************************************************************************************
83 
85 {
86  emit closeCommandThreads();
87 }
88 
89 
90 //*************************************************************************************************************
91 
92 void CommandServer::incommingCommand(QString p_sCommand, qint32 p_iThreadID)
93 {
94  QStringList t_qListParsedCommands;
95 
96  m_iCurrentCommandThreadID = p_iThreadID;
97 
98  if(!m_commandParser.parse(p_sCommand, t_qListParsedCommands))
99  {
100  QByteArray t_blockReply;
101  t_blockReply.append("command unknown\r\n");
102  printf("%s", t_blockReply.data());
103 
104  //send reply
105  emit replyCommand(t_blockReply, p_iThreadID);
106  }
107 }
108 
109 
110 //*************************************************************************************************************
111 
112 void CommandServer::incomingConnection(qintptr socketDescriptor)
113 {
114  CommandThread* t_pCommandThread = new CommandThread(socketDescriptor, m_iThreadCount, this);
115  ++m_iThreadCount;
116 
117  //when thread has finished it gets deleted
118  connect(t_pCommandThread, SIGNAL(finished()), t_pCommandThread, SLOT(deleteLater()));
119  connect(this, SIGNAL(closeCommandThreads()), t_pCommandThread, SLOT(deleteLater()));
120 
121  //Forwards for thread safety
122  //Connect incomming commands
123  connect(t_pCommandThread, &CommandThread::newCommand,
125  //Connect command Replies
126  connect(this, &CommandServer::replyCommand,
127  t_pCommandThread, &CommandThread::attachCommandReply);
128 
129  t_pCommandThread->start();
130 }
131 
132 
133 //*************************************************************************************************************
134 
136 {
137  //Attach Observer to Subject
138  m_commandParser.attach(&p_commandManager);
139  //Register Reply Channel
140 // p_commandManager.registerResponseChannel(&m_commandParser, &CommandParser::response);
141  QObject::connect(&p_commandManager, &CommandManager::response, &m_commandParser, &CommandParser::response);
142 }
143 
144 
145 //*************************************************************************************************************
146 
147 void CommandServer::prepareReply(QString p_sReply, Command p_command)
148 {
149  //Only when multi threaded command parsing is applied
150 // qDebug() << m_qMultiMapCommandThreadID;
151 // QMultiMap<QString, qint32>::iterator it = m_qMultiMapCommandThreadID.find(p_command.command());
152 // qint32 t_iThreadID = it.value(); //Remove this id from stored set
153 // m_qMultiMapCommandThreadID.remove(p_command.command(), t_iThreadID);
154 // qDebug() << QThread::currentThreadId();
155 
156  //Currently only one parsing thread per time
157  qint32 t_iThreadID = m_iCurrentCommandThreadID;
158 
159  //print
160 // printf("%s",p_sReply.toLatin1().constData());
161 
162  emit replyCommand(p_sReply, t_iThreadID);
163 
164  Q_UNUSED(p_command);
165 }
void response(QString p_sReply, Command p_command)
void incommingCommand(QString p_sCommand, qint32 p_iThreadID)
void registerCommandManager(CommandManager &p_commandManager)
bool parse(const QString &p_sInput, QStringList &p_qListCommandsParsed)
CommandServer(QObject *parent=0)
implementation of the FiffStreamServer Class.
void response(QString p_sResponse, Command p_command)
void incomingConnection(qintptr socketDescriptor)
implementation of the MNERTServer Class.
implementation of the FiffStreamThread Class.
void attach(IObserver *pObserver)
void replyCommand(QString p_blockReply, qint32 p_iID)
implementation of the ConnectorManager Class.
void prepareReply(QString p_sReply, Command p_command)
implementation of the CommandThread Class.
implementation of the CommandServer Class.