• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames/kgame

kgameprocess.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2001 Martin Heni (kde at heni-online.de)
00004     Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License version 2 as published by the Free Software Foundation.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kgameprocess.h"
00022 #include "kplayer.h"
00023 #include "kgame.h"
00024 #include "kgamemessage.h"
00025 #include "kmessageio.h"
00026 
00027 #include <krandomsequence.h>
00028 
00029 #include <qbuffer.h>
00030 #include <QDataStream>
00031 #include <QtCore/QFile>
00032 
00033 #include <assert.h>
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 #include <string.h>
00038 
00039 #define READ_BUFFER_SIZE  1024
00040 
00041 class KGameProcessPrivate
00042 {
00043 public:
00044     QFile rFile;
00045     QFile wFile;
00046     KRandomSequence* mRandom;
00047 };
00048 
00049 // ----------------------- Process Child ---------------------------
00050 
00051 KGameProcess::KGameProcess()
00052     : QObject(), d(new KGameProcessPrivate)
00053 {
00054   mTerminate=false;
00055   // Check whether a player is set. If not create one!
00056   d->rFile.open(stdin, QIODevice::ReadOnly|QIODevice::Unbuffered);
00057   d->wFile.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered);
00058   mMessageIO = new KMessageFilePipe(this, &d->rFile, &d->wFile);
00059 //  mMessageClient=new KMessageClient(this);
00060 //  mMessageClient->setServer(mMessageIO);
00061 //  connect (mMessageClient, SIGNAL(broadcastReceived(const QByteArray&, quint32)),
00062 //          this, SLOT(receivedMessage(const QByteArray&, quint32)));
00063   connect (mMessageIO, SIGNAL(received(const QByteArray&)),
00064           this, SLOT(receivedMessage(const QByteArray&)));
00065  
00066   d->mRandom = new KRandomSequence;
00067   d->mRandom->setSeed(0);
00068 }
00069 KGameProcess::~KGameProcess() 
00070 {
00071   delete d->mRandom;
00072   //delete mMessageClient;
00073   //delete mMessageServer;
00074   fprintf(stderr,"KGameProcess::destructor\n");
00075   fflush(stderr);
00076   delete mMessageIO;
00077   d->rFile.close();
00078   d->wFile.close();
00079   delete d;
00080 }
00081 
00082 
00083 bool KGameProcess::exec(int argc, char *argv[])
00084 {
00085   // Get id and cookie, ... from command line
00086   processArgs(argc,argv);
00087   do
00088   {
00089     mMessageIO->exec();
00090   }  while(!mTerminate);
00091   return true;
00092 }
00093 
00094 //    You have to do this to create a message 
00095 //    QByteArray buffer;
00096 //    QDataStream wstream(buffer,QIODevice::WriteOnly);
00097 //    then stream data into the stream and call this function
00098 void KGameProcess::sendSystemMessage(QDataStream &stream,int msgid,quint32 receiver)
00099 {
00100   fprintf(stderr,"KGameProcess::sendSystemMessage to parent id=%d recv=%ld\n",msgid,(unsigned long)receiver);
00101   QByteArray a;
00102   QDataStream outstream(&a,QIODevice::WriteOnly);
00103 
00104   QBuffer *device=(QBuffer *)stream.device();
00105   QByteArray data=device->buffer();
00106 
00107   KGameMessage::createHeader(outstream,0,receiver,msgid);
00108   outstream.writeRawData(data.data(),data.size());
00109 
00110   //  if (mMessageClient) mMessageClient->sendForward(a,2);
00111   if (mMessageIO) mMessageIO->send(a);
00112   else fprintf(stderr,"KGameProcess::sendSystemMessage:: NO IO DEVICE ... WILL FAIL\n");
00113 }
00114 
00115 void KGameProcess::sendMessage(QDataStream &stream,int msgid,quint32 receiver)
00116 {
00117   sendSystemMessage(stream,msgid+KGameMessage::IdUser,receiver);
00118 }
00119 
00120 void KGameProcess::processArgs(int argc, char *argv[])
00121 {
00122   int v=0;
00123   if (argc>2)
00124   {
00125     v=atoi(argv[2]);
00126     //kDebug(11001) << "cookie (unused) " << v;
00127   }
00128   if (argc>1)
00129   {
00130     v=atoi(argv[1]);
00131     //kDebug(11001) << "id (unused) " << v;
00132   }
00133   fprintf(stderr,"KGameProcess::processArgs \n");
00134   fflush(stderr);
00135 }
00136 
00137 void KGameProcess::receivedMessage(const QByteArray& receiveBuffer)
00138 {
00139  QDataStream stream(receiveBuffer);
00140  int msgid;
00141  quint32 sender; 
00142  quint32 receiver; 
00143  KGameMessage::extractHeader(stream, sender, receiver, msgid);
00144  fprintf(stderr,"--- KGameProcess::receivedMessage(): id=%d sender=%ld,recv=%ld\n",
00145          msgid,(unsigned long)sender,(unsigned long)receiver);
00146  switch(msgid)
00147  {
00148    case KGameMessage::IdTurn:
00149      qint8 b;
00150      stream >> b;
00151      emit signalTurn(stream,(bool)b);
00152    break;
00153    case KGameMessage::IdIOAdded:
00154      qint16 id;
00155      stream >> id;
00156      emit signalInit(stream,(int)id);
00157    break;
00158    default:
00159       emit signalCommand(stream,msgid-KGameMessage::IdUser,receiver,sender);
00160    break;
00161  }
00162 }
00163 
00164 KRandomSequence* KGameProcess::random()
00165 {
00166   return d->mRandom;
00167 }
00168 
00169 
00170 #include "kgameprocess.moc"

libkdegames/kgame

Skip menu "libkdegames/kgame"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal