summaryrefslogtreecommitdiffstats
path: root/libksirtet/lib/meeting.cpp
blob: 58acf205c09390f7b3f938adb98193e8d08b73f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#include "meeting.h"

#include <tqmessagebox.h>
#include <tqpushbutton.h>

#include <klocale.h>
#include <kmessagebox.h>

#include "defines.h"
#include "mp_option.h"

#define LIST_INTERVAL 0


NetMeeting::NetMeeting(const cId &_id, Socket *socket,
                       MPOptionWidget *option,
					   bool _server, TQWidget *parent, const char * name)
: KDialogBase(Plain, i18n("Network Meeting"),
			  (_server ? Ok|Cancel|Help : Cancel|Help),
			  (_server ? Ok : Cancel), parent, name),
  server(_server), ow(option), id(_id), socketRemoved(FALSE)
{
	sm.append(socket, SocketManager::ReadWrite);
	sm[0]->notifier()->setEnabled(TRUE);

/* top tqlayout */
	TQVBoxLayout *top = new TQVBoxLayout(plainPage(), spacingHint());
	top->setResizeMode(TQLayout::Fixed);

	// server line
	spl = new MeetingLine(server, server, true, plainPage());
	top->addWidget(spl);

	// widget list
	wl = new WidgetList<MeetingLine>(LIST_INTERVAL, plainPage());
	wl->hide();
	top->addWidget(wl);

	labWait = new TQLabel(i18n("Waiting for clients"), plainPage());
	labWait->setAlignment(AlignCenter);
	top->addWidget(labWait);

	// options widget
//	if (ow)	top->addWidget(ow); #### FIXME

    // status bar
	status = new TQStatusBar(plainPage());
	status->setSizeGripEnabled(false);
	top->addWidget(status);

	// buttons
	enableButtonSeparator(TRUE);
	if (server) {
		setButtonOK(i18n("Start Game"));
		enableButtonOK(FALSE);
	}
	setButtonCancel(server ? i18n("Abort") : i18n("Quit"));
	enableButton(Help, FALSE);
}

NetMeeting::~NetMeeting()
{}

void NetMeeting::appendLine(const MeetingLineData &pld, bool server)
{
	MeetingLine *pl;
	pl = new MeetingLine(pld.own, server, false, wl);
	if (pld.own) connect(pl, TQT_SIGNAL(textChanged(const TQString &)),
						 TQT_SLOT(textChanged(const TQString &)));
	else message(i18n("A new client has just arrived (#%1)")
				 .arg(wl->size()+1));
	pl->setData(pld.ed);
	connect(pl, TQT_SIGNAL(typeChanged(MeetingCheckBox::Type)),
			TQT_SLOT(typeChanged(MeetingCheckBox::Type)));
	wl->append(pl);
	waiting();
}

void NetMeeting::removeLine(uint i)
{
	wl->remove(i);
	waiting();
}

void NetMeeting::waiting()
{
	if ( wl->size() ) {
		labWait->hide();
		wl->show();
	} else {
		labWait->show();
		wl->hide();
	}
	if (server) enableButtonOK(ready());
}

void NetMeeting::setType(const TypeInfo &ti)
{
	if ( ti.i==0 ) spl->setType(ti.type); // in fact should not append
	else {
		wl->widget(ti.i-1)->setType(ti.type);
		if (server) enableButtonOK(ready());
	}
}

void NetMeeting::setText(const TextInfo &ti)
{
	if ( ti.i==0 ) spl->setText(ti.text);
	else wl->widget(ti.i-1)->setText(ti.text);
}

bool NetMeeting::ready() const
{
	int nbReady = 0;
	for(uint k=0; k<wl->size(); k++) {
		switch ( wl->widget(k)->type() ) {
		 case MeetingCheckBox::Ready    : nbReady++; break;
		 case MeetingCheckBox::NotReady : return FALSE;
		 default : break;
		}
	}
	return ( nbReady!=0 );
}

void NetMeeting::cleanReject(const TQString &str)
{
	sm.clean(); // remove the sockets immediately to avoid possible further mess
	if ( !str.isEmpty() )
		KMessageBox::information(this, str, caption());
	KDialogBase::reject();
}

#define WRITE(i)      if ( !sm[i]->write() ) { writeError(i); return; }
#define CHECK_READ(i) if ( !sm[i]->readingStream().readOk() ) { dataError(i);  return; }

// Read incoming data
void NetMeeting::readNotifier(int fd)
{
	int i = sm.find(fd);
    Q_ASSERT( i!=-1 );
	switch ( sm[i]->read() ) {
	 case -1: readError(i);  break;
	 case  0: brokeError(i); break;
	 default: readData(i);
	}
}

