/*--------------------------------------------------------------------------- ** MBUSCONNECT -- Connect the client to the message bus. In the case of ** a client we'll look first for a running Supervisor and notify them we've ** come online. The Client connection is complete when a MB_READY state ** is sent. */ int mbusConnect (char *whoAmI, char *group, int singleton) { int i, mytid, super, info, oldval; MBusPtr mbus = (MBusPtr) NULL; char msg[SZ_LINE]; pid_t pid = getpid(); /* Initialize host name. */ mbInitMBHost (); mbSetMBHost (); /* At this point we're only getting state information from the * application cache, we don't talk to the message bus yet. */ if (mbAppGet (APP_INIT)) { fprintf (stderr, "Application already initialized, resetting....\n"); mbInitApp(); /* FIXME: ....disconnect old client.....*/ } /*if (isSupervisor (whoAmI) ) {*/ if (MB_DEBUG) printf ("Re-Initializing default message bus...\n"); mbusInitialize (whoAmI, NULL); /* Attach this process to the message bus. */ if ((mytid = pvm_mytid()) < 0) { pvm_perror (whoAmI); exit (1); } oldval = pvm_setopt (PvmAutoErr, 0); /* disable libpvm msgs */ /*}*/ /* No MBus pointer found meaning we have no connection established * or context yet. If we're a SUPERVISOR, reinitialize. Otherwise, * now is the time to connect and set the state. */ mbus = mbAppGetMBus (); if (mbus == (MBusPtr) NULL) { /* Message bus not initialized by supervisor. * Attach this process to the message bus. */ if ((mytid = pvm_mytid()) < 0) { pvm_perror (whoAmI); exit (1); } oldval = pvm_setopt (PvmAutoErr, 0); /* disable libpvm msgs */ /* Allocate an message bus structure. We'll update the state from * the supervisor once connected and setup. */ if (MB_DEBUG) printf ("Allocating mbus struct ...\n"); mbus = (MBusPtr) calloc (1, sizeof (MBus)); } /********************** * CLIENT CODE **********************/ if (! isSupervisor (whoAmI)) { /* If we're a Client, look for a Supervisor. If we can't find a * Supervisor the simply return an error, individuals client may * handle it differently. */ for (i=MB_CONNECT_RETRYS; i; i--) { info = pvm_lookup (SUPERVISOR, -1, &super); if (info == PvmNoEntry) { fprintf (stderr, "Can't find Supervisor on msgbus, retrying\n"); sleep (1); } else { if (MB_DEBUG) { printf ("Supervisor on msgbus at tid=%d\n", super); printf ("Registering '%s' with VM....\n", whoAmI); } if (pvm_insert (whoAmI, -1, mytid) < 0) { fprintf (stderr, "Register of '%s' failed....\n", whoAmI); return (ERR); } else { /* Send the Supervisor a CONNECT message. */ memset (msg, 0, SZ_LINE); sprintf (msg, "{ tid=%d who=%s host=%s pid=%d }", mytid, whoAmI, mbGetMBHost(), pid); mbusSend (SUPERVISOR, ANY, MB_CONNECT, msg); } break; } } if (i == 0) { if (MB_VERBOSE) fprintf (stderr, "Supervisor not on msgbus, returning\n"); mytid *= (-1); super = ERR; /*return (((-1) * mytid)); */ } /********************* * SUPERVISOR CODE **********************/ } else if (isSupervisor (whoAmI)) { /* If we're a Supervisor, check whether we're already registered. */ info = pvm_lookup (SUPERVISOR, -1, &super); if (info == PvmBadParam) { fprintf (stderr, "Supervisor lookup error, BadParam...\n"); return (ERR); } else if (info == PvmNoEntry) { /* Supervisor does not exist */ if (MB_DEBUG) fprintf (stderr, "Supervisor not found, registering...\n"); if (pvm_insert (SUPERVISOR, -1, (super = mytid)) < 0) { fprintf (stderr, "Supervisor register failed....\n"); return (ERR); } } else if (info >= 0) { if (MB_DEBUG) fprintf (stderr, "Supervisor already registered at tid=%d, validating..\n", super); /* Try sending a message to the registered tid to see if it is * alive. */ if (mbusPing (super, 500) == OK) { if (singleton) { /* tell other supervisor we're taking over.... */ printf ("got a ping reply....\n"); } else { fprintf (stderr, "ERROR: Supervisor already registered at tid=%d\n", super); return (ERR); } } else { /* The registered supervisor didn't respond, so delete it * from the database and try again. */ extern int pvmreset(); if (MB_DEBUG) fprintf (stderr, "Cleaning up earlier super at %d(%d)...\n", info, super); /* FIXME: ....disconnect old supervisor.....*/ (void) pvmreset (mytid, 1, "", 0); if (pvm_delete (SUPERVISOR, info) != PvmOk) if (MB_DEBUG) fprintf (stderr, "Supervisor cleanup failed\n"); if ((info = pvm_kill (super)) != PvmOk) if (MB_DEBUG) fprintf (stderr, "Supervisor assasination failed\n"); /* goto lookup_; */ } } /* Broadcast a message to any running clients telling them we're * now running. */ memset (msg, 0, SZ_LINE); sprintf (msg, "{ tid=%d who=%s host=%s pid=%d }", super, SUPERVISOR, mbGetMBHost(), pid); mbusBcast (CLIENT, msg, MB_CONNECT); } /* Save the Supervisor location and other bits about this client. */ mbAppSet (APP_TID, mytid); mbAppSet (APP_STID, super); mbAppSet (APP_FD, mbGetMBusFD()); mbAppSetName (whoAmI); mbAppSetMBus ((MBusPtr) mbus); /* Install the exit handler so we're sure to make a clean getaway. */ atexit (mbusExitHandler); if (MB_DEBUG) fprintf (stderr, "mbConnect: whoAmI='%s' group='%s' mbus = 0x%x\n", whoAmI, group, (int) mbus); /* Join a specific group if specified. A process may be part of * multiple groups and so we use the mbus routine the same as a * caller would. */ if (group) mbusJoinGroup (group); /* Return our tid. */ return (mytid); }
int main(int argc, char* argv[]) { // Timeout for Message Receive struct timeval tmout; tmout.tv_usec=100; tmout.tv_sec=0; // Size for Data Packets int data_size; // Numer of running tasks int curcount; // Check if all data sent bool finished; // TID of host to send data int slave_tid; // Data Buffer for outgoing messages int buff; // Read the frequency list read_freq_list(FREQ_LIST_FILE); // Total number of Data Elements int data_count=freq_count; // Current possition in the array int current_data=0; // Store master-id in PVM { int master_id=pvm_mytid(); pvm_delete((char *) "master",0); pvm_insert((char *)"master",0,master_id); // Parameters for Spawn char *params[2]={(char *) "1", NULL}; char *slave=(char *) "matcher_slave"; int tids[HOST_COUNT]; int spawncount=pvm_spawn(slave,params,PvmTaskDefault,NULL,HOST_COUNT,tids); } // Get number of current tasks pvm_tasks(0,&curcount,NULL); finished=false; do { // Check for error if (pvm_probe(-1,MSG_SLAVE_ERROR)) { char error[32]; int err_no=0; pvm_recv(-1,MSG_SLAVE_ERROR); pvm_upkint(&slave_tid,1,1); pvm_upkint(&err_no,1,1); printf("Fehler in Slave %d Code %d\n",slave_tid,err_no); } // Send Data or End-Of-Data else if(pvm_trecv(-1,MSG_SLAVE_READY,&tmout)>0) { pvm_upkint(&slave_tid,1,1); // No more data if (finished) { pvm_initsend(PvmDataDefault); pvm_send(slave_tid,MSG_MASTER_EOD); } // Send data packet else { buff=pvm_initsend(PvmDataDefault); if (data_count>=PACK_SIZE) data_size=PACK_SIZE; else data_size=data_count; data_count-=data_size; pvm_pkint(&data_size,1,1); for(int ct=0;ct<data_size;ct++) { // Store the data in the buffer if (current_data<=freq_count) { pvm_pkstr(freq_list[current_data]->word.getValue()); pvm_pkulong(&(freq_list[current_data]->count),1,1); current_data++; } } pvm_initsend(PvmDataDefault); pvm_send(slave_tid,MSG_MASTER_DATA); printf("Send %d sets of %d to %d\n",data_size,data_count,slave_tid); if (data_count==0) finished=true; } } // Receive Result else if (pvm_probe(-1,MSG_SLAVE_RESULT)) { if (pvm_trecv(-1,MSG_SLAVE_RESULT,&tmout)>0) { pvm_upkint(&slave_tid,1,1); // Number of elements pvm_upkint(&data_size,1,1); for (int ct=0;ct<data_size;ct++) { unsigned long res1=0; unsigned long res2=0; // Read the result data /* to fill */ // pvm_upkulong(&res1,1,1); // pvm_upkulong(&res2,1,1); // printf("Result %llu\n",(unsigned long long int)res2*ULONG_MAX+res1); } PvmDataDefault pvm_send(slave_tid,MSG_MASTER_EXIT); } } pvm_tasks(0,&curcount,NULL); } while(curcount>2); pvm_exit(); return 0; }