changeset 53:6907e2177eb8 ultra-staging

Fixed bugs in Udp classes, updated tests
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 07 Jun 2015 14:58:34 +0100
parents f5b5c648cd5d
children d3f869b98147
files core/UdpClient.cpp core/UdpServer.cpp include/UdpClient.h resources/tests/UdpClientUdpServerTest.cpp
diffstat 4 files changed, 56 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/core/UdpClient.cpp	Wed May 20 18:07:16 2015 +0100
+++ b/core/UdpClient.cpp	Sun Jun 07 14:58:34 2015 +0100
@@ -7,6 +7,9 @@
 #include "../include/UdpClient.h"
 
 	UdpClient::UdpClient(){
+		outSocket=socket(AF_INET, SOCK_DGRAM, 0);
+		isSetPort=false;
+		isSetServer=false;
 		enabled=false;
 	}
 	UdpClient::UdpClient(int aPort, const char* aServerName){
@@ -17,6 +20,8 @@
 		}
 		setPort(aPort);
 		setServer(aServerName);
+		isSetPort=true;
+		isSetServer=true;
 		enabled=true;
 	}
 	UdpClient::~UdpClient(){
@@ -26,9 +31,17 @@
 		port=aPort;
 		destinationServer.sin_port = htons(port);
 		destinationServer.sin_family = AF_INET;
+		isSetPort=true;
+		if(isSetServer){
+			enabled=true;
+		}
 	};
 	void UdpClient::setServer(const char* aServerName){
 		inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
+		isSetServer=true;
+		if(isSetPort){
+			enabled=true;
+		}
 	};
 	int UdpClient::send(void * message, int size){
 		if(!enabled)
@@ -36,8 +49,9 @@
 		unsigned int length;
 		length=sizeof(struct sockaddr_in);
 		int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
-		if (n < 0)
+		if (n < 0){
 			return n;
+		}
 		return 1;
 	};
 
--- a/core/UdpServer.cpp	Wed May 20 18:07:16 2015 +0100
+++ b/core/UdpServer.cpp	Sun Jun 07 14:58:34 2015 +0100
@@ -34,11 +34,16 @@
 
 bool UdpServer::bindToPort(int aPort){
 	port=aPort;
+	if(port<1){
+		enabled=false;
+		return false;
+	}
 	server.sin_port=htons(port);
 	if (bind(inSocket,(struct sockaddr *)&server,length)<0){
 		enabled=false;
 		return false;
 	}
+	enabled=true;
 	return true;
 };
 int UdpServer::read(//Returns the number of bytes read, or -1 if there was an error.
--- a/include/UdpClient.h	Wed May 20 18:07:16 2015 +0100
+++ b/include/UdpClient.h	Sun Jun 07 14:58:34 2015 +0100
@@ -23,6 +23,8 @@
 		int port;
 		int enabled;
 		int outSocket;
+		bool isSetPort;
+		bool isSetServer;
 		struct sockaddr_in destinationServer;
 	public:
 		UdpClient();
--- a/resources/tests/UdpClientUdpServerTest.cpp	Wed May 20 18:07:16 2015 +0100
+++ b/resources/tests/UdpClientUdpServerTest.cpp	Sun Jun 07 14:58:34 2015 +0100
@@ -6,10 +6,18 @@
 	int buffer;
 	int tot=100;
 	int errors=0;
-	for(int n=0; n<tot; n++)
-		client->send(&n,sizeof(int));
 	for(int n=0; n<tot; n++){
-		server->read(&buffer,sizeof(int));
+		if(client->send(&n,sizeof(int))!=1){
+			printf("error: while sending\n");
+			errors++;
+		}
+	}
+	for(int n=0; n<tot; n++){
+		if(server->read(&buffer,sizeof(int))<0){
+			printf("error: unable to read\n");
+			errors++;
+			continue;
+		};
 		if(n!=buffer){
 			printf("error: %d!=%d\n",n,buffer);
 			errors++;
@@ -17,7 +25,7 @@
 	}
 	int n=server->emptySocket();
 	if(n!=0)
-		printf("Error: the socket had %d bytes",n);
+		printf("Error: the socket had %d bytes\n",n);
 	return errors;
 }
 int compareStrings(char * str1, char * str2){
@@ -71,11 +79,12 @@
 
 
 int main(){
-    int port=1234;
+	int port=1234;
+	char serverName[]="127.0.0.1";
 	UdpServer server(port);
-	UdpClient client(port,"127.0.0.1");
+	UdpClient client(port,serverName);
 	int errors=0;
-	int ret;
+	int ret=0;
 	ret=test1(&server,&client);
 	errors+=ret;
 	if(ret)
@@ -96,6 +105,24 @@
 		printf("test3 failed with %d errors\n", ret);
 	else
 		printf("test3 passed\n");
+//now test if the setPort and setServer methods work
+	client.~UdpClient();
+	server.~UdpServer();
+	port=1235;
+	UdpServer server1;
+	UdpClient client1;
+	client1.setPort(port);
+	client1.setServer(serverName);
+	if(server1.bindToPort(port)==false){
+		printf("unable to bind to port %d\n",port);
+		errors+=1;
+	}
+	ret=test1(&server1, &client1);
+	errors+=ret;
+	if(ret)
+		printf("test1 failed with %d errors\n", ret);
+	else
+		printf("test1 passed\n");
 
 	return errors;
 }