

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <winsock.h>

#include "client.h"

#define TCPPORT 44444

//extern BGPViewer B1;
//extern int numDistinctDates;
//extern int DistinctDates[2048];


char buf[5000000]; // network buffer 

int MyClient::socket_main () {
		
	int sockfd; // sockfd
	int numbytes = 0 ; // byte counter 
	char onemsg[1024];
	struct sockaddr_in REMOTE; // store remote info
	struct hostent *he; // store command line input 
	int i, msgstart;
	int starting;
	FILE *ofptr;
	int lastchar;
	int plusfivetime;
	int plusfivedate;
	int year, month, date, cutoffdate;

	WORD sockVersion;
	WSADATA wsaData;

	sockVersion = MAKEWORD(1, 1); // We'd like Winsock version 1.1
	WSAStartup(sockVersion, &wsaData);

	if ((   he=gethostbyname(hostname_buf)   ) == NULL) {
			perror("gethostbyname");
			return 1;
	}

	// get a sockfd 
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd == -1 ) {
	  	perror("Cannot open socket ");
	  	return 1;
	} else {
		printf("Got a socket \n");
	}
	
	// setup remote
	REMOTE.sin_family = AF_INET;
	REMOTE.sin_addr = *((struct in_addr *)he  -> h_addr ); // this is server ip 
		//xxx.sin_addr.s_addr = htonl(argv[1]);
		//xxx.sin_addr.s_addr = htonl(INADDR_ANY);
		//	REMOTE.sin_addr.s_addr = inet_addr("169.237.5.226");
	REMOTE.sin_port = htons(TCPPORT); // this is server port 
		//xxx.sin_port = 0; // choose an unused port at random
	memset(&(REMOTE.sin_zero), '\0', 8); // zero the rest of the struct
	printf("Prepare to connect to %s \n", hostname_buf );



	//connect to remote via ip and port 
	if ( connect(sockfd, (struct sockaddr *)&REMOTE, sizeof(REMOTE) ) == -1) {
		perror("connect");
		return 1;
	} else {
		printf("Connected to %s \n", hostname_buf );
	}


	if (minutebyminute) {

		plusfivetime=starttime;
		plusfivedate=startdate;
		if ((plusfivetime%100) >= 59) { // if need to increment hour
			plusfivetime = plusfivetime - (plusfivetime%100);
			if (plusfivetime >= 2300) { // if need to increment date
				plusfivetime = 0;
				month = (plusfivedate/100)%100;
				year = plusfivedate/10000;
				if ((month==9)||(month==4)||(month==6)||(month==11)) {
					cutoffdate = 30;
				} else if (month==2) {
					if (year%4) {
						cutoffdate = 29;
					} else {
						cutoffdate = 28;
					}
				} else {
					cutoffdate = 31;
				}
				date = plusfivedate%100;
				if (date >= cutoffdate) { // if need to increment month 
					plusfivedate -= date;
					plusfivedate += 1;
					if (month>=12) { // if need to increment year
						plusfivedate = (year+1) * 10000 + 101;
					} else {
						plusfivedate += 100;
					}
				} else { // just increment date
					plusfivedate += 1;
				}
			} else { // just increment hour
				plusfivetime += 100;
			}
		} else { // just increment minutes
			plusfivetime += 1;
		}
		printf("%d %d %d %d\n", startdate,starttime,plusfivedate,plusfivetime);
		sprintf(buf,"%08d%04d%08d%04d",startdate,starttime,plusfivedate,plusfivetime);
		startdate = plusfivedate;
		starttime = plusfivetime;
	} else {
		sprintf(buf,"%08d%04d%08d%04d",startdate,starttime,enddate,endtime);
	}
	
	printf("buffer %s end\n", buf);
	
	numbytes = send( sockfd, buf, strlen(buf), 0) ;
	if ( numbytes == -1) {
		perror("send");
		return 1;
	}
	

	starting = 0;


	while (1) {
		
		numbytes=recv( sockfd , &(buf[starting]) , 1499 , 0) ;
		//numbytes=recv( sockfd , buf , 1499 , 0) ;

		if ( numbytes == 0 ) {
			printf("Remote close the connection.\n") ; 
			closesocket (sockfd) ;
			printf("I close the connection too.\n") ; 
			break; // out of while loop

		} else if ( numbytes == -1 ) {
			printf("Error in transmission\n") ; 
			perror("recv");
			return 1;
		} else {
			starting += numbytes;
			printf("I got something of size %d bytes.\n", numbytes ) ;	
			printf("starting %d\n", starting);
		}
	} // while

		
	buf[starting] = '\0'; // make it as a string 
	
	printf("Call soontee's function with \"%s\" \n ", buf ) ;
	// add BGP update message to memory
	msgstart = 0;
	for (i=0;i<starting;i++) {
		if (buf[i]=='\n') {
			buf[i] = '\0';
			//printf("total size %d bytes.\n", starting ) ;
			//printf("this message %d %s\n", msgstart, &(buf[msgstart]));
			strcpy(onemsg,&(buf[msgstart]));
			// get the timestamp part
			onemsg[12] = '\0';
			sscanf(&(onemsg[8]),"%d",&(lastReceivedTime));
			onemsg[8] = '\0';
			sscanf(onemsg,"%d",&(lastReceivedDate));
			// write the OASC event part to file
			ofptr = fopen("../realtime_data/tempfile.txt","wt");
			lastchar = strlen(&(onemsg[13]));
			onemsg[13+lastchar] = '\n'; 
			onemsg[13+lastchar+1] = '\0'; 
			fputs(&(onemsg[13]),ofptr);
			fclose(ofptr);
			// addentry
			//B1.AddEntry(lastReceivedDate);
			msgstart = i+1;
		}
	}
        
	printf("End of socket \n") ;

	if ((minutebyminute==0) && (starting > 0)) {

		startdate = lastReceivedDate;
		starttime = lastReceivedTime;
	
		plusfivetime=starttime;
		plusfivedate=startdate;
		if ((plusfivetime%100) >= 59) { // if need to increment hour
			plusfivetime = plusfivetime - (plusfivetime%100);
			if (plusfivetime >= 2300) { // if need to increment date
				plusfivetime = 0;
				month = (plusfivedate/100)%100;
				year = plusfivedate/10000;
				if ((month==9)||(month==4)||(month==6)||(month==11)) {
					cutoffdate = 30;
				} else if (month==2) {
					if (year%4) {
						cutoffdate = 29;
					} else {
						cutoffdate = 28;
					}
				} else {
					cutoffdate = 31;
				}
				date = plusfivedate%100;
				if (date >= cutoffdate) { // if need to increment month 
					plusfivedate -= date;
					plusfivedate += 1;
					if (month>=12) { // if need to increment year
						plusfivedate = (year+1) * 10000 + 101;
					} else {
						plusfivedate += 100;
					}
				} else { // just increment date
					plusfivedate += 1;
				}
			} else { // just increment hour
				plusfivetime += 100;
			}
		} else { // just increment minutes
			plusfivetime += 1;
		}
		printf("%d %d %d %d\n", startdate,starttime,plusfivedate,plusfivetime);
		sprintf(buf,"%08d%04d%08d%04d",startdate,starttime,plusfivedate,plusfivetime);

		startdate = plusfivedate;
		starttime = plusfivetime;
	}
	return starting ;  

} // main


