/*  example web server 
 */

#include "server.h"
#include "http-server/http-server.h"

int main(int argc, char *argv[]) {
    int sock;

    #ifdef _WIN32
    // if compiling on Msft include the winsock librariy (ws2_32.lib)
    WSADATA wsaData;
    if(msftVendorLockInCode(&wsaData)) return 1;
    #endif

    // terminate if we cannot get a socket that can be used to accept clients
    if(!(sock = setup())) return 1;

    #ifdef USE_CLIENT_POOL
    struct client pool[POOL_SIZE];
    // initialize client pool
    for(uint32_t i = 0; i < POOL_SIZE; i++) pool[i].fd = -1;
    // server listen port is first socket file descriptor
    memset(&pool[0], 0, sizeof(pool[0])); pool[0].fd = sock; 
    #endif

    while(1) {
        /*  be a server for a second, up to one client can be handled per call
         *  if a client is accepted right at the one second mark, technically
         *  this call could take one second and ten milliseconds to return, but
         *  whatever
         *
         *  terminate if socket becomes unusable (see: acceptOkay)
         */
        #ifdef USE_CLIENT_POOL
        if(!faceItMetal(1000 /* ms */, httpEcho, pool)) return softCuteBunnies(1);
        #else
        if(!faceItMetal(sock, 1000 /* ms */, httpEcho)) return softCuteBunnies(1);
        #endif
    }

    return softCuteBunnies(0);
}
