/*
 * An example of client implementation
 * by Hoang Nguyen 
 * Feel free to modify and copy the code
 * and use it AT YOUR OWN RISK
*/

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

#include <arpa/inet.h>

#include "msg.h"

#define VERBAL 1
#define IVERBAL if (VERBAL) 

#define CPORT "4000" // assuming the control port is 4000
#define DPORT "4001" // data port is 4001


#define MAX_SERVER 5
#define MAXDATASIZE 100

int no_server;
int serverfd[MAX_SERVER];

char buf[MAXDATASIZE];

void usage() {
	printf("client dispatcher put filename\n");
}


void sendCommand(char* dispatcher, char* command, char* filename) {
	msg_t msg, reply;
	int sockfd = connectToServer(dispatcher, CPORT);	

	if (sockfd > 0)  {
		msg.len = 2;
		msg.data[0] = 1;
		printf("\tSend request to dispatcher %s:%s\n", dispatcher, CPORT);
		sprintf(msg.data, "%s %s", command, filename);
		msg.len = strlen(msg.data);

		sendRequest(sockfd, &msg, &reply); 
		reply.data[reply.len] = 0;

		printf("\tRecv reply: %s\n", reply.data);
		close(sockfd);
	}
}


int main(int argc, char* argv[]) {
	int i;	
	// add server
	if (argc < 4) {
		usage();
		return 0;
	}

	sendCommand(argv[1], argv[2], argv[3]);

	return 0;
}