• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • kgame
kgameprocess.cpp
1 /*
2  This file is part of the TDE games library
3  Copyright (C) 2001 Martin Heni (martin@heni-online.de)
4  Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library 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 GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 /*
21  $Id$
22 */
23 
24 #include "kgameprocess.h"
25 #include "kplayer.h"
26 #include "kgame.h"
27 #include "kgamemessage.h"
28 #include "kmessageio.h"
29 
30 #include <krandomsequence.h>
31 
32 #include <tqbuffer.h>
33 #include <tqdatastream.h>
34 #include <tqcstring.h>
35 
36 #include <assert.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 
42 #define READ_BUFFER_SIZE 1024
43 
44 // ----------------------- Process Child ---------------------------
45 
46 KGameProcess::KGameProcess() : TQObject(0,0)
47 {
48  mTerminate=false;
49  // Check whether a player is set. If not create one!
50  rFile.open(IO_ReadOnly|IO_Raw,stdin);
51  wFile.open(IO_WriteOnly|IO_Raw,stdout);
52  mMessageIO=new KMessageFilePipe(this,&rFile,&wFile);
53 // mMessageClient=new KMessageClient(this);
54 // mMessageClient->setServer(mMessageIO);
55 // connect (mMessageClient, TQ_SIGNAL(broadcastReceived(const TQByteArray&, TQ_UINT32)),
56 // this, TQ_SLOT(receivedMessage(const TQByteArray&, TQ_UINT32)));
57  connect (mMessageIO, TQ_SIGNAL(received(const TQByteArray&)),
58  this, TQ_SLOT(receivedMessage(const TQByteArray&)));
59  fprintf(stderr,"KGameProcess::constructor %p %p\n",&rFile,&wFile);
60 
61  mRandom = new KRandomSequence;
62  mRandom->setSeed(0);
63 }
64 KGameProcess::~KGameProcess()
65 {
66  delete mRandom;
67  //delete mMessageClient;
68  //delete mMessageServer;
69  delete mMessageIO;
70  rFile.close();
71  wFile.close();
72  fprintf(stderr,"KGameProcess::destructor\n");
73 }
74 
75 
76 bool KGameProcess::exec(int argc, char *argv[])
77 {
78  // Get id and cookie, ... from command line
79  processArgs(argc,argv);
80  do
81  {
82  mMessageIO->exec();
83  } while(!mTerminate);
84  return true;
85 }
86 
87 // You have to do this to create a message
88 // TQByteArray buffer;
89 // TQDataStream wstream(buffer,IO_WriteOnly);
90 // then stream data into the stream and call this function
91 void KGameProcess::sendSystemMessage(TQDataStream &stream,int msgid,TQ_UINT32 receiver)
92 {
93  fprintf(stderr,"KGameProcess::sendMessage id=%d recv=%d",msgid,receiver);
94  TQByteArray a;
95  TQDataStream outstream(a,IO_WriteOnly);
96 
97  TQBuffer *device=(TQBuffer *)stream.device();
98  TQByteArray data=device->buffer();;
99 
100  KGameMessage::createHeader(outstream,0,receiver,msgid);
101  outstream.writeRawBytes(data.data(),data.size());
102 
103  //if (mMessageClient) mMessageClient->sendBroadcast(a);
104  // TODO: The fixed received 2 will cause problems. But how to address the
105  // proper one?
106 // if (mMessageClient) mMessageClient->sendForward(a,2);
107  if (mMessageIO) mMessageIO->send(a);
108 }
109 
110 void KGameProcess::sendMessage(TQDataStream &stream,int msgid,TQ_UINT32 receiver)
111 {
112  sendSystemMessage(stream,msgid+KGameMessage::IdUser,receiver);
113 }
114 
115 void KGameProcess::processArgs(int argc, char *argv[])
116 {
117  int v=0;
118  if (argc>2)
119  {
120  v=atoi(argv[2]);
121  //kdDebug(11001) << "cookie (unused) " << v << endl;
122  }
123  if (argc>1)
124  {
125  v=atoi(argv[1]);
126  //kdDebug(11001) << "id (unused) " << v << endl;
127  }
128  fprintf(stderr,"processArgs \n");
129  fflush(stderr);
130 }
131 
132 void KGameProcess::receivedMessage(const TQByteArray& receiveBuffer)
133 {
134  TQDataStream stream(receiveBuffer, IO_ReadOnly);
135  int msgid;
136  TQ_UINT32 sender;
137  TQ_UINT32 receiver;
138  KGameMessage::extractHeader(stream, sender, receiver, msgid);
139  fprintf(stderr,"------ receiveNetworkTransmission(): id=%d sender=%d,recv=%d\n",msgid,sender,receiver);
140  switch(msgid)
141  {
142  case KGameMessage::IdTurn:
143  TQ_INT8 b;
144  stream >> b;
145  emit signalTurn(stream,(bool)b);
146  break;
147  case KGameMessage::IdIOAdded:
148  TQ_INT16 id;
149  stream >> id;
150  emit signalInit(stream,(int)id);
151  break;
152  default:
153  emit signalCommand(stream,msgid-KGameMessage::IdUser,receiver,sender);
154  break;
155  }
156 }
157 
158 #include "kgameprocess.moc"
KGameProcess::receivedMessage
void receivedMessage(const TQByteArray &receiveBuffer)
A message is received via the interprocess connection.
Definition: kgameprocess.cpp:132
KGameProcess::signalInit
void signalInit(TQDataStream &stream, int userid)
This signal is emmited when the process is initialized, i.e.
KGameProcess::exec
bool exec(int argc, char *argv[])
Enters the event loop of the computer process.
Definition: kgameprocess.cpp:76
KGameProcess::KGameProcess
KGameProcess()
Creates a KGameProcess class.
Definition: kgameprocess.cpp:46
KGameProcess::signalCommand
void signalCommand(TQDataStream &inputStream, int msgid, int receiver, int sender)
The generic communication signal.
KGameProcess::~KGameProcess
~KGameProcess()
Destruct the process.
Definition: kgameprocess.cpp:64
KGameProcess::sendSystemMessage
void sendSystemMessage(TQDataStream &stream, int msgid, TQ_UINT32 receiver=0)
Sends a system message to the corresonding KGameIO device.
Definition: kgameprocess.cpp:91
KGameProcess::processArgs
void processArgs(int argc, char *argv[])
processes the command line argumens to set up the computer player Pass the argumens exactely as given...
Definition: kgameprocess.cpp:115
KGameProcess::sendMessage
void sendMessage(TQDataStream &stream, int msgid, TQ_UINT32 receiver=0)
Sends a message to the corresponding KGameIO device.
Definition: kgameprocess.cpp:110
KGameProcess::signalTurn
void signalTurn(TQDataStream &stream, bool turn)
This signal is emmited if the computer player should perform a turn.

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.1
This website is maintained by Timothy Pearson.