1 // Baulk - Information Server - Server
3 // Baulk - Copyright (C) 2008 - Jacob Alexander
6 // Author(s): Jacob Alexander (HaaTa)
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/>.
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() ) {
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() ) );
39 // Server Already Running Case
45 allowTerminate
= false;
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" )
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
);
82 qDebug( QString("InformationServer\n\t|Blank Packet!").toUtf8() );
86 incomingPacket
= new Packet( data
, this );
89 if ( Packet::idToInfo( incomingPacket
->destinationId() ).windowId
== 0 ) {
95 if ( Packet::idToInfo( incomingPacket
->destinationId() ).windowId
> 0 ) {
101 qDebug( QString("InformationServer\n\t|Invalid Packet!\n\t\t%1").arg( data
).toUtf8() );
104 void InformationServer::outgoingData( QString data
) {
106 QDataStream
out( &block
, QIODevice::WriteOnly
);
107 out
.setVersion( QDataStream::Qt_4_4
);
111 clientConnection
->write( block
);
112 clientConnection
->flush();
115 void InformationServer::requestId() {
117 if ( emptyClientListEntries
.count() > 0 ) {
118 newId
= emptyClientListEntries
.first();
119 emptyClientListEntries
.removeFirst();
122 newId
= clientList
.count();
124 clientList
.append( clientConnection
);
126 QString destination
= Packet::infoToId(
127 Packet::idToInfo( incomingPacket
->senderId() ).screenId
,
130 Packet
answerPacket( destination
,
131 incomingPacket
->destinationId(),
132 QStringList() << "NewId",
133 QStringList() << QString::number( newId
) );
135 outgoingData( answerPacket
.packet() );
137 allowTerminate
= true;
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" )
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() );
172 if ( connectedClients
< 1 )
179 bool InformationServer::terminate() {
180 // Only Allow Close if the Server has been used
181 if ( !allowTerminate
)