Adding BaulkControl Skel (Bus-Dev)
[baulk.git] / src / Baulk / Server / server.cpp
bloba948418e3f325034847ef50f2989a642ff76478f
1 // Baulk - Information Server - Server
2 //
3 // Baulk - Copyright (C) 2008 - Jacob Alexander
4 //
5 // File: server.cpp
6 // Author(s): Jacob Alexander (HaaTa)
7 //
8 // Baulk is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // any later version, including version 3 of the License.
13 // Baulk is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "server.h"
23 InformationServer::InformationServer( QString listen, QObject *parent ) : QObject( parent ) {
24 allowTerminate = true;
25 // Reserve First Socket and already running test
26 QLocalSocket *tmp = new QLocalSocket( this );
27 clientList.append( tmp );
28 tmp->connectToServer( listen );
30 switch ( tmp->error() ) {
31 // Server Not Running
32 case QLocalSocket::ServerNotFoundError:
33 listenSocket = listen;
34 server = new QLocalServer( this );
35 if ( !server->listen( listenSocket ) )
36 qCritical( tr("InformationServer\n\t|Could not open listen socket\n\t||%1").arg( listenSocket ).toUtf8() );
37 connect( server, SIGNAL( newConnection() ), this, SLOT( connection() ) );
38 break;
39 // Server Already Running Case
40 default:
41 terminate();
42 break;
45 allowTerminate = false;
46 connectedClients = 0;
49 InformationServer::~InformationServer() {
50 qDebug("BaulkServ - Closing");
53 void InformationServer::clientRedirect() {
54 qDebug("InformationServer\n\t|Client Request (Server Redirect)");
56 QStringList flags = incomingPacket->dataFlags();
57 QStringList data = incomingPacket->data();
59 for ( int c = 0; c < flags.count(); ++c ) {
60 /* if ( flags[c] == "RequestId" ) {
61 if ( data[c] == "True" )
62 requestId();
63 }*/
67 void InformationServer::connection() {
68 clientConnection = server->nextPendingConnection();
69 connect( clientConnection, SIGNAL( readyRead() ), this, SLOT( incomingData() ) );
70 connect( clientConnection, SIGNAL( disconnected() ), clientConnection, SLOT( deleteLater() ) );
73 // All Incoming Data goes through here
74 void InformationServer::incomingData() {
75 QDataStream in( clientConnection );
76 in.setVersion( QDataStream::Qt_4_4 );
77 QString data;
79 in >> data;
81 if ( data == "" ) {
82 qDebug( QString("InformationServer\n\t|Blank Packet!").toUtf8() );
83 return;
86 incomingPacket = new Packet( data, this );
88 // Server Request
89 if ( Packet::idToInfo( incomingPacket->destinationId() ).windowId == 0 ) {
90 serverRequest();
91 return;
94 // Client Request
95 if ( Packet::idToInfo( incomingPacket->destinationId() ).windowId > 0 ) {
96 clientRedirect();
97 return;
100 // Invalid Packet
101 qDebug( QString("InformationServer\n\t|Invalid Packet!\n\t\t%1").arg( data ).toUtf8() );
104 void InformationServer::outgoingData( QString data ) {
105 QByteArray block;
106 QDataStream out( &block, QIODevice::WriteOnly );
107 out.setVersion( QDataStream::Qt_4_4 );
109 out << data;
111 clientConnection->write( block );
112 clientConnection->flush();
115 void InformationServer::requestId() {
116 int newId = 0;
117 if ( emptyClientListEntries.count() > 0 ) {
118 newId = emptyClientListEntries.first();
119 emptyClientListEntries.removeFirst();
121 else
122 newId = clientList.count();
124 clientList.append( clientConnection );
126 QString destination = Packet::infoToId(
127 Packet::idToInfo( incomingPacket->senderId() ).screenId,
128 newId );
130 Packet answerPacket( destination,
131 incomingPacket->destinationId(),
132 QStringList() << "NewId",
133 QStringList() << QString::number( newId ) );
135 outgoingData( answerPacket.packet() );
137 allowTerminate = true;
138 ++connectedClients;
141 void InformationServer::serverRequest() {
142 qDebug("InformationServer\n\t|Server Request");
144 QStringList flags = incomingPacket->dataFlags();
145 QStringList data = incomingPacket->data();
147 for ( int c = 0; c < flags.count(); ++c ) {
148 if ( flags[c] == "RequestId" ) {
149 if ( data[c] == "True" )
150 requestId();
152 if ( flags[c] == "Ping" ) {
153 if ( data[c] == "True" ) {
154 Packet answerPacket( incomingPacket->senderId(),
155 incomingPacket->destinationId(),
156 QStringList() << "PingReply",
157 QStringList() << "True");
158 outgoingData( answerPacket.packet() );
161 if ( flags[c] == "RemoveId" ) {
162 if ( data[c] == "True" ) {
163 int id = Packet::idToInfo( incomingPacket->senderId() ).windowId;
164 emptyClientListEntries.append( id );
166 if ( emptyClientListEntries.count() >= clientList.count() )
167 emptyClientListEntries.removeOne( clientList.count() );
169 qDebug( QString("InformationServer\n\t|Id Removed\n\t\t%1").arg( id ).toUtf8() );
171 --connectedClients;
172 if ( connectedClients < 1 )
173 terminate();
179 bool InformationServer::terminate() {
180 // Only Allow Close if the Server has been used
181 if ( !allowTerminate )
182 return false;
184 deleteLater();
185 return true;