File: mhf_server.c

package info (click to toggle)
pvm 3.4.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,284 kB
  • sloc: ansic: 72,074; makefile: 1,198; fortran: 631; sh: 285; csh: 74; asm: 37
file content (581 lines) | stat: -rw-r--r-- 17,229 bytes parent folder | download | duplicates (4)
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
576
577
578
579
580
581

static char rcsid[] =
	"$Id: mhf_server.c,v 1.5 2009/01/23 01:12:52 pvmsrc Exp $";

/*
 *         PVM version 3.4:  Parallel Virtual Machine System
 *               University of Tennessee, Knoxville TN.
 *           Oak Ridge National Laboratory, Oak Ridge TN.
 *                   Emory University, Atlanta GA.
 *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
 *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
 *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
 *                   (C) 1997 All Rights Reserved
 *
 *                              NOTICE
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted
 * provided that the above copyright notice appear in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * Neither the Institutions (Emory University, Oak Ridge National
 * Laboratory, and University of Tennessee) nor the Authors make any
 * representations about the suitability of this software for any
 * purpose.  This software is provided ``as is'' without express or
 * implied warranty.
 *
 * PVM version 3 was funded in part by the U.S. Department of Energy,
 * the National Science Foundation and the State of Tennessee.
 */

/***************************************************************
*	Filename: mhf_server.c
*
*  This is a "server" program for testing message handler functions.
*  Use the mhf_tickle program to speak to this server.
*
*  Operation:
*	1. startup pvm console
*	2. startup the mhf_server
*	3. startup as many mhf_tickle instances as you wish
*		- follow menu options and in-code comments on
*		  how to run and what it tests / shows...
*
***************************************************************/

#include <stdio.h>
#ifdef HASSTDLIB
#include <stdlib.h>
#endif
#ifdef	SYSVSTR
#include <string.h>
#else
#include <strings.h>
#endif
#include "pvm3.h"

#define NOISE1 1	 /* sets level of program noise  - minimum noise */
/* #define NOISE2 2	 each level "up" adds noise  */

#define  PMH 0		/* display present message handlers		*/
#define  LMH 1		/* load message handler				*/
#define  DMH 2		/* delete message handler			*/
#define  ERJ 3		/* tell mhf_server() to pvm_exit() and rejoin */
#define  HIT 4		/* message to hit a specified handler...	*/
#define GSMB 7		/* NOT USED IN mhf_server() - use to grab the */
					/* mhf_server contact info from mailbox again... */
#define CEAT 8		/* client (this program ) exit pvm and terminate */
					/* self  - server not contacted... */
#define SEAT 9		/* server exits pvm and terminate self		*/

#define SC1 1		/* string_catcher1 */
#define SC2 2		/* string_catcher2 */
#define SC3 3		/* string_catcher3 */
#define SC4 4		/* string_catcher4 */
#define SC5 5		/* string_catcher5 */
#define IC1 6		/* int_catcher6 */
#define IC2 7		/* int_catcher7 */
#define IC3 8		/* int_catcher8 */
#define IC4 9		/* int_catcher9 */
#define IC5 0		/* int_catcher10 */

#define STR_LEN 100
#define YES 1
#define NO 0

/*
*  globals to bring back message handler information
*/
int mhf_catch;		/* should be same as number of "catcher" hit */
int mhf_magic_number;
char mhf_msg[STR_LEN];


/**********************************************************************/
/**********************************************************************/
/**********************************************************************
*  register this server via "well known" mailbox so others may contact us
*  mailbox name is set to...  server = "mhf_server"
*
*  Returns mailbox index where placed
*/
int
register_server( mytid, ctx, machine, server )
int mytid;
int ctx;
char *machine;
char *server;
{	char *me = "register_server";
	int index;

	pvm_initsend( PvmDataDefault );
	pvm_pkint( &mytid, 1, 1 );
	pvm_pkint( &ctx, 1, 1 );
	pvm_pkstr( machine );
	if ( ( index = pvm_putinfo( server, pvm_getsbuf( ), PvmMboxDefault ) ) < 0 ) {
		/*
		 *  problem inserting mailbox entry - tell user why, exit pvm, and terminate task...
		 */
		switch ( index ) {
			case PvmExists:
				printf( "\n%s: Named service already running\n", me );
				break;
			case PvmBadParam:
				printf( "\n%s: Invalid argument to pvm_recvinfo( ).\n", me );
				break;
			case PvmNoSuchBuf:
				printf( "\n%s: Message buffer id does not exist.\n", me );
				break;
			case PvmDenied:
				printf( "\n%s: Key locked by another task, can't delete.\n", me );
				break;
			case PvmNoMem:
				printf( "\n%s: Libpvm is unable to allocate memory to pack data.\n", me );
				break;
			case PvmNotFound:
				printf( "\n%s: Requested key does not exist.\n", me );
				break;
			default:
				lpvmerr( "\n%s: task0.c: error %d", me, index );
				break;
		} /* end_switch */
		return( index );		/* returns error code */
	}
	printf( "%s: mailbox... just inserted server <%s> at mailbox %d with context %d.\n", 
		me, server, index, ctx );
	return( index );
} /* register_server */