void NetMeeting::readData(uint i)
{
	// get message type
	MeetingMsgFlag mt;
	sm[i]->readingStream() >> mt;
	CHECK_READ(i);
	switch (mt) {
	 case EndFlag:      endFlag(i);     break;
	 case NewFlag:      newFlag(i);     break;
	 case Mod_TextFlag: modTextFlag(i); break;
	 case Mod_TypeFlag: modTypeFlag(i); break;
	 case IdFlag:       idFlag(i);      break;
	 case DelFlag:      delFlag(i);     break;
	 case Mod_OptFlag:  modOptFlag(i);  break;
	 case PlayFlag:     playFlag(i);    break;
	 default:       dataError(i);
	}

	if (socketRemoved) socketRemoved = FALSE;
	else if ( !sm[i]->readingStream().atEnd() )
		readData(i); // more pending data
}

void NetMeeting::readError(uint i)
    { netError(i, i18n("Error reading data from")); }
void NetMeeting::dataError(uint i)
    { netError(i, i18n("Unknown data from")); }
void NetMeeting::writeError(uint i)
    { netError(i, i18n("Error writing to")); }
void NetMeeting::brokeError(uint i)
    { netError(i, i18n("Link broken or empty data from")); }

bool NetMeeting::checkState(uint i, PlayerState s)
{
	bool ok = ( players[i]==s );
	if (!ok) dataError(i);
	return ok;
}

bool NetMeeting::checkAndSetState(uint i, PlayerState os, PlayerState ns)
{
	bool ok = checkState(i, os);
	if (ok) players[i] = ns;
	return ok;
}

void NetMeeting::reject()
{
	// send an End flag
	sm.commonWritingStream() << EndFlag;
	writeToAll();

	cleanReject();
}

void NetMeeting::accept()
{
    KDialogBase::accept();
}

void NetMeeting::message(const TQString &str)
{
	status->message(str, 3000);
}

/** ServerNetMeeting *********************************************************/
ServerNetMeeting::ServerNetMeeting(const cId &id,
	const RemoteHostData &r, MPOptionWidget *option,
	TQPtrList<RemoteHostData> &arhd, TQWidget *parent, const char * name)
: NetMeeting(id, r.socket, option, TRUE, parent, name), rhd(arhd)
{
	connect(sm[0]->notifier(), TQT_SIGNAL(activated(int)), TQT_SLOT(newHost(int)));
	players.append(Accepted); // server

	// set server line
	ExtData ed(r.bds, "", MeetingCheckBox::Ready);
	spl->setData(ed);
	connect(spl, TQT_SIGNAL(textChanged(const TQString &)),
			TQT_SLOT(textChanged(const TQString &)));

	// options signal
	if (ow) connect(ow, TQT_SIGNAL(changed()), TQT_SLOT(optionsChanged()));
}

void ServerNetMeeting::writeToAll(uint i)
{
	for (uint k=1; k<sm.size(); k++) {
		if ( k==i ) continue;
		if ( !sm.writeCommon(k) ) writeError(k);
	}
	sm.commonWritingStream().clear();
}

void ServerNetMeeting::netError(uint i, const TQString &type)
{
	Q_ASSERT( i!=0 );
	disconnectHost(i, i18n("%1 client #%2: disconnect it").arg(type).arg(i));
}

void ServerNetMeeting::disconnectHost(uint i, const TQString &str)
{
	sm.remove(i, true);
	socketRemoved = TRUE;
	if ( players[i]==Accepted ) {
		removeLine(i-1);

		// Send a Del message to all (other) clients
		sm.commonWritingStream() << DelFlag << i;
		writeToAll();
	}
	players.remove(players.at(i));
	message(str);
}

void ServerNetMeeting::newHost(int)
{
    KExtendedSocket *s;
    int res = sm[0]->accept(s);
    if ( res!=0 ) {
        message(i18n("Failed to accept incoming client:\n%1")
                .arg(socketError(s)));
        return;
    }
	players.append(NewPlayer);
    Socket *socket = new Socket(s, true);
	uint i = sm.append(socket, SocketManager::ReadWrite);
	connect(sm[i]->notifier(), TQT_SIGNAL(activated(int)),
            TQT_SLOT(readNotifier(int)));
	sm[i]->notifier()->setEnabled(TRUE);
}

