#include <stdio.h>
#include "../registrar/protocol.h"
#include "../registrar/return_codes.h"
#include "inet.h"
#include "client.h"
#include "buffer.h"

char * next_field (buffer_t *b);

#define SBUFLEN 2000
static char     sbuf[2000];
buffer_t serv_client_buf;
message_header_t  message_header;

int sock;
#define terminator(str) (!strncmp(str,EOM,EOM_LEN))

/*
 * net_init establishes connection with the server
 */
net_init()
{
  int ssock;
  ssock = inet_establish_connection(SERVER, PORT, 0);
  if (ssock == -1)
    return 0;
  sock = ssock;
  printf ("Client, net_init, connection is established.\n");
  get_msg();
  printf("Client, net_init, read the message from server: [%s]\n",sbuf );
  if (!sock){
    printf("Could not connect to server. Call %s for help.\n",HELP_PHONE);
    sleep(10);
    exit(0);
  }
  return ssock;
}

/*
 * get_msg reads the message from the server into already declared buffer
 */
get_msg()
{
  int     rc,len = 0;
  
  printf("Client, get_msg: starting to read the message from the server\n");
  printf("Client, sock = %d\n", sock);
  do {
    rc = read(sock, &sbuf[len], SBUFLEN - len);
    if (rc < 0 )
      perror("Client, get_msg, in read");
    len = rc + len;
    printf("Client: red = %d\n",rc);
  
  } while (len <= SBUFLEN && !terminator(&sbuf[len - EOM_LEN]));
  len = len - EOM_LEN;
  if (sbuf[len - 1] == LF)
    len--;
  if (sbuf[len - 1] == CR)
    len--;
  sbuf[len] = '\0';
  printf("Client: Total length=%d ",len);

  fflush(stdout);

  /* Now we have to save buflen and the address of the buffer in the 
   * descriptor buffer_t, so we'll be able to use parsing routines
   */
  init_buf(&serv_client_buf, len, &sbuf[0]);
  return;
}

/* parse_header will parse the header of the message, look at the return_code
 *
 */
static void
parse_header ()
{
   message_header.buflen      = atoi ( next_field (&serv_client_buf) );
   message_header.return_code = atoi ( next_field (&serv_client_buf) );
   message_header.record_type = *( next_field (&serv_client_buf) );
   message_header.num_records = atoi ( next_field (&serv_client_buf) );

   printf("Client: buflen      = %d\n", message_header.buflen);
   printf("Client: return_code = %d\n", message_header.return_code);
   printf("Client: record_type = %c\n", message_header.record_type);
   printf("Client: num_records = %d\n", message_header.num_records);
   
   fflush(stdout);
   /* May we have to check:
    * if (serv_client_buf.buflen != message_header.buflen)
    * The actual length of the received buffer(serv_client_buf.buflen)
    *  is not equal the length which the client sent 
    * (message_header.buflen)
    *
    *  printf("Buflen is wrong\n");
    */
   if (message_header.return_code != OK)
     {
       printf("Return code =%d\n", message_header.return_code);
       /* call decrypt_routine which should know what requests should be
	* decrypted
	*/
     }
 }
main()
{

char  buf[300];

net_init();

for (;;) {

   gets(buf);
   if (buf[0] == '.')
     {
       printf("Client: End of connection (.) encountered\n");
       break;
     }
   /* encrypt before writing to server */
   write(sock,buf,strlen(buf));
   /*   write(sock,"\n",1);     */
   get_msg();
   printf("Client: <%s>\n",sbuf);
   fflush(stdout);
   /* now serv_client_buf is initialized, and buffer read, so we can parse
    * the header
    */

   parse_header();

   printf("Client: Buffer(next)= %s\n", next_field( &serv_client_buf) );

 }
}