/**********************************************************************
*  Function:	string_catcher1		- unpack 1 string
*		string_catcher2		- unpack 2 strings
*		string_catcher3		- unpack 3 strings
*		string_catcher4		- unpack 4 strings
*		string_catcher5		- unpack 5 strings
*
*		int_catcher6		- unpack 1 integer
*		int_catcher7		- unpack 2 integers
*		int_catcher8		- unpack 3 integers
*		int_catcher9		- unpack 4 integers
*		int_catcher10		- unpack 5 integers
*
*  These are all simple message handlers to use in testing loading,
*  deleting, and using message handlers.
*
*  Each one simply echos out the information that is being sent.
*  Thus showing that a given handler may "handle" messages of a given type.
**********************************************************************/
int
string_catcher1( mid )
int mid;
{	char *me = "string_catcher1";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkstr( mhf_msg );
	printf( "\n%s: string_catcher%d received message -->> <%s>\n\n", me, mhf_catch, mhf_msg );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* string_catcher1 */

int
string_catcher2( mid )
int mid;
{	char *me = "string_catcher2";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkstr( mhf_msg );
	printf( "\n%s: string_catcher%d received message -->> <%s>\n\n", me, mhf_catch, mhf_msg );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* string_catcher2 */

int
string_catcher3( mid )
int mid;
{	char *me = "string_catcher3";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkstr( mhf_msg );
	printf( "\n%s: string_catcher%d received message -->> <%s>\n\n", me, mhf_catch, mhf_msg );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* string_catcher3 */

int
string_catcher4( mid )
int mid;
{	char *me = "string_catcher4";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkstr( mhf_msg );
	printf( "\n%s: string_catcher%d received message -->> <%s>\n\n", me, mhf_catch, mhf_msg );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* string_catcher4 */

int
string_catcher5( mid )
int mid;
{	char *me = "string_catcher5";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkstr( mhf_msg );
	printf( "\n%s: string_catcher%d received message -->> <%s>\n\n", me, mhf_catch, mhf_msg );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* string_catcher5 */

int
int_catcher6( mid )
int mid;
{	char *me = "int_catcher6";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkint( &mhf_magic_number, 1, 1 );
	printf( "\n%s: int_catcher%d received magic number -->> %d.\n\n", me, mhf_catch, mhf_magic_number );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* int_catcher6 */

int
int_catcher7( mid )
int mid;
{	char *me = "int_catcher7";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkint( &mhf_magic_number, 1, 1 );
	printf( "\n%s: int_catcher%d received magic number -->> %d.\n\n", me, mhf_catch, mhf_magic_number );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* int_catcher7 */

int
int_catcher8( mid )
int mid;
{	char *me = "int_catcher8";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkint( &mhf_magic_number, 1, 1 );
	printf( "\n%s: int_catcher%d received magic number -->> %d.\n\n", me, mhf_catch, mhf_magic_number );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* int_catcher8 */

int
int_catcher9( mid )
int mid;
{	char *me = "int_catcher9";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkint( &mhf_magic_number, 1, 1 );
	printf( "\n%s: int_catcher%d received magic number -->> %d.\n\n", me, mhf_catch, mhf_magic_number );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* int_catcher9 */

int
int_catcher10( mid )
int mid;
{	char *me = "int_catcher10";
	int rbf;
	rbf = pvm_setrbuf( mid );

	pvm_upkint( &mhf_catch, 1, 1 );
	pvm_upkint( &mhf_magic_number, 1, 1 );
	printf( "\n%s: int_catcher%d received magic number -->> %d.\n\n", me, mhf_catch, mhf_magic_number );

	pvm_freebuf( pvm_setrbuf( rbf ) );
	return 0;
} /* int_catcher10 */
/**********************************************************************/
/**********************************************************************/


/**********************************************************************/
main ( argc, argv ) 
int argc;
char *argv[];
{	char *me = "mhf_server";
	int cc;			/* local stuff */
	int mytid;		/* tid of this server */
	int ctid;		/* tid of request sender - to this server */
	int msgtag;		/* sender specified message tag */
	int length;		/* length of message */
	int sctx;		/* server's context */
	int info;		/* status return and temporary input values */
	int mhid;		/* message handler id */
	int lmh;		/* int represent local message handler to ping */
	int index;		/* mailbox entry index */
	char msg[STR_LEN];	/* used for local info messages */
	char msg_txt[STR_LEN];	/* used for received messages */
	char machine[25];
	int FOREVER = YES;

	/*
	*  for display - note IC5 position
	*/
	static char *mh_arr[] = { "IC5", "SC1", "SC2", "SC3", "SC4", "SC5", "IC1",
			"IC2", "IC3", "IC4" };

	/* expected message information coming from "tickle" program */
	char action_tag;	/* action for server to take -> p, l, d, x j */
	int src;		/* message source = sender's tid */
	int tag;		/* message tag */
	int ctx;		/* message context */

	if ( ( mytid = pvm_mytid( ) ) < 0 ){
		printf( "\n%s: try starting pvm first...\n", me );
		exit( -1 );
	}

	gethostname( machine, 25 );
	sctx = pvm_newcontext( );		/* get new context */
	pvm_setcontext( sctx );			/* activate new context */
	printf( "%s: t%x on machine <%s> with context %d.\n",
			me, mytid, machine, pvm_getcontext( ) );
	if ( register_server( mytid, sctx, machine, me ) < 0 ){
		pvm_exit( );
		exit( -1 );
	}

	pvmdisplaymhfinfo( me, "startup...", mytid );

	/*
	*  startup message "server" to wait for messages
	*  leave when receive the "END" message
	*
	*  Remember: message handlers are keying on specific
	*	source - tag - context --> tuples...
	*/
	while( FOREVER )
	{
		if ( ( cc = pvm_recv( -1, -1 ) ) < 0 ){		/* receive all */
			lpvmerr( "\nmhf_server: pvm_recv( ): error %d", cc );
			pvm_exit( ); exit( 0 );
		}
		/* who sent message */
		if ( (info = pvm_bufinfo( cc, &length, &msgtag, &ctid )) < 0 ){
			lpvmerr( "\nmhf_server: pvm_bufinfo( ): error %d", info );
			pvm_exit( ); exit( 0 );
		}

		switch ( msgtag ){
		case PMH:  /* display present message handlers */
				pvm_upkstr( msg_txt );
				sprintf( msg, "%s: PMH: %s", me, msg_txt );
				pvmdisplaymhfinfo( me, msg, mytid );
				break;

		/* load message handler */
		/* ( src=tid_of_sender ...usually..., tag, ctx ) */
		case LMH:
				/* tid of msg handler ONLY user - unless wildcard -1 */
				pvm_upkint( &src, 1, 1 );
				pvm_upkint( &tag, 1, 1 );
				pvm_upkint( &ctx, 1, 1 );
				/* number indicates which handler to load */
				pvm_upkint( &info, 1, 1 );
#ifdef NOISE2
				sprintf( msg,
					"%s: LMH: BEFORE load with pvm_addmhf( src = t%x, tag = %d, ctx = %d )  -> load %s",
					me, src, tag, ctx, mh_arr[info] );
				pvmdisplaymhfinfo( me, msg, mytid );
#endif
				switch ( info ){
				case SC1:
					mhid = pvm_addmhf( src, tag, ctx, string_catcher1 );
					break;
				case SC2:
					mhid = pvm_addmhf( src, tag, ctx, string_catcher2 );
					break;
				case SC3:
					mhid = pvm_addmhf( src, tag, ctx, string_catcher3 );
					break;
				case SC4:
					mhid = pvm_addmhf( src, tag, ctx, string_catcher4 );
					break;
				case SC5:
					mhid = pvm_addmhf( src, tag, ctx, string_catcher5 );
					break;
				case IC1:
					mhid = pvm_addmhf( src, tag, ctx, int_catcher6 );
					break;
				case IC2:
					mhid = pvm_addmhf( src, tag, ctx, int_catcher7 );
					break;
				case IC3:
					mhid = pvm_addmhf( src, tag, ctx, int_catcher8 );
					break;
				case IC4:
					mhid = pvm_addmhf( src, tag, ctx, int_catcher9 );
					break;
				case IC5:
					mhid = pvm_addmhf( src, tag, ctx, int_catcher10 );
					break;
				default:
					printf( "\n%s: no such message handler <%d> to load...  Only 0 - 9 available!", me, info );
					mhid = PvmBadParam;
					break;
				} /* end_switch ( info ) */

				if ( mhid == PvmExists ){
					printf( "\n%s: message handler already exists...\n", me );
					break;
				}
				if ( mhid < 0 ){
					printf( "\n%s: pvm_addmhf failed with error code %d.\n", me, mhid );
					break;
				}
#ifdef NOISE1
				sprintf( msg, 
					"%s: LMH: AFTER load with pvm_addmhf( src = t%x, tag = %d, ctx = %d )  -> loaded %s", 
					me, src, tag, ctx, mh_arr[info] );
				pvmdisplaymhfinfo( me, msg, mytid );
#endif
				break;

		case DMH:  /* delete message handler */
				pvm_upkint( &mhid, 1, 1 );
#ifdef NOISE2
				sprintf( msg, "%s: DMH: BEFORE delete with pvm_delmhf( mhid = %d )", me, mhid );
				pvmdisplaymhfinfo( me, msg, mytid );
#endif
				if ( ( info = pvm_delmhf( mhid ) ) < 0 ){
					sprintf( msg,"%s: pvm_delmhf failed - no such handler existed.", me );
					lpvmerr( msg, PvmNotFound );
					break;
				}
				sprintf( msg, "%s: DMH: AFTER delete with pvm_delmhf( mhid = %d )", me, mhid );
				pvmdisplaymhfinfo( me, msg, mytid );
				break;

		case ERJ:  /* tell mhf_server() to pvm_exit( ) and then rejoin ->> check message handlers existance */
#ifdef NOISE2
				sprintf( msg, "%s: ERJ: BEFORE pvm_exit( )", me );
				pvmdisplaymhfinfo( me, msg, mytid );
#endif
				/*
				*  exit pvm part...
				*/
				if ( ( info = pvm_exit( ) ) < 0 ){
					printf( "\n%s: pvm_exit failed with error code %d.\n", me, info );
					break;
				}
#ifdef NOISE1
				printf( "\n%s: Just did pvm_exit...\n", me );
				sprintf( msg, "%s: ERJ: AFTER pvm_exit( )", me );
				pvmdisplaymhfinfo( me, msg, mytid );
#endif
				/*
				*  rejoin pvm part...
				*/
				if ( ( mytid = pvm_mytid( ) ) < 0 ){
					printf( "\n%s: could not restart - pvm not up...\n", me );
					exit( -1 );
				}

#ifdef NOISE1
				printf( "%s: just rejoined pvm...\n", me );
				printf( "%s: restarted as t%x on machine <%s> with context %d.\n", me, mytid, machine, pvm_getcontext( ) );
				sprintf( msg, "%s: ERJ: rejoin pvm AFTER pvm_exit( )", me );
				pvmdisplaymhfinfo( me, msg, mytid );
#endif
				/*
				*  re-register server via mailbox...
				*/
				if ( ( info = register_server( mytid, sctx, machine, me ) ) < 0 ){
					printf( "\n%s: register_server( ) failed with error code %d.\n", me, info );
					pvm_exit( );
					exit( -1 );
				}
#ifdef NOISE1
				printf( "%s: just registered server...\n", me );
#endif
				break;

		case HIT:  /* message to hit a specified handler... */

				pvm_upkint( &lmh, 1, 1 );	/* integer indicates which target trying to ping */

				if ( lmh == mhf_catch )
					printf( "\n%s: HIT:  ping target %d - %s   ", me, lmh, mh_arr[lmh] );
				else
					printf( "\n%s: NO HIT:  lmh = %d - %s   mhf_catch = %d - ?C?   ", 
						me, lmh, mh_arr[lmh], mhf_catch );

				switch ( lmh ) {
				case 1: case 2: case 3: case 4: case 5:
					pvm_upkstr( msg_txt );
					if ( strcmp( msg_txt, mhf_msg ) == 0 )
						printf( "strings match <%s>.\n", msg_txt );
					else
						printf( "strings NO match - local <%s>   mhf <%s>.\n", msg_txt, mhf_msg );
					break;
				case 0: case 6: case 7: case 8: case 9:
					pvm_upkint( &cc, 1, 1 );
					if ( cc == mhf_magic_number )
						printf( "magic_numbers match %d.\n", cc );
					else
						printf( "magic_numbers NO match - local %d   mhf %d.\n", cc, mhf_magic_number );
					break;
				}

				sprintf( msg, "%s: HIT:", me );
				pvmdisplaymhfinfo( me, msg, mytid );

				/*
				*  reset globals to some known values for testing
				*/
				mhf_catch = -99;
				mhf_magic_number = -99;
				strcpy ( mhf_msg, "NO MATCH" );
				break;

		case SEAT:  /* server exits pvm and terminate self */
				sprintf( msg, "%s: CEAT: Termiate Self...", me );
				pvmdisplaymhfinfo( me, msg, mytid );
				FOREVER = NO;				/* will cause program termination */
				break;

		default:/* invalid msgtag - just ignore the message
			*/
			printf( "%s: default: INVALID msgtag:   length = %d     msgtag = <%d>     ctid = t%x", 
				me, length, msgtag, ctid );
			break;
		} /* switch_end */
		fflush( stdout );
	} /* end_while_FOREVER */
	pvm_exit( );
	exit( 0 );
} /* end_main */