/****************************************************************************/
/* Copyright (c) 2014 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Simple HTML interface to instrument web servers.              */
/* Filename : HtmlUtil.h                                                    */
/* Author   : Henthorn                                                      */
/* Project  : i2MAP                                                         */
/* Version  : 1.0                                                           */
/* Created  : 11/07/2014                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _HTMLUTIL_H
#define _HTMLUTIL_H

#include <sys/select.h>
#include <unistd.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

// Simple HTML interface for use with the Ki Pro Quad recorder from AJA.
//
class HtmlUtil
{
public:
   // Create an interface to the server. The connection is only opened
   // when posting and getting.
   //
   HtmlUtil(const char* server_ip);
   ~HtmlUtil();

   // POST the params to the url. Equiv. to:
   // http://server.com/values?eParamID_pid&thisParam=that&thatParam=this
   // Returns 0 when all is well and the response contains "200 OK".
   // Call get_response() to get a copy of the server response or error message.
   // The connection is closed after the response is read.
   //
   int post(const char* url, const char* params);

   // Query the server using GET. Equiv. to:
   // http://server.com/values?eParamID_pid
   // Returns 0 when all is well and the response contains "200 OK".
   // Call get_response() to get a copy of the server response or error message.
   // The connection is closed after the response is read.
   //
   int  get(const char* url);

   // Get a copy of the latest server response or error message.
   //
   int get_response(const char* your_response_buffer,
                    unsigned int your_response_buffer_length);

private:
   int create_tcp_socket();
   int read_response(int socket);

   /* data */
   char* _server_ip;
   char  _response[1000];
   int   _sockfd;
   struct sockaddr_in _server_addr;

};

#endif
