/* LICENSE:
  =========================================================================
    CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
    Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
    School of Computer Science, Carnegie Mellon University
    All rights reserved.
  ========================================================================= */

/*
* This program reads bytes from dog robot and
* sends those bytes along over to choke chain
* so that it can interpret them and make sense
* of them for debugging purposes.
*
* Written by Juan Fasola
*/

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

#define CHOKEPORT	2048
#define DOGPORT		54321
#define BUFSIZE		512
#define CHOKEHOST	"localhost"

int connect2(char* remote,
	     int port,
	     int &s,
	     sockaddr_in &addr,
	     hostent* hp);

/**
* Main point of execution, accepts the
* dog's hostname as commandline argument.
*/
int main(int argc, char **argv)
{
    //Function Initializations
    int    len;
    char   buf[BUFSIZE];
    int    ret;
    int foo;

    //Choke Initializations
    struct sockaddr_in  choke_addr;
    struct hostent*		choke_hp;
    int    choke_s;

    //Dog Initializations
    struct sockaddr_in  dog_addr;
    struct hostent*		dog_hp;
    int    dog_s;

    //Error Checking
    if (argc != 2){
        printf("Usage: wlan_connect <aibo hostname>\n");
        exit(1);
    }

    //Connect to choke chain
    connect2(CHOKEHOST,
	     CHOKEPORT,
	     choke_s,
	     choke_addr,
	     choke_hp);

    printf("Connected to choke chain\n");

    //Connect to dog
    connect2(argv[1],
	     DOGPORT,
	     dog_s,
	     dog_addr,
	     dog_hp);

    printf("Connected to dog\n");

    //Do that funky echo stuff you do
    while(true)
    {
        int size;

	//Read from dog
        size = read(dog_s, buf, BUFSIZE);
	if (size <= 0) {
            perror("read");
            close(dog_s);
            exit(1);
        }

	//Send to choke chain
        ret = write(choke_s, buf, size);
        if (ret != size) {
            perror("write");
            close(choke_s);
            exit(1);
        }
    }

    //Close sockets, won't ever get here prob.
    close(dog_s);
    close(choke_s);

    return 0;
}

/**
* Connects to the remote computer given
* its hostname, and sets the socket.
*/
int connect2(char* remote,
	     int port,
	     int &s,
	     sockaddr_in &addr,
	     hostent* hp)
{
    //Connect the socket
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }

    //Get the host by its name
    if ((hp = gethostbyname(remote)) == NULL) {
        perror("gethostbyname");
        exit(1);
    }

    //Do some other funky stuff
    memset((void*)&addr, 0, sizeof(addr));
    memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);

    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);

    //Connect to remote computer
    if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0){
        perror("connect");
        exit(1);
    }

    return 0;
}
