#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#define BUF_LEN 200

int get_client_socket();

int main(void) {
        fd_set read_fd_set;            
        struct timeval timeval;
        int retval;
        int client_socket;

        printf("Waiting for connection\n");
        client_socket = get_client_socket();
        printf("Connected\n");


        while (1) {
                /* Watch stdin (fd 0) and socket to see when it has input. */
                FD_ZERO(&read_fd_set); /* Empty set */
                FD_SET(fileno(stdin), &read_fd_set); /* Add the FD of stdin to the set. */
                FD_SET(client_socket, &read_fd_set); /* Add the socket to the set */

                /* Wait up to five seconds. */
                timeval.tv_sec = 5;
                timeval.tv_usec = 0;
                printf("Calling select...\n");
                retval = select(FD_SETSIZE, &read_fd_set, NULL, NULL, &timeval);

                if (retval == -1) {
                        perror("select()");
                        exit(1);
                }
                
                if (retval == 0) {
                        printf("Timeout - no data within five seconds.\n");
                } else if (retval >= 0) {
                        size_t len;
                        char buffer[BUF_LEN];
                        
                        if (FD_ISSET(fileno(stdin), &read_fd_set)) {
                                /* Data available on stdin */
                                len = read(fileno(stdin), buffer, BUF_LEN-1);
                                if (len >= 0) {
                                        buffer[len] = '\0'; /* mark end of string */
                                        printf("Data on stdin: %s", buffer);
                                }
                        }
                        if (FD_ISSET(client_socket, &read_fd_set)) {
                                /* Data available on the socket */
                                len = recv(client_socket, buffer, BUF_LEN-1, 0);
                                if (len >= 0) {
                                        buffer[len] = '\0'; /* mark end of string */
                                        printf("Data on socket: %s", buffer);
                                }
                        }
                }
        }
        return 0;
}

#define MYPORT 5555    // the port users will be connecting to

#define BACKLOG 10     // how many pending connections queue will hold

int get_client_socket()
{
        int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
        struct sockaddr_in my_addr;    // my address information
        struct sockaddr_in their_addr; // connector's address information
        int sin_size;
        int yes = 1;

        sockfd = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!

        // tento radek zpusobi, ze pri opakovanem restartu serveru, bude volani
        // funkce bind() uspesne, kdo neveri, at ho zakomentuje :))
        if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
                perror("setsockopt");
                exit(1);
        }

        my_addr.sin_family = AF_INET;         // host byte order
        my_addr.sin_port = htons(MYPORT);     // short, network byte order
        my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
        memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

        // don't forget your error checking for these calls:
        bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));

        listen(sockfd, BACKLOG);
                
        sin_size = sizeof(struct sockaddr_in);
        new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
        return new_fd;
}