/**
* This is a ipcfind written for the IPC.
*
*
* @author Christian Decker (cdecker@teco.edu)
* Licence: GPL
*
* http://www.teco.edu/~cdecker/webchip/
* TecO - University of Karlsruhe, Germany
*/

#include "netapi.h"
#include <stdlib.h>
#include <stdio.h>

/* port the FINDSTR is sent to via broadcast */
#define BCPORT 8001 
/* the address the FINDSTR is sent to */
#define BCADDRESS "255.255.255.255"
/* a IPC responses with its IP number and name etc. if it gets this string*/
#define FINDSTR "0 1 "

/** 
* real timeout = TIMEOUT * TIMESTEP 
* TIMESTEP is in mseconds
*/
#define TIMESTEP 500
#define TIMEOUT 10

#define FALSE 0
#define TRUE 1

void failedFunc(char* funcName, errorInfoPtr eip)
{
    printf("%s failed:\n", funcName);
    printf("ErrorCode: %d\n", eip->errorCode);
    printf("Description: %s\n", eip->errorDescr);
    printf("Abort\n");
    exit(1);
}

int main(int argc, char* argv[])
{
    int sd; /* socket descriptor */
    int rec, i; 
    char* IPAddress;
    unsigned int port; /* port where FINDSTR is sent to */
    byte dataBuffer[255]; /* received data are saved here */
    int time; /* actual time count */
	int id[255]; /* id which were found (used for duplicate finding) */
	int id_counter = 0; /* counter for id array */
	int ipc_id; /* actual found IPC id */
	int dup = FALSE; /* TRUE, if a duplicate was found */
    
    IPAddress = BCADDRESS;
    port = (unsigned int) 8001;

    sd = openSocket(UDP); /* open a socket */

    if ( !bind(sd, 1234) ) { /* bind socket to port */
        failedFunc("bind", getErrorInfo());
    }


    /* sending data */
    i = UDPSend(sd, IPAddress, port, FINDSTR, strlen(FINDSTR), MSG_BLOCKING, 1);

    
    /* receive data from separate IPCs */
	time = 0;
	while (time < TIMEOUT) {
        rec = 0;
	    rec = UDPReceive(sd, dataBuffer, sizeof(dataBuffer), MSG_DONTWAIT, 1);
           if (getErrorCode() != 0) { /* we got nothing or an error */
                if (getErrorCode() != -1) { /* there's really an error */
                    failedFunc("UDPReceive", getErrorInfo());
                }
           } else { /* we've received something */

			    /* for duplicate finding */
    			dup = FALSE;
	    		sscanf(dataBuffer, "%d", &ipc_id);
		    	for (i = 0; i < id_counter; i++) {
			    	if (id[i] == ipc_id) {
				    	dup = TRUE;
    				}
	    		}
			   		
		    	if (!dup) { /* it's not a duplicate - print it! */
			    	id[id_counter++] = ipc_id;
				    for (i = 0; i < rec; i++) {
					    printf("%c", dataBuffer[i]);
    				} 
	    		}

		    	if (rec > 0 && (!dup)) { /* there was an output string and no duplicate before */
			    	printf("\n");
    			}
		    }
        time++; /* next time cycle */
		sleep(TIMESTEP); /* sleep in milli seconds */
	}

    /* closing the socket */
    if (!closeSocket(sd))
    {
        failedFunc("closeSocket", getErrorInfo());
    }
    return 0;
}
