/******************************************************************************/
/************************** MM-Client *****************************************/
/******************************************************************************/
/* Parameters: hostname portnumber numfiles                                   */ 
/******************************************************************************/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include "mm_prot.h"

#define STREAMPATH "/mnt/tmp/streams"

int init();

//############################################################################
int main(int argc, char *argv[]) 
{ 
  int                sock,rval,i,j,quit,acc,rej,end;
  char               buf[4096];
  hello_pkt          hello;
  accept_pkt         *accept;
  reject_pkt         *reject;

  if (argc<4) { 
    printf("USAGE: mm_client hostname portnumber numfiles\n");
    return -1; 
  };
  acc=rej=end=0;
  sock=init(argc,argv);
  strcpy(hello.tag,"HELLO");
  hello.fpos=0;     //starting at the beginning
  hello.rate=196608;   // in byte/sec --> 1.5 MBit/sec
  
  i=1;
  while (i<9999) { //ends when the first client is dequeued
  if (i<65) { //max num of concurrent clients
    i++;
    sprintf(buf,"%s/stream%i",STREAMPATH,i%atoi(argv[3]));
    strncpy(hello.file,buf,STRLEN);
    //printf("(%3i) sending HELLO for %s...",i,hello.file);
    if (write(sock, &hello, sizeof(hello)) < 0)
      perror("sending HELLO");
  }
    //printf("waiting for answer..."); fflush(NULL);
    quit=0;

    do { //wait for answer, silently ignores real data-packages
      bzero(&buf,sizeof(buf));
      if ((rval=recv(sock,&buf,sizeof(buf),0))<0) {
        perror("recv servers answer");
      }
      if (strcmp(buf,"ACCPT")==0) {
        accept=(accept_pkt*)buf;
        //printf("ACCEPTED!  :)\n");
        quit=1; //got answer
        acc++; //one more client accepted by server
      }
      if (strcmp(buf,"REJCT")==0) {
        reject=(reject_pkt*)buf;
        //printf("REJECTED!  :(\n");
        quit=1; //got answer
        rej++; //one more client rejected by server
      }
      if (strcmp(buf,"END")==0) {
        //printf("END!\n");
        end++; //one more client dequeued
        i=9999; //go to next file-sharing-level
        quit=1; //got answer
      }
    } while(quit==0);
  }//while i<9999

  printf("acc: %d rej: %d end: %d\n",acc,rej,end);
  close(sock);
  return 0;

}//main


//############################################################################
int init(int argc, char *argv[]) {
 int                sock, length;
 struct sockaddr_in server;
 struct hostent    *hp;

  /*---------------------------- Create a socket -----------------------------*/
  sock = socket(PF_INET, SOCK_DGRAM, 0);
   if (sock < 0) {    perror("opening dgram socket");
     exit(1);
  }

  /*---------- Connect socket using name specified by command line -----------*/
  server.sin_family = AF_INET;
   hp                = gethostbyname(argv[1]);
  if (hp == 0) {
    fprintf(stderr, "%s: unknown host\n", argv[1]);
    exit(2);
   }
    bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
  server.sin_port = htons(atoi(argv[2]));

  if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
    perror("connecting stream socket");
     exit(1);
  }
  length = sizeof(server);
   if (getsockname(sock, (struct sockaddr *) &server, &length)) {
    perror("getting socket name");
     exit(1);
  }

   printf("Port #%d ", ntohs(server.sin_port));

  return sock;
}//init


