/*****************************************************************************
* 	Project 	: Network Based Message Queue System		    **
*	Course		: CIS 650 - Software Engineering		    **
*	Location 	: ~sameer/cis650/proj/sr                 	    **
*	File		: s1.cc		       	       			    **
*	Description 	: A server - an echo server example. 		    **
*****************************************************************************/

#include <iostream.h>
#include "uai.h"

struct nmsgbuf *ptr;


main()
{
  int msqid, n1, n2, clientID, serverID;

  msqid = nmsgget(6, 5);

  serverID = ngetownid();
  cout << "server id is " << serverID << endl;

  ptr = new nmsgbuf;

  for (;;) {
    /* receive a messge */
    n1 = nmsgrcv(msqid, ptr, 256, 1);
    cout << "client uniqueID is" << endl;
    cout << ptr->mtype << endl;
    if (n1 < 0) {
      cout << "nmsgrcv error" << endl;
      exit(1);
    }
    clientID = ptr->mtype;

    /* process information here */
    /* process information here */
    if ((ptr->mtext[0] == -1) && (ptr->mtext[1] == 0))
    {
        printf("Received KILL message - exiting\n");
        exit(0);
    }


    cout << "server: received from client" << endl;
    cout << ptr->mtext << endl;

    /* send the result */
    ptr->mtype = clientID;
    n2 = nmsgsnd(msqid, ptr, 256, 0);
    if (n2 < 0) {
      cout << "nmsgsnd error" << endl;
      exit(1);
    }
  }
  delete ptr;
}



