/**
 * \addtogroup helloworld
 * @{
 */

/**
 * \file
 *         An example of how to write uIP applications
 *         with protosockets.
 * \author
 *         Adam Dunkels <adam@sics.se>
 */

/*
 * This is a short example of how to write uIP applications using
 * protosockets.
 */

/*
 * We define the application state (struct hello_world_state) in the
 * kom_modul.h file, so we need to include it here. We also include
 * uip.h (since this cannot be included in kom_modul.h) and
 * <string.h>, since we use the memcpy() function in the code.
 */
#include "kom_modul.h"
#include "uip.h"
#include <string.h>

/*
 * Declaration of the protosocket function that handles the connection
 * (defined at the end of the code).
 */
static int handle_connection(uip_tcp_appstate_t *s);
/*---------------------------------------------------------------------------*/
/*
 * The initialization function. We must explicitly call this function
 * from the system initialization code, some time after uip_init() is
 * called.
 */
void
hello_world_init(void)
{
  /* We start to listen for connections on TCP port 1000. */
  uip_listen(HTONS(1000));
}
/*---------------------------------------------------------------------------*/
/*
 * In kom_modul.h we have defined the UIP_APPCALL macro to
 * hello_world_appcall so that this funcion is uIP's application
 * function. This function is called whenever an uIP event occurs
 * (e.g. when a new connection is established, new data arrives, sent
 * data is acknowledged, data needs to be retransmitted, etc.).
 */
void
hello_world_appcall(void)
{
  /*
   * The uip_conn structure has a field called "appstate" that holds
   * the application state of the connection. We make a pointer to
   * this to access it easier.
   */
 struct httpd_state *s = (struct httpd_state *) &(uip_conn->appstate);
 pripojenie = s;
  /*
   * If a new connection was just established, we should initialize
   * the protosocket in our applications' state structure.
   */
  if(uip_connected()) {
   PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf)-1);

   PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf)-1);

  }

  /*
   * Finally, we run the protosocket function that actually handles
   * the communication. We pass it a pointer to the application state
   * of the current connection.
   */
  handle_connection(s);
  handle_connection2(s);
}
/*---------------------------------------------------------------------------*/
/*
 * This is the protosocket function that handles the communication. A
 * protosocket function must always return an int, but must never
 * explicitly return - all return statements are hidden in the PSOCK
 * macros.
 */
static int
handle_connection(uip_tcp_appstate_t *s)
{


  PSOCK_BEGIN(&s->sin);

 // PSOCK_SEND_STR(&s->sin, "Hello. What is your name?\n");

  for(;;){
  PSOCK_READTO(&s->sin, '\n');
  strncpy(s->filename, s->inputbuf, sizeof(s->filename));
  PSOCK_SEND_STR(&s->sin, "Napisanie: ");
  PSOCK_SEND_STR(&s->sin, s->filename);
  };

  kom_podmienka=0;
  PSOCK_WAIT_UNTIL(&s->sin,kom_podmienka);
  kom_podmienka=0;
  PSOCK_SEND_STR(&s->sin, "Koniec");
  PSOCK_CLOSE();
  
  PSOCK_END(&s->sin);
}

int
handle_connection2(uip_tcp_appstate_t *s)
{

	PSOCK_BEGIN(&s->sout);
  char buffer[sizeQueue];
  PSOCK_SEND_STR(&s->sout, "\nDruhy thread\n");

/*  for(;;){
  kom_podmienka=0;
  PSOCK_WAIT_UNTIL(&s->sout,kom_podmienka);
  PSOCK_SEND_STR(&s->sout, "\nKonzola");
  };*/

  for(;;){
	  PSOCK_WAIT_THREAD(&s->sout,uxQueueMessagesWaiting(komOutSocketQueue));
	  (void) xQueueReceive(komOutSocketQueue,buffer,1);
	  PSOCK_SEND_STR(&s->sout,buffer);
  }

  PSOCK_CLOSE();

  PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
