//---------------------------------------------------------------------------
//
// MODULE NAME:     main.cpp
//
// COPYRIGHT:       Copyright (c) 2013, GladeSoft, Inc.
//                  All Rights Reserved.
//
//                  NOTICE: THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
//                  GLADESOFT, INC., AND SHALL NOT BE REPRODUCED, COPIED
//                  IN WHOLE OR IN PART ADAPTED, MODIFIED, OR
//                  DISSEMINATED WITHOUT THE EXPRESS WRITTEN CONSENT
//                  OF GLADESOFT, INC. ANY REDISTRIBUTION OR
//                  PUBLICATION TO UNAUTHORIZED PERSONS IS STRICTLY
//                  PROHIBITED BY U.S. AND INTERNATIONAL COPYRIGHT LAW.
//
// DESCRIPTION:     Example of using the discovery service.
//
// AUTHOR:          M. Grosberg
//
// CREATION DATE:   Oct 21, 2007
//
//---------------------------------------------------------------------------
//
// HISTORY OF CHANGE  
//
// -----+--------+------------------------------------------+------+---------
// VERS | DATE   | DESCRIPTION OF CHANGE                    | INIT | PR#
// =====+========+==========================================+======+=========
//  001 |10/21/07| Initial creation.                        | MYG  | 
// -----+--------+------------------------------------------+------+---------
// -----+--------+------------------------------------------+------+---------
//---------------------------------------------------------------------------

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "seminole.h"
#include "sem_discovery.h"

static const char *const dev_class[] =
{
  "demos",
  NULL
};

static const HttpdPair params[] = 
{
  { "descr",   "A computer running the Seminole discovery demo." },
  { "os",      HTTPD_OS_NAME                                     }
};

static const HttpdDiscoveryServer::Config disc_config =
{
  &HttpdDiscoveryServer::mDefaultNetwork,
  dev_class,
  HTTPD_NUMELEM(params),
  params
};

/*************************************************************************/

class DemoDiscoveryServer : public HttpdDiscoveryServer
{
protected:
  virtual int BuildResponse(HttpdCgiWriter *p_writer);
  virtual int PrepareResponse();

public:
  DemoDiscoveryServer(Httpd *p_server, const Config *p_config)
    : HttpdDiscoveryServer(p_server, p_config) {}
};

//---------------------------------------------------------------------------
//
// NAME:           DemoDiscoveryServer::BuildResponse
//
// ACCESS:         protected
//
// DESCRIPTION:    Build discovery response beacons.
//
// RETURNS:        OS Status code.
//
// AUTHOR:         M. Grosberg
// CREATED:        Oct 21, 2007
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
int DemoDiscoveryServer::BuildResponse(HttpdCgiWriter *p_writer)
{
  int         rc;
  time_t      now;
  struct tm  *p_lt;
  char        buf[72];

  time(&now);
  p_lt = localtime(&now);
  strftime(buf, sizeof(buf), "%c", p_lt);

  rc = p_writer->Write("now", buf);
  if (rc != 0)
    return (rc);

  // Don't forget to call the superclass implementation
  // so that the remaining parameters can be written.
  return (HttpdDiscoveryServer::BuildResponse(p_writer));
}

//---------------------------------------------------------------------------
//
// NAME:           DemoDiscoveryServer::PrepareResponse
//
// ACCESS:         protected
//
// DESCRIPTION:    Prepare for a discovery response.
//
// RETURNS:        OS Status code.
//
// AUTHOR:         M. Grosberg
// CREATED:        Oct 21, 2007
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
int DemoDiscoveryServer::PrepareResponse()
{
  mRebuildResponse = true; // Since we contain a timestamp always rebuild.
  return (HttpdDiscoveryServer::PrepareResponse());
}

/*************************************************************************/

class DemoHandler : public HttpdHandler
{
public:
  DemoHandler() { mpPrefix = "/"; }
  virtual bool Handle(HttpdRequest *p_req);
};

//---------------------------------------------------------------------------
//
// NAME:           DemoHandler::Handle
//
// ACCESS:         protected
//
// DESCRIPTION:    Handle an HTTP request.
//
// RETURNS:        True if the request was handled by this handler or
//                 false if the request remains unhandled.
//
// AUTHOR:         M. Grosberg
// CREATED:        Oct 21, 2007
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
bool DemoHandler::Handle(HttpdRequest *p_req)
{
  if (!IsMe(p_req))
    return (false);

  HttpdDynamicOutput output(p_req, p_req->IsHeadRequest());
  p_req->Respond(HTTPD_RESP_OK);
  output.Header("Content-Type", "text/html");
  output.HeaderComplete();
    
  output.Body()->Printf
  (
    "<html><head><title>Boo!</title></head>\n"
    "<body><h1>Hey There<h1><p>You found me!\n"
    "</body></html>\n"
  );

  return (true);
}

/*************************************************************************/

//---------------------------------------------------------------------------
//
// NAME:           main
//
// DESCRIPTION:    Main entry point for the example.
//
// RETURNS:        Process exit status.
//
// AUTHOR:         M. Grosberg
// CREATED:        Oct 21, 2007
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
int main(int argc,char *argv[])
{
  if (Httpd::Init() != 0)
  {
    printf("Can't initialize platform\n");
    return (EXIT_FAILURE);
  }

  Httpd *p_webserv = new Httpd("localhost");

  DemoHandler          dh;
  DemoDiscoveryServer  ds(p_webserv, &disc_config);

  p_webserv->Install(&dh);
  p_webserv->Start();

  int rc = ds.Create();
  if (rc != 0)
  {
    printf("Can't create HDS: %d\n", rc);
    return (EXIT_FAILURE);
  }

  rc = ds.Start();
  if (rc != 0)
  {
    printf("Can't start HDS: %d\n", rc);
    return (EXIT_FAILURE);
  }

  char buf[4];
  printf("Press <ENTER> to stop.\n");
  fgets(buf,sizeof(buf),stdin);
  
  ds.Stop();
  p_webserv->Stop();

  return (EXIT_SUCCESS);
}