void MyClient::fake() {

	int msgstart;
	int i;
	char buf[1024];
	int buflen;
	char onemsg[256];
	FILE *ofptr;
	int lastchar;


	strcpy(buf,"200405150820:HSS:63.82.80.0/24 (63.80.0.0/13)	701	==>	8012\n");
	strcat(buf,"200405150825:BSS:63.208.2.0/24 (63.208.0.0/14)	3356	==>	3356\n");
	strcat(buf,"200405150826:OS:138.42.0.0/16*	701\n");
	strcat(buf,"200405150826:CSS:144.198.192.0/19	701	==>	7238\n");

	msgstart = 0;
	buflen = strlen(buf);
	for (i=0;i<buflen;i++) {
		if (buf[i]=='\n') {
			buf[i] = '\0';
			printf("total size %d bytes.\n", buflen ) ;
			printf("this message %d %s\n", msgstart, &(buf[msgstart]));
			strcpy(onemsg,&(buf[msgstart]));
			// get the timestamp part
			onemsg[12] = '\0';
			sscanf(&(onemsg[8]),"%d",&(lastReceivedTime));
			onemsg[8] = '\0';
			sscanf(onemsg,"%d",&(lastReceivedDate));
			// write the OASC event part to file
			ofptr = fopen("../realtime_data/tempfile.txt","wt");
			lastchar = strlen(&(onemsg[13]));
			onemsg[13+lastchar] = '\n'; 
			onemsg[13+lastchar+1] = '\0'; 
			fputs(&(onemsg[13]),ofptr);
			fclose(ofptr);
			// addentry
			//B1.AddEntry(lastReceivedDate);
			msgstart = i+1;
		}
	}




}




