/**
 * \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>
#include "printf-stdarg.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) {
int i;
	PSOCK_BEGIN(&s->sin);

		for(;;) {
			PSOCK_READTO(&s->sin, '\n');
			for(i=0;i<sizeof(s->filename);i++){
				if(s->inputbuf[i]=='\n') goto sem;
			}
sem:
			xprintf("\n\r i: %d\n\r",i);
			s->inputbuf[i+1]='\0';
			strncpy(s->filename, s->inputbuf, i+1);

			(void) xQueueSend(komInSocketQueue,s->filename,portMAX_DELAY);
		};

	PSOCK_END(&s->sin);
	}

int handle_connection2(uip_tcp_appstate_t *s) {

	PSOCK_BEGIN(&s->sout);
		PSOCK_SEND_STR(&s->sout, "\nDruhy thread\n");
		for(;;) {
			PSOCK_WAIT_THREAD(&s->sout,uxQueueMessagesWaiting(komOutSocketQueue));

			while((kom_podmienka=uxQueueMessagesWaiting(komOutSocketQueue))) {
				for(;kom_podmienka>0;kom_podmienka--) {
					(void) xQueueReceive(komOutSocketQueue,komOutSocketbuffer,1);

					strcpy(komOutSocketbuffer+strlen(komOutSocketbuffer),"ZYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");

					PSOCK_SEND_STR(&s->sout,komOutSocketbuffer);

				}
			}

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