void ServerNetMeeting::idFlag(uint i)
{
	bool b = checkAndSetState(i, NewPlayer, IdChecked);
	Q_ASSERT(b);

	// get client id
	cId clientId;
	sm[i]->readingStream() >> clientId;
	CHECK_READ(i);

	// compare id
	id.check(clientId);

	// send result to client
	Stream &s = sm[i]->writingStream();
	s << IdFlag << id;
	WRITE(i);

	// if not accepted : remove socket and player from list
	if ( !id.accepted() )
        disconnectHost(i, i18n("Client rejected for incompatible ID"));
}

void ServerNetMeeting::endFlag(uint i)
{
	disconnectHost(i, i18n("Client #%1 has left").arg(i));
}

void ServerNetMeeting::newFlag(uint i)
{
	checkAndSetState(i, IdChecked, Accepted);

	// get line infos from new client (GameData struct)
	MeetingLineData pld;
	sm[i]->readingStream() >> pld.ed.bds;
	CHECK_READ(i);

	// complete the MeetingLineData struct with initial values
	pld.own     = FALSE;                 // client line
	pld.ed.type = MeetingCheckBox::NotReady; // not ready by default
    pld.ed.text = "";                    // empty line to begin with
	appendLine(pld, TRUE);

	// send to the new client already present lines including its own
	// (New flag + MeetingLineData struct)
	spl->data(pld.ed);
	sm[i]->writingStream() << NewFlag << pld.ed;
	for(uint k=1; k<sm.size(); k++) {
		wl->widget(k-1)->data(pld.ed);
		pld.own = ( k==i );
		sm[i]->writingStream() << NewFlag << pld;
	}
	WRITE(i);

	// send to all other clients the new line (New flag + MeetingLineData struct)
	wl->widget(i-1)->data(pld.ed);
	pld.own = FALSE;
	sm.commonWritingStream() << NewFlag << pld;
	writeToAll(i);
}

void ServerNetMeeting::modTextFlag(uint i)
{
	checkState(i-1, Accepted);

	// the client i has just sent a new text (TQString)
	TextInfo ti;
	sm[i]->readingStream() >> ti.text;
	CHECK_READ(i);
	ti.i = i;
	setText(ti);

	// send it to all other clients (Mod_Text flag + TextInfo struct)
	sm.commonWritingStream() << Mod_TextFlag << ti;
	writeToAll(i);
}

void ServerNetMeeting::modTypeFlag(uint i)
{
	checkState(i-1, Accepted);

	// a client has just sent a new TCB type (TCB type)
	TypeInfo ti;
	sm[i]->readingStream() >> ti.type;
	CHECK_READ(i);
	ti.i = i;
	setType(ti);

	// send it to all other clients (Mod_Type flag + TypeInfo struct)
	sm.commonWritingStream() << Mod_TypeFlag << ti;
	writeToAll(i);
}

void ServerNetMeeting::textChanged(const TQString &text)
{
	// server line text changed : send to every clients (Mod_Text flag + TextInfo struct)
	TextInfo ti; ti.i = 0; ti.text = text;
	sm.commonWritingStream() << Mod_TextFlag << ti;
	writeToAll();
}

void ServerNetMeeting::typeChanged(MeetingCheckBox::Type type)
{
	Q_ASSERT( sender()!=spl ); // server TCB not modifiable
	// the server has changed a client TCB

	// find the changed TCB index
	TypeInfo ty;
	ty.type = type;
	for (ty.i=0; ty.i<wl->size(); ty.i++)
		if ( sender()==wl->widget(ty.i) ) break;
	ty.i++;

	// TCB change : send to every clients (Mod_Type flag + TypeInfo struct)
	sm.commonWritingStream() << Mod_TypeFlag << ty;
	writeToAll();
	if (server) enableButtonOK(ready());
}

void ServerNetMeeting::accept()
{
	Q_ASSERT( ready() && rhd.count()==0 );

	// stop receiving data from clients (will be buffered by OS)
	for (uint k=0; k<sm.size(); k++) disconnect(sm[k]->notifier());
	sm.remove(0, true);

	// check which client will play and fill RemoteHostData array
	ExtData ed;
	bool willPlay;
	for (uint k=1; k<players.count(); k++) {
		willPlay = FALSE;

		if ( players[k]==Accepted ) { // client with lines
				wl->widget(k-1)->data(ed);
			if ( ed.type==MeetingCheckBox::Ready ) {
				willPlay  = TRUE;
				RemoteHostData *r = new RemoteHostData;
				r->socket = sm[0];
				r->bds    = ed.bds;
				rhd.append(r);
			}

			// send play message to client (Play flag
			// + bool [accepted/rejected])
			sm[0]->writingStream() << PlayFlag << (TQ_UINT8)willPlay;
			// if write failed and the client is not playing : silently
			// put it aside ...
			if ( !sm[0]->write() && willPlay ) {
				cleanReject(i18n("Unable to write to client #%1 at game "
                                 "beginning."));
				return;
			}
		}

        sm[0]->notifier()->setEnabled(false);
		sm.remove(0, !willPlay);
	}

	NetMeeting::accept();
}

