libkdegames Library API Documentation

KGameProcess Class Reference

This is the process class used on the computer player side to communicate with its counterpart KProcessIO class. More...

#include <kgameprocess.h>

Inheritance diagram for KGameProcess:

Inheritance graph
[legend]
Collaboration diagram for KGameProcess:

Collaboration graph
[legend]
List of all members.

Signals

void signalCommand (QDataStream &inputStream, int msgid, int receiver, int sender)
void signalTurn (QDataStream &stream, bool turn)
void signalInit (QDataStream &stream, int userid)

Public Member Functions

 KGameProcess ()
 ~KGameProcess ()
bool exec (int argc, char *argv[])
bool terminate () const
void setTerminate (bool b)
void sendMessage (QDataStream &stream, int msgid, Q_UINT32 receiver=0)
void sendSystemMessage (QDataStream &stream, int msgid, Q_UINT32 receiver=0)
KRandomSequence * random ()

Protected Slots

void receivedMessage (const QByteArray &receiveBuffer)

Protected Member Functions

void processArgs (int argc, char *argv[])

Protected Attributes

bool mTerminate
KMessageFilePipemMessageIO

Detailed Description

This is the process class used on the computer player side to communicate with its counterpart KProcessIO class.

Using these two classes will give fully transparent communication via QDataStreams.

Definition at line 42 of file kgameprocess.h.


Constructor & Destructor Documentation

KGameProcess::KGameProcess  ) 
 

Creates a KGameProcess class.

Done only in the computer player. To activate the communication you have to call the exec function of this class which will listen to the communication and emit signals to notify you of any incoming messages. Note: This function will only return after you set setTerminate(true) in one of the received signals. So you can not do any computer calculation after the exec function. Instead you react on the signals which are emitted after a message is received and perform the calculations there! Example:

  int main(int argc ,char * argv[])
  {
    KGameProcess proc;
    connect(&proc,SIGNAL(signalCommand(QDataStream &,int ,int ,int )),
                    this,SLOT(slotCommand(QDataStream & ,int ,int ,int )));
    connect(&proc,SIGNAL(signalInit(QDataStream &,int)),
                    this,SLOT(slotInit(QDataStream & ,int )));
    connect(&proc,SIGNAL(signalTurn(QDataStream &,bool )),
                    this,SLOT(slotTurn(QDataStream & ,bool )));
    return proc.exec(argc,argv);
  }

Definition at line 46 of file kgameprocess.cpp.

References QObject::connect(), mMessageIO, mTerminate, QFile::open(), and receivedMessage().

KGameProcess::~KGameProcess  ) 
 

Destruct the process.

Definition at line 64 of file kgameprocess.cpp.

References QFile::close(), and mMessageIO.


Member Function Documentation

bool KGameProcess::exec int  argc,
char *  argv[]
 

Enters the event loop of the computer process.

Does only return on setTerminate(true)!

Definition at line 76 of file kgameprocess.cpp.

References KMessageFilePipe::exec(), mMessageIO, mTerminate, and processArgs().

bool KGameProcess::terminate  )  const [inline]
 

Should the computer process leave its exec function? Activated if you setTerminate(true);.

Returns:
true/false

Definition at line 91 of file kgameprocess.h.

void KGameProcess::setTerminate bool  b  )  [inline]
 

Set this to true if the computer process should end, ie leave its exec function.

Parameters:
b true for exit the exec function

Definition at line 99 of file kgameprocess.h.

void KGameProcess::sendMessage QDataStream stream,
int  msgid,
Q_UINT32  receiver = 0
 

Sends a message to the corresponding KGameIO device.

Works like the sendSystemMessage but for user id's

Parameters:
stream the QDataStream containing the message
msgid the message id for the message
receiver unused

Definition at line 110 of file kgameprocess.cpp.

References sendSystemMessage().

void KGameProcess::sendSystemMessage QDataStream stream,
int  msgid,
Q_UINT32  receiver = 0
 

Sends a system message to the corresonding KGameIO device.

This will normally be either a performed move or a query (IdProcessQuery). The query option is a way to communicate with the KGameIO at the other side and e.g. retrieve some game relevant data from here. Exmaple for a query:

  QByteArray buffer;
  QDataStream out(buffer,IO_WriteOnly);
  int msgid=KGameMessage::IdProcessQuery;
  out << (int)1;
  proc.sendSystemMessage(out,msgid,0);

Parameters:
stream the QDataStream containing the message
msgid the message id for the message
receiver unused

Definition at line 91 of file kgameprocess.cpp.

References QBuffer::buffer(), KGameMessage::createHeader(), QDataStream::device(), mMessageIO, KMessageFilePipe::send(), and QDataStream::writeRawBytes().

Referenced by sendMessage().

KRandomSequence* KGameProcess::random  )  [inline]
 

Returns a pointer to a KRandomSequence.

You can generate random numbers via e.g.

   random()->getLong(100);

Returns:
KRandomSequence pointer

Definition at line 142 of file kgameprocess.h.

void KGameProcess::processArgs int  argc,
char *  argv[]
[protected]
 

processes the command line argumens to set up the computer player Pass the argumens exactely as given by main()

Definition at line 115 of file kgameprocess.cpp.

Referenced by exec().

void KGameProcess::receivedMessage const QByteArray receiveBuffer  )  [protected, slot]
 

A message is received via the interprocess connection.

The appropriate signals are called.

Definition at line 132 of file kgameprocess.cpp.

References KGameMessage::extractHeader(), QObject::sender(), signalCommand(), signalInit(), and signalTurn().

Referenced by KGameProcess().

void KGameProcess::signalCommand QDataStream inputStream,
int  msgid,
int  receiver,
int  sender
[signal]
 

The generic communication signal.

You have to connect to this signal to generate a valid computer response onto arbitrary messages. All signals but IdIOAdded and IdTurn end up here! Example:

 void Computer::slotCommand(int &msgid,QDataStream &in,QDataStream &out)
 {
   Q_INT32 data,move;
   in >> data;
   // compute move ...
   move=data*2;
   out << move;
 }

Parameters:
inputStream the incoming data stream
msgid the message id of the message which got transmitted to the computer
receiver the id of the receiver
sender the id of the sender

Referenced by receivedMessage().

void KGameProcess::signalTurn QDataStream stream,
bool  turn
[signal]
 

This signal is emmited if the computer player should perform a turn.

Calculations can be made here and the move can then be send back with sendSystemMessage with the message id KGameMessage::IdPlayerInput. These must provide a move which complies to your other move syntax as e.g. produces by keyboard or mouse input. Additonal data which have been written into the stream from the ProcessIO's signal signalPrepareTurn can be retrieved from the stream here. Example:

 void slotTurn(QDataStream &in,bool turn)
 {
   int id;
   int recv;
   QByteArray buffer;
   QDataStream out(buffer,IO_WriteOnly);
   if (turn)
   {
     // Create a move - the format is yours to decide
     // It arrives exactly as this in the kgame inputMove function!!
     Q_INT8 x1,y1,pl;
     pl=-1;
     x1=proc.random()->getLong(8);
     y1=proc.random()->getLong(8);
     // Stream it
     out << pl << x1 << y1;
     id=KGameMessage::IdPlayerInput;
     proc.sendSystemMessage(out,id,0);
   }
 }

Parameters:
stream The datastream which contains user data
turn True or false whether the turn is activated or deactivated

Referenced by receivedMessage().

void KGameProcess::signalInit QDataStream stream,
int  userid
[signal]
 

This signal is emmited when the process is initialized, i.e.

added to a KPlayer. Initial initialisation can be performed here be reacting to the KProcessIO signal signalIOAdded and retrieving the data here from the stream. It works just as the signalTurn() but is only send when the player is added to the game, i.e. it needs some initialization data

Parameters:
stream The datastream which contains user data
userid The userId of the player. (Careful to rely on it yet)

Referenced by receivedMessage().


The documentation for this class was generated from the following files:
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.4.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 9 09:38:23 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003