1 // Baulk - Information Server - Client
3 // Baulk - Copyright (C) 2008 - Jacob Alexander
5 // Baulk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version, including version 3 of the License.
10 // Baulk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // Constructor ************************************************************************************
21 InformationClient::InformationClient( QString call
, bool informOnClose
, QObject
*parent
) : QObject( parent
) {
22 currentId
= 0; // Default Id;
24 socket
= new QLocalSocket( this );
26 informDaemonOnClose
= informOnClose
;
28 connect( socket
, SIGNAL( readyRead() ), this, SLOT( incomingData() ) );
31 // Destructor *************************************************************************************
32 InformationClient::~InformationClient() {
33 // Remove Client From Server List
34 if ( informDaemonOnClose
) {
35 Packet
removePacket( Packet::infoToId( 0, 0 ),
36 Packet::infoToId( 0, currentId
),
37 QStringList() << "RemoveId",
38 QStringList() << "True");
39 outgoingData( removePacket
.packet() );
41 // Since Windows cuts corners, I have to force it to keep the
42 // client open until the very end
43 qCritical("Client Closing");
48 void InformationClient::clientRequest() {
49 QStringList flags
= incomingPacket
->dataFlags();
50 QStringList data
= incomingPacket
->data();
52 for ( int c
= 0; c
< flags
.count(); ++c
) {
53 if ( flags
[c
] == "NewId" ) {
54 int newIdnum
= data
[c
].toInt();
55 if ( newIdnum
!= 0 ) // toInt defaults to 0 on failure, and 0 will never be an assigned Id
58 qCritical( QString("%1 || Failure to get ID").arg( errorName() ).toUtf8() );
63 void InformationClient::connectToServer() {
67 // All Incoming Data goes through here ************************************************************
68 void InformationClient::incomingData() {
69 QDataStream
in( socket
);
70 in
.setVersion( QDataStream::Qt_4_4
);
74 incomingPacket
= new Packet( data
, this );
76 // Only Accept Packets Assigning Id's initially
77 if ( currentId
== 0 ) {
78 // Only accept Packets from Server
79 if ( Packet::idToInfo( incomingPacket
->senderId() ).windowId
== 0 ) {
88 qDebug( QString("%1\n\tInvalid Packet!\n\t\t%2").arg( errorName() ).arg( data
).toUtf8() );
91 void InformationClient::newId( int newIdnum
) {
94 // Allows for the parent program to determine when an ID has been assigned
97 qDebug( QString("%1\n\tNew Id || %2").arg( errorName() ).arg( currentId
).toUtf8() );
100 // All Outgoing Data goes through here ************************************************************
101 void InformationClient::outgoingData( QString data
) {
102 //if ( !socket->isValid() ) {
104 socket
->connectToServer( serverName
);
108 QDataStream
out( &block
, QIODevice::WriteOnly
);
109 out
.setVersion( QDataStream::Qt_4_4
);
113 socket
->write( block
);
117 void InformationClient::requestId() {
118 Packet
requestPacket( Packet::infoToId( 0, 0 ),
119 Packet::infoToId( 0, 0 ),
120 QStringList() << "RequestId",
121 QStringList() << "True" );
123 outgoingData( requestPacket
.packet() );
126 // Request startNewHostInstance signal on server **************************************************
127 void InformationClient::requestStartNewHostInstance() {
128 Packet
requestPacket( Packet::infoToId( 0, 0 ),
129 Packet::infoToId( 0, id() ),
130 QStringList() << "RequestStartNewHostInstance",
131 QStringList() << "True" );
133 outgoingData( requestPacket
.packet() );