changeset 44:f5b5c648cd5d ultra-staging

- added (unused) simple c++ classes for udp datagrams\n- added tests for the new classes
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 20 May 2015 18:07:16 +0100
parents 24af9a14b203
children 6907e2177eb8
files core/UdpClient.cpp core/UdpServer.cpp include/UdpClient.h include/UdpServer.h resources/tests/UdpClientUdpServerTest.cpp resources/tests/make_tests.sh resources/tests/run_tests.sh
diffstat 7 files changed, 325 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/UdpClient.cpp	Wed May 20 18:07:16 2015 +0100
@@ -0,0 +1,43 @@
+/*
+ * udpClient.cpp
+ *
+ *  Created on: 19 May 2015
+ *      Author: giulio moro
+ */
+#include "../include/UdpClient.h"
+
+	UdpClient::UdpClient(){
+		enabled=false;
+	}
+	UdpClient::UdpClient(int aPort, const char* aServerName){
+		outSocket=socket(AF_INET, SOCK_DGRAM, 0);
+		if(outSocket<0){
+			enabled=false;
+			return;
+		}
+		setPort(aPort);
+		setServer(aServerName);
+		enabled=true;
+	}
+	UdpClient::~UdpClient(){
+	   close(outSocket);
+	}
+	void UdpClient::setPort(int aPort){
+		port=aPort;
+		destinationServer.sin_port = htons(port);
+		destinationServer.sin_family = AF_INET;
+	};
+	void UdpClient::setServer(const char* aServerName){
+		inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
+	};
+	int UdpClient::send(void * message, int size){
+		if(!enabled)
+			return -1;
+		unsigned int length;
+		length=sizeof(struct sockaddr_in);
+		int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
+		if (n < 0)
+			return n;
+		return 1;
+	};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/UdpServer.cpp	Wed May 20 18:07:16 2015 +0100
@@ -0,0 +1,83 @@
+/*
+ * udpServer.cpp
+ *
+ *  Created on: 19 May 2015
+ *      Author: giulio moro
+ */
+#include "../include/UdpServer.h"
+
+UdpServer::UdpServer(int aPort){
+	init(aPort);
+};
+UdpServer::UdpServer(){
+	init(0);
+}
+UdpServer::~UdpServer(){
+
+};
+bool UdpServer::init(int aPort){
+	enabled=true;
+	stTimeOut.tv_sec = 0; //set timeout to 0
+	stTimeOut.tv_usec = 0;
+	inSocket=socket(AF_INET, SOCK_DGRAM, 0);
+	if (inSocket < 0){
+		enabled=false;
+	}
+	length = sizeof(server);
+	server.sin_family=AF_INET;
+	server.sin_addr.s_addr=INADDR_ANY;
+	enabled=bindToPort(aPort);
+	wasteBufferSize=2048;
+	wasteBuffer=malloc(wasteBufferSize);
+	return enabled;
+}
+
+bool UdpServer::bindToPort(int aPort){
+	port=aPort;
+	server.sin_port=htons(port);
+	if (bind(inSocket,(struct sockaddr *)&server,length)<0){
+		enabled=false;
+		return false;
+	}
+	return true;
+};
+int UdpServer::read(//Returns the number of bytes read, or -1 if there was an error.
+					void *destBuffer,
+					int maxBytesToRead){
+	if(enabled==false)
+		return -1;
+	FD_ZERO(&stReadFDS);
+	FD_SET(inSocket, &stReadFDS);
+	int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stTimeOut);
+	if(descriptorReady<0){ //an error occurred
+		return -1;
+	}
+	int numberOfBytes=0;
+	if (FD_ISSET(inSocket, &stReadFDS)) {
+		numberOfBytes=recvfrom(inSocket,destBuffer,maxBytesToRead,0,(struct sockaddr *)&from,&fromLength);
+		if(numberOfBytes<0)
+			return -1;
+	}
+	return numberOfBytes;
+};
+int UdpServer::emptySocket(){
+	return emptySocket(0);
+}
+int UdpServer::emptySocket(int maxBytes){//discards up to maxBytes from the socket. Returns the number of bytes discarded.
+	if(wasteBuffer==NULL)
+		return -1;
+	int numberOfBytes=0;
+	while(int n=read(wasteBuffer, wasteBufferSize)){// calls the read function until it does not return any more bytes (i.e.: socket is empty)
+		if(n>0)
+			numberOfBytes+=n;
+		if(n<0)
+			return -1;
+		if(maxBytes>0 && numberOfBytes>=maxBytes)
+			break;
+	};
+	return numberOfBytes;
+}
+void* UdpServer::getWaste(){ //returns the last datagram retrieved by emptySocket()
+	return wasteBuffer;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/UdpClient.h	Wed May 20 18:07:16 2015 +0100
@@ -0,0 +1,38 @@
+/*
+ * udpClient.h
+ *
+ *  Created on: 19 May 2015
+ *      Author: giulio moro
+ */
+
+#ifndef UDPCLIENT_H_
+#define UDPCLIENT_H_
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+class UdpClient{
+	private:
+		int port;
+		int enabled;
+		int outSocket;
+		struct sockaddr_in destinationServer;
+	public:
+		UdpClient();
+		UdpClient(int aPort, const char* aServerName);
+		~UdpClient();
+		void setPort(int aPort);
+		void setServer(const char* aServerName);
+		int send(void* message, int size);
+};
+
+
+
+#endif /* UDPCLIENT_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/UdpServer.h	Wed May 20 18:07:16 2015 +0100
@@ -0,0 +1,51 @@
+/*
+ * udpServer.h
+ *
+ *  Created on: 19 May 2015
+ *      Author: giulio moro
+ */
+
+#ifndef UDPSERVER_H_
+#define UDPSERVER_H_
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+class UdpServer{
+	private:
+		int port;
+		int enabled;
+		int inSocket;
+		struct sockaddr_in server;
+		struct timeval stTimeOut;
+		fd_set stReadFDS;
+		int size;
+		void *wasteBuffer;
+		int wasteBufferSize;
+		int length;
+		socklen_t fromLength;
+		struct sockaddr_in from;
+	public:
+		UdpServer();
+		UdpServer(int aPort);
+		~UdpServer();
+		bool init(int aPort);
+		bool bindToPort(int aPort);
+		int getBoundPort() const;
+		int read(void *destBuffer,
+				int maxBytesToRead);
+		int emptySocket();
+		int emptySocket(int maxBytes);
+		void *getWaste();
+};
+
+
+
+#endif /* UDPSERVER_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/tests/UdpClientUdpServerTest.cpp	Wed May 20 18:07:16 2015 +0100
@@ -0,0 +1,101 @@
+#include "../../include/UdpServer.h"
+#include "../../include/UdpClient.h"
+#include <unistd.h>
+
+int test1(UdpServer *server, UdpClient *client){
+	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(n!=buffer){
+			printf("error: %d!=%d\n",n,buffer);
+			errors++;
+		}
+	}
+	int n=server->emptySocket();
+	if(n!=0)
+		printf("Error: the socket had %d bytes",n);
+	return errors;
+}
+int compareStrings(char * str1, char * str2){
+	if(strlen(str1)!=strlen(str2))
+		return -1;
+	for(int n=0; n<strlen(str1); n++){
+		if(str1[n]!=str2[n])
+			return -1;
+	}
+	return 0;
+}
+
+int test2(UdpServer *server, UdpClient *client){
+	char buffer[1000];
+	int tot=100;
+	int errors=0;
+	for(int n=0; n<tot; n++){
+		int num=sprintf(buffer,"%08.8f",n/1000.0);
+		client->send(&buffer,sizeof(char)*(num+1));
+	}
+	char auxBuffer[100];
+	for(int n=0; n<tot; n++){
+		int num=sprintf(auxBuffer,"%08.8f",n/1000.0);
+		server->read(&buffer,num*sizeof(char));
+		if(compareStrings(auxBuffer,buffer)==-1){
+			printf("error: %s!=%s\n",auxBuffer,buffer);
+			errors++;
+		}
+	}
+
+	return errors;
+}
+
+int test3(UdpServer *server, UdpClient *client){
+	char buffer[1000];
+	int tot=100;
+	int errors=0;
+    int totNum=0;
+	for(int n=0; n<tot; n++){
+		int num=sprintf(buffer,"%.8f",n/1000.0);
+		client->send(&buffer,sizeof(char)*(num+1));
+        totNum+=1+num;
+	}
+    int n=server->emptySocket();
+    if(n!=totNum){
+        errors=1;
+        printf("retrieved bytes differs from sent bytes: %d!=%d\n",n,totNum);
+    }
+	return errors;
+}
+
+
+int main(){
+    int port=1234;
+	UdpServer server(port);
+	UdpClient client(port,"127.0.0.1");
+	int errors=0;
+	int ret;
+	ret=test1(&server,&client);
+	errors+=ret;
+	if(ret)
+		printf("test1 failed with %d errors\n", ret);
+	else
+		printf("test1 passed\n");
+
+	ret=test2(&server,&client);
+	errors+=ret;
+	if(ret)
+		printf("test2 failed with %d errors\n", ret);
+	else
+		printf("test2 passed\n");
+
+	ret=test3(&server,&client);
+	errors+=ret;
+	if(ret)
+		printf("test3 failed with %d errors\n", ret);
+	else
+		printf("test3 passed\n");
+
+	return errors;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/tests/make_tests.sh	Wed May 20 18:07:16 2015 +0100
@@ -0,0 +1,7 @@
+#!/bin/bash
+mkdir -p build
+mkdir -p bin
+g++ -o build/UdpServer.o -O2 -c ../../core/UdpServer.cpp &&\
+g++ -O2 -o build/UdpClient.o -c ../../core/UdpClient.cpp && \
+g++ -O2 -o build/UdpClientUdpServerTest.o -c UdpClientUdpServerTest.cpp && \
+g++ -o bin/UdpClientUdpServerTest build/UdpClientUdpServerTest.o build/UdpClient.o build/UdpServer.o 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/tests/run_tests.sh	Wed May 20 18:07:16 2015 +0100
@@ -0,0 +1,2 @@
+#!/bin/bash
+bin/*