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



char * next_field (buffer_t *b);

#define SBUFLEN 2000
static char     sbuf[2000];
static char     header[40];
#define HEADER_BUF_LEN 40
buffer_t header_buf;
buffer_t data_buf;


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.Sock=%d\n",sock);
  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;
  
  init_buf (&header_buf, MSG_HEADER_LEN, header);

  log_aline("Db_fetch,get_msg: before get  message from server\n");

  log_aline("Db_fetch,Get_msg: before call.get_server_buf\n");  
  /* We'll read first line of server's response, i.e. header */
  get_server_buf(&header_buf,sock);  


  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_t *m)
{
printf ("in parse_header, m = %s\n", m);

   m->buflen      = atoi ( next_field (&header_buf) );
   m->return_code = atoi ( next_field (&header_buf) );
   m->record_type = *( next_field (&header_buf) );
   m->num_records = atoi ( next_field (&header_buf) );

   printf("Db_fetch: buflen      = %d\n", m->buflen);
   printf("Db_fetch: return_code = %d\n", m->return_code);
   printf("Db_fetch: record_type = %c\n", m->record_type);
   printf("Db_fetch: num_records = %d\n", m->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 (m->return_code != OK)
     {
       printf("Return code =%d\n", message_header.return_code);
        * call decrypt_routine which should know what requests should be
	* decrypted
	*
     }
     */
 }
db_fetch(char *request_ptr,message_header_t *m,char **data_ptr)
{

char  buf[300];
char  request[40];


net_init();

   /* encrypt before writing to server */
   log_aline("db_fetch, before write request to server\n");
   write(sock,request_ptr,strlen(request_ptr));
   /*   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(m);

   init_buf (&data_buf, m->buflen, NULL);

   /* We'll read the rest of the data from server */
   log_aline("We will read the data buffer from server\n");
   get_server_buf(&data_buf,sock);  


   *data_ptr = data_buf.bufptr;

/***
   printf("Db_fetch:data = %s",*data_ptr);
***/
   print_data_buf (*data_ptr);
}
int print_data_buf (ptr)
char *ptr;
{
   char *p; 

   printf ("Db_fetch:data follows:\n[");
   p = ptr;
   while (1)
	{
	if (*p == 0 && *(p+1) == 0)
	    break;
	if (*p == 0)
	    printf ("]\n[");
	else
	    printf ("%c", *p);
	*p++;
	}
   printf ("]\n");
 } 