void ServerNetMeeting::optionsChanged()
{
	sm.commonWritingStream() << Mod_OptFlag;
	ow->dataOut( sm.commonWritingStream() );
	writeToAll();
}

/** ClientNetMeeting *********************************************************/
ClientNetMeeting::ClientNetMeeting(const cId &id,
		const RemoteHostData &rhd, MPOptionWidget *option,
	    TQWidget *parent, const char * name)
: NetMeeting(id, rhd.socket, option, FALSE, parent, name), bds(rhd.bds)
{
	connect(sm[0]->notifier(), TQT_SIGNAL(activated(int)),
			TQT_SLOT(readNotifier(int)));
	players.append(NewPlayer); // server player

	// Send id to server (Id flag + Id struct)
	sm.commonWritingStream() << IdFlag << id;
	writeToAll(); // what happens if there is a message box appearing before exec() call ??
}

void ClientNetMeeting::netError(uint, const TQString &str)
{
	cleanReject(i18n("%1 server: aborting connection.").arg(str));
}

void ClientNetMeeting::writeToAll(uint)
{
	if ( !sm.writeCommon(0) ) writeError(0);
	sm.commonWritingStream().clear();
}

void ClientNetMeeting::idFlag(uint)
{
	checkAndSetState(0, NewPlayer, IdChecked);

	// read Id result (Id flag + Id struct)
	cId serverId;
	sm[0]->readingStream() >> serverId;
	CHECK_READ(0);

	// check result
	if ( !serverId.accepted() ) cleanReject(serverId.errorMessage(id));
	else {
		// send client info (New flag + GameData struct)
		sm.commonWritingStream() << NewFlag << bds;
		writeToAll();
	}
}

void ClientNetMeeting::newFlag(uint)
{
	if ( players[0]==IdChecked ) {
		ExtData ed;
		sm[0]->readingStream() >> ed;
		spl->setData(ed);
		players[0] = Accepted;
	} else {
		MeetingLineData pld;
		sm[0]->readingStream() >> pld;
		appendLine(pld, FALSE);
	}
	CHECK_READ(0);
}

void ClientNetMeeting::modTextFlag(uint)
{
	// receive new text from server (TextInfo struct)
	TextInfo ti;
	sm[0]->readingStream() >> ti;
	CHECK_READ(0);
	setText(ti);
}

void ClientNetMeeting::modTypeFlag(uint)
{
	// receive new type from server (TypeInfo struct)
	TypeInfo ti;
	sm[0]->readingStream() >> ti;
	CHECK_READ(0);
	setType(ti);
}

void ClientNetMeeting::delFlag(uint)
{
	// receive client number (uint)
	uint k;
	sm[0]->readingStream() >> k;
	CHECK_READ(0);
	removeLine(k-1);
	message(i18n("Client %1 has left").arg(k));
}

void ClientNetMeeting::textChanged(const TQString &text)
{
	// text changed : send to server (Mod_Text flag + TQString)
	sm.commonWritingStream() << Mod_TextFlag << text;
	writeToAll();
}

void ClientNetMeeting::typeChanged(MeetingCheckBox::Type type)
{
	// type changed : send to server (Mod_Type flag + TCB)
	sm.commonWritingStream() << Mod_TypeFlag << type;
	writeToAll();
}

void ClientNetMeeting::playFlag(uint)
{
	// receive accept or reject (bool)
	TQ_UINT8 i;
	sm[0]->readingStream() >> i;
	CHECK_READ(0);
    sm[0]->notifier()->setEnabled(false);
    sm.remove(0, i==0);
    socketRemoved = true;
	if (i) accept();
	else cleanReject(i18n("The game has begun without you\n"
                          "(You have been excluded by the server)."));
}

void ClientNetMeeting::modOptFlag(uint)
{
	// read new option data
	ow->dataIn( sm[0]->readingStream() );
	CHECK_READ(0);
}

void ClientNetMeeting::endFlag(uint)
{
	// abort from server
	cleanReject(i18n("The server has aborted the game."));
}
#include "meeting.moc"