// To run this example do
// 1) start server using
//    % test_xrecv {number windows}
//    This opens the specified number of windows where images will be displayed.
//    The packets ids determine to which window received each image will go.
// 2) send packet to server using
//    % test_xsend ../../FastTrack/images/board.dib
// This sends to the server the same given image in 5 packets with ids 0,...,4

#include "FastTrack.h"
#include "NCS.h"

#define SERVERIP "128.111.56.204"  // tamina
//#define SERVERIP "128.111.58.116"  // shelley

// 8bit encoding for UDP
//#define UDPCONVERT(a,b) Convert16to8(a,b)

// 4bit encoding for UDP
//#define UDPCONVERT(a,b) Convert16to4(a,b)

// 8bit RLE encoding for UDP
#define UDPCONVERT(a,b) Convert16to8RLE(a,b,16)

int main(int argc, char *argv[])
{
  int i;
  struct timeval time_stamp;
  FT_16GrayImage_T Image;
  FT_8GrayImage_T  buffer;
  int sockfd;
  unsigned long seq=0;

  ////// initialize gtk /////////////////////////////////////////////
  gtk_init(&argc, &argv);
  gdk_rgb_init();

  if (argc != 2) {
    fprintf(stderr,"%s: wrong number of arguments (%d found, 1 expected)\n",
	    argv[0],argc-1);
    exit(1);
  }

  sockfd=InitSendUDPSocket(SERVERIP,NCS_SEND_PORT);

  LoadImage(argv[1],&Image);
  gettimeofday(&time_stamp,NULL);

  fprintf(stderr,"filename \"%s\", width = %d, height = %d, len = %d\n",
	  argv[1],Image.width,Image.height,Image.pixelslength);
  UDPCONVERT(&Image,(void*)&buffer);
  fprintf(stderr,"buffer width = %d, height = %d, mem = 0x%lx, len = %d (%d)\n",
	  buffer.width,buffer.height,((long*)buffer.pixels)[0],buffer.pixelslength,FT_IMAGESIZE(&buffer));

  int length=FT_IMAGESIZE(&buffer);

  if (FT_IMAGESIZE(&buffer)>16384) {
    printf("data larger than 16384, expect problems!!!\n"); 
  }

  for (i=0;i<5;i++)
  SendUDPSocket(sockfd,               // socket handle                  (input) 
		i,                    // packet sender id               (input)
		&seq,                 // sequence number of last packet (input)
		&time_stamp,          // data time stamp for packet     (input)
		(char*)&buffer,       // data in packet                 (input)
		length);// length of data (in bytes)     (input)

  close(sockfd);
  return 0;
}


