From 45b2b9d36440c1928130e637a25f41969eae4154 Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Mon, 15 Feb 2016 21:02:41 -0800 Subject: [PATCH 1/9] Change the file structure --- src/{ => Header Files}/AsynchHandler.h | 0 src/{ => Header Files}/Buffer.h | 0 src/{ => Header Files}/Daemon.h | 0 src/{ => Header Files}/Datum.h | 0 src/{ => Header Files}/Handler.h | 0 src/{ => Header Files}/IPAddr.h | 0 src/{ => Header Files}/Protocol.h | 0 src/{ => Header Files}/Timer.h | 0 src/Header Files/UDP_Client.h | 8 ++++++++ src/Header Files/UDP_Server.h | 6 ++++++ src/{ => Source Files}/Buffer.cpp | 2 +- src/{ => Source Files}/Daemon.cpp | 2 +- src/{ => Source Files}/Datum.cpp | 2 +- src/{ => Source Files}/IPAddr.cpp | 2 +- src/Source Files/UDP_Client.cpp | 5 +++++ src/Source Files/UDP_Server.cpp | 5 +++++ 16 files changed, 28 insertions(+), 4 deletions(-) rename src/{ => Header Files}/AsynchHandler.h (100%) rename src/{ => Header Files}/Buffer.h (100%) rename src/{ => Header Files}/Daemon.h (100%) rename src/{ => Header Files}/Datum.h (100%) rename src/{ => Header Files}/Handler.h (100%) rename src/{ => Header Files}/IPAddr.h (100%) rename src/{ => Header Files}/Protocol.h (100%) rename src/{ => Header Files}/Timer.h (100%) create mode 100644 src/Header Files/UDP_Client.h create mode 100644 src/Header Files/UDP_Server.h rename src/{ => Source Files}/Buffer.cpp (94%) rename src/{ => Source Files}/Daemon.cpp (97%) rename src/{ => Source Files}/Datum.cpp (94%) rename src/{ => Source Files}/IPAddr.cpp (96%) create mode 100644 src/Source Files/UDP_Client.cpp create mode 100644 src/Source Files/UDP_Server.cpp diff --git a/src/AsynchHandler.h b/src/Header Files/AsynchHandler.h similarity index 100% rename from src/AsynchHandler.h rename to src/Header Files/AsynchHandler.h diff --git a/src/Buffer.h b/src/Header Files/Buffer.h similarity index 100% rename from src/Buffer.h rename to src/Header Files/Buffer.h diff --git a/src/Daemon.h b/src/Header Files/Daemon.h similarity index 100% rename from src/Daemon.h rename to src/Header Files/Daemon.h diff --git a/src/Datum.h b/src/Header Files/Datum.h similarity index 100% rename from src/Datum.h rename to src/Header Files/Datum.h diff --git a/src/Handler.h b/src/Header Files/Handler.h similarity index 100% rename from src/Handler.h rename to src/Header Files/Handler.h diff --git a/src/IPAddr.h b/src/Header Files/IPAddr.h similarity index 100% rename from src/IPAddr.h rename to src/Header Files/IPAddr.h diff --git a/src/Protocol.h b/src/Header Files/Protocol.h similarity index 100% rename from src/Protocol.h rename to src/Header Files/Protocol.h diff --git a/src/Timer.h b/src/Header Files/Timer.h similarity index 100% rename from src/Timer.h rename to src/Header Files/Timer.h diff --git a/src/Header Files/UDP_Client.h b/src/Header Files/UDP_Client.h new file mode 100644 index 0000000..44c4cde --- /dev/null +++ b/src/Header Files/UDP_Client.h @@ -0,0 +1,8 @@ +#ifndef __INTERCHANGE_UDP_CLIENT_H +#define __INTERCHANGE_UDP_CLIENT_H + +/* +* @author Arushi Rai +*/ + +#endif \ No newline at end of file diff --git a/src/Header Files/UDP_Server.h b/src/Header Files/UDP_Server.h new file mode 100644 index 0000000..94c96f8 --- /dev/null +++ b/src/Header Files/UDP_Server.h @@ -0,0 +1,6 @@ +#ifndef __INTERCHANGE_UDP_SERVER_H +#define __INTERCHANGE_UDP_SERVER_H +/* +* @author Arushi Rai +*/ +#endif \ No newline at end of file diff --git a/src/Buffer.cpp b/src/Source Files/Buffer.cpp similarity index 94% rename from src/Buffer.cpp rename to src/Source Files/Buffer.cpp index f552d4b..5d71877 100644 --- a/src/Buffer.cpp +++ b/src/Source Files/Buffer.cpp @@ -1,4 +1,4 @@ -#include "Buffer.h" +#include "..\\Header Files\Buffer.h" Buffer::Buffer(std::int identifer, unsigned short initSize){ this->identifier = identifier; diff --git a/src/Daemon.cpp b/src/Source Files/Daemon.cpp similarity index 97% rename from src/Daemon.cpp rename to src/Source Files/Daemon.cpp index 46565f1..34c0248 100644 --- a/src/Daemon.cpp +++ b/src/Source Files/Daemon.cpp @@ -1,4 +1,4 @@ -#include "Daemon.h" +#include "..\\Header Files\Daemon.h" /* * @author Marcus Plutowski diff --git a/src/Datum.cpp b/src/Source Files/Datum.cpp similarity index 94% rename from src/Datum.cpp rename to src/Source Files/Datum.cpp index 8dbc8b1..94630b5 100644 --- a/src/Datum.cpp +++ b/src/Source Files/Datum.cpp @@ -1,4 +1,4 @@ -#include "Datum.h" +#include "..\\Header Files\Datum.h" Datum::Datum(std::string initData, std::string target){ data = initData; diff --git a/src/IPAddr.cpp b/src/Source Files/IPAddr.cpp similarity index 96% rename from src/IPAddr.cpp rename to src/Source Files/IPAddr.cpp index 8540c2f..6ecbe32 100644 --- a/src/IPAddr.cpp +++ b/src/Source Files/IPAddr.cpp @@ -1,4 +1,4 @@ -#include "IPAddr.h" +#include "..\\Header Files\IPAddr.h" #include #include #include diff --git a/src/Source Files/UDP_Client.cpp b/src/Source Files/UDP_Client.cpp new file mode 100644 index 0000000..030f29e --- /dev/null +++ b/src/Source Files/UDP_Client.cpp @@ -0,0 +1,5 @@ +#include "..\\Header Files\UDP_Client.h" + +/* +* @author Arushi Rai +*/ \ No newline at end of file diff --git a/src/Source Files/UDP_Server.cpp b/src/Source Files/UDP_Server.cpp new file mode 100644 index 0000000..fe98a9b --- /dev/null +++ b/src/Source Files/UDP_Server.cpp @@ -0,0 +1,5 @@ +#include "..\\Header Files\UDP_Server.h" + +/* +* @author Arushi Rai +*/ \ No newline at end of file From 6a96ab14b7132b203ba7449f6a32eb049ef4d03f Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Tue, 16 Feb 2016 09:38:02 -0800 Subject: [PATCH 2/9] Add std namespace scope identifier --- src/Header Files/Protocol.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Header Files/Protocol.h b/src/Header Files/Protocol.h index 43bbc37..07bb4b4 100644 --- a/src/Header Files/Protocol.h +++ b/src/Header Files/Protocol.h @@ -14,14 +14,14 @@ class Handler; class Protocol{ public: - virtual Protocol(string name, void (*retData) (string), int port) = 0; + virtual Protocol(std::string name, void (*retData) (std::string), int port) = 0; - virtual void setName(string newName) = 0; - virtual string getName() = 0; + virtual void setName(std::string newName) = 0; + virtual std::string getName() = 0; protected: - void (*retData) (string); + void (*retData) (std::string); boost::asio::io_service io_service; - string name; + std::string name; int targetPort; }; From dbf79b55afe48811a6f9c3f0e0e4e7d451054e23 Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Tue, 16 Feb 2016 10:15:47 -0800 Subject: [PATCH 3/9] Implement the UDP Protocol --- src/Header Files/UDP_Protocol.h | 29 +++++++++++++++++++++++++++++ src/Source Files/UDP_Protocol.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/Header Files/UDP_Protocol.h create mode 100644 src/Source Files/UDP_Protocol.cpp diff --git a/src/Header Files/UDP_Protocol.h b/src/Header Files/UDP_Protocol.h new file mode 100644 index 0000000..0dbf56a --- /dev/null +++ b/src/Header Files/UDP_Protocol.h @@ -0,0 +1,29 @@ +#ifndef __INTERCHANGE_UDP_PROTOCOL_H +#define __INTERCHANGE_UDP_PROTOCOL_H + +#include "UDP\UDP_Client.h" +#include "UDP\UDP_Server.h" +#include "Protocol.h" +#include "IPAddr.h" + +#include +#include + +/* +* @author Arushi Rai +*/ + +class UDP_Protocol :: public Protocol { +private: + UDP_Server server; //make static? + UDP_Client client; //make static? + IPAddr address; +public: + UDP_Protocol(std::string name, void (*retData) (std::string), int port); + std::string getName(); + void start(); //will start thread to run client and server simultaniously + std::string receive(); + void send(string message); +}; + +#endif \ No newline at end of file diff --git a/src/Source Files/UDP_Protocol.cpp b/src/Source Files/UDP_Protocol.cpp new file mode 100644 index 0000000..0b01ed1 --- /dev/null +++ b/src/Source Files/UDP_Protocol.cpp @@ -0,0 +1,25 @@ +#include "..\\Header Files\UDP_Protocol" + +/* +* @author Arushi Rai +*/ + +UDP_Protocol::UDP_Protocol (std::string name, void (*retData) (std::string), string addr, int port) : Protocol(name, *retData, port) { + address = new IPAddr(addr); + client = new UDP_Client(address.getIPString(), port); + server = new UDP_Server(); +} + +std::string UDP_Protocol::getName() { + return name; +} + +void UDP_Protocol::start() { } //will start thread to run client and server simultaniously + +std::string UDP_Protocol::receive() { + return server.empty(); +} + +void UDP_Protocol::send(string message) { + client.send(message); +} \ No newline at end of file From 16f23747390cf013224c3ebd815f5fb1410c076e Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Tue, 16 Feb 2016 10:24:29 -0800 Subject: [PATCH 4/9] Set up the UDP client and server header files --- src/Header Files/UDP/UDP_Client.h | 37 +++++++++++++++++++++++ src/Header Files/UDP/UDP_Server.h | 34 +++++++++++++++++++++ src/Header Files/UDP_Client.h | 8 ----- src/Header Files/UDP_Server.h | 6 ---- src/Source Files/{ => UDP}/UDP_Client.cpp | 0 src/Source Files/{ => UDP}/UDP_Server.cpp | 0 src/Source Files/UDP_Protocol.cpp | 2 +- 7 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 src/Header Files/UDP/UDP_Client.h create mode 100644 src/Header Files/UDP/UDP_Server.h delete mode 100644 src/Header Files/UDP_Client.h delete mode 100644 src/Header Files/UDP_Server.h rename src/Source Files/{ => UDP}/UDP_Client.cpp (100%) rename src/Source Files/{ => UDP}/UDP_Server.cpp (100%) diff --git a/src/Header Files/UDP/UDP_Client.h b/src/Header Files/UDP/UDP_Client.h new file mode 100644 index 0000000..76b3872 --- /dev/null +++ b/src/Header Files/UDP/UDP_Client.h @@ -0,0 +1,37 @@ +#ifndef __INTERCHANGE_UDP_CLIENT_H +#define __INTERCHANGE_UDP_CLIENT_H + +#include +#include +#include +#include +#include +#include //used for fprintf + +using namespace std; + +/* +* @author Arushi Rai +*/ + +class UDP_Client { +private: + char recipientAddr[32]; + queue buffer; + int udp_socket; + int PORT; + struct sockaddr_in servaddr; + + void setup(); + void bindSocket(int s, struct sockaddr_in* myaddr); + void timestamp(string* data, bool add); + bool validate(string data); //validates the buffer + string empty(); //empties buffer + +public: + UDP_Client(); + void send(); //sends the next item in the buffer, empties it as well + void addToSendBuffer(string message); +}; + +#endif \ No newline at end of file diff --git a/src/Header Files/UDP/UDP_Server.h b/src/Header Files/UDP/UDP_Server.h new file mode 100644 index 0000000..2155a7d --- /dev/null +++ b/src/Header Files/UDP/UDP_Server.h @@ -0,0 +1,34 @@ +#ifndef __INTERCHANGE_UDP_SERVER_H +#define __INTERCHANGE_UDP_SERVER_H + +#include +#include +#include +#include +#include +#include //used for fprintf + +using namespace std; + +/* +* @author Arushi Rai +*/ + +class UDP_Server { +private: + char recipientAddr[32]; + int server_socket; + int PORT; + queue buffer; + + void start(); + void timestamp(string* data, bool add); + bool validate(string data); //validates the buffer + +public: + UDP_Server(string address, int port); + void receive(); + string empty(); //empties buffer //use a more intuitive name +}; + +#endif \ No newline at end of file diff --git a/src/Header Files/UDP_Client.h b/src/Header Files/UDP_Client.h deleted file mode 100644 index 44c4cde..0000000 --- a/src/Header Files/UDP_Client.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __INTERCHANGE_UDP_CLIENT_H -#define __INTERCHANGE_UDP_CLIENT_H - -/* -* @author Arushi Rai -*/ - -#endif \ No newline at end of file diff --git a/src/Header Files/UDP_Server.h b/src/Header Files/UDP_Server.h deleted file mode 100644 index 94c96f8..0000000 --- a/src/Header Files/UDP_Server.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef __INTERCHANGE_UDP_SERVER_H -#define __INTERCHANGE_UDP_SERVER_H -/* -* @author Arushi Rai -*/ -#endif \ No newline at end of file diff --git a/src/Source Files/UDP_Client.cpp b/src/Source Files/UDP/UDP_Client.cpp similarity index 100% rename from src/Source Files/UDP_Client.cpp rename to src/Source Files/UDP/UDP_Client.cpp diff --git a/src/Source Files/UDP_Server.cpp b/src/Source Files/UDP/UDP_Server.cpp similarity index 100% rename from src/Source Files/UDP_Server.cpp rename to src/Source Files/UDP/UDP_Server.cpp diff --git a/src/Source Files/UDP_Protocol.cpp b/src/Source Files/UDP_Protocol.cpp index 0b01ed1..b93ef43 100644 --- a/src/Source Files/UDP_Protocol.cpp +++ b/src/Source Files/UDP_Protocol.cpp @@ -21,5 +21,5 @@ std::string UDP_Protocol::receive() { } void UDP_Protocol::send(string message) { - client.send(message); + client.addToSendBuffer(message); } \ No newline at end of file From b44451ed518a197b0deacb4462a3eeda533127f6 Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Tue, 16 Feb 2016 10:29:42 -0800 Subject: [PATCH 5/9] Set up the UDP client and server source files --- src/Header Files/UDP/UDP_Client.h | 4 +- src/Source Files/UDP/UDP_Client.cpp | 85 ++++++++++++++++++++++++- src/Source Files/UDP/UDP_Server.cpp | 98 +++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 3 deletions(-) diff --git a/src/Header Files/UDP/UDP_Client.h b/src/Header Files/UDP/UDP_Client.h index 76b3872..1ddd5cc 100644 --- a/src/Header Files/UDP/UDP_Client.h +++ b/src/Header Files/UDP/UDP_Client.h @@ -20,8 +20,8 @@ class UDP_Client { queue buffer; int udp_socket; int PORT; - struct sockaddr_in servaddr; - + struct sockaddr_in servaddr; + void setup(); void bindSocket(int s, struct sockaddr_in* myaddr); void timestamp(string* data, bool add); diff --git a/src/Source Files/UDP/UDP_Client.cpp b/src/Source Files/UDP/UDP_Client.cpp index 030f29e..c4a7885 100644 --- a/src/Source Files/UDP/UDP_Client.cpp +++ b/src/Source Files/UDP/UDP_Client.cpp @@ -2,4 +2,87 @@ /* * @author Arushi Rai -*/ \ No newline at end of file +*/ + +UDP_Client::UDP_Client(char recipientAddr[128]) { + memcpy(this->recipientAddr, recipientAddr, sizeof(recipientAddr)); + setup(); +} + +//ic +void UDP_Client::setup() { + //binds socket and sets upp server address + int udp_socket; + + if ((udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + std::perror("cannot create socket"); + } + + struct sockaddr_in myaddr; + + bindSocket(udp_socket, myaddr); + + //initializes the servaddr struct + memset((char*)&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(PORT); + memcpy((void *)&servaddr.sin_addr, recipientAddr[0], recipientAddr_length); + +} + +void UDP_Client::addToSendBuffer(string message) { + if (!validate(message)) //if the data fails the validation, then don't send + return; + + timestamp(&message, true); + buffer.push_back(message); +} + +void UDP_Client::send() { + string data; + data = empty(); + char* message = data; + if (sendto(udp_socket, message, strlen(message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + perror("sendto failed"); + return; +} + +void UDPClient::bindSocket (int s, struct sockaddr_in* myaddr) { + memset((char *)myaddr, 0, sizeof(*myaddr)); + + *myaddr.sin_family = AF_INET; + *myaddr.sin_addr = htonl(INADDR_ANY); //IP address + *myaddr.sin_port = htons(7848); //socket + if((bind(s, (struct sockaddr *)myaddr, sizeof(*myaddr))) < 0) + perror("cannot bind"); +} +//ic +bool UDP_Client::validate() { + bool validation = true; //change default to false after receiving data format + + return validation; +} + +//ic +string UDP_Client::empty() { + string data = buffer.front(); + buffer.pop(); + timestamp(&data, false); + + return data; +} + +void UDP_Client::timestamp(string* data, bool add) { + string time; + + if (add) + time = "; ASB: "; //stands for added to send buffer + else + time = "; ESB: "; //stands for emptied from send buffer + + //time += get time here + time += ";" + *data += time; +} + + diff --git a/src/Source Files/UDP/UDP_Server.cpp b/src/Source Files/UDP/UDP_Server.cpp index fe98a9b..a520753 100644 --- a/src/Source Files/UDP/UDP_Server.cpp +++ b/src/Source Files/UDP/UDP_Server.cpp @@ -2,4 +2,102 @@ /* * @author Arushi Rai +*/ + +#include "udp_server.h" + +/* +#define PORT 5789 +#define BUFFER_LENGTH 64 //enter bytes +*/ + +UDP_Server::UDP_Server( char recipientAddr[128]) { + memcpy(this->recipientAddr, recipientAddr, sizeof(recipientAddr)); + start(); +} + +//ic +void UDP_Server::start() { + //set up server here + struct sockaddr_in myaddr; /* our address */ + struct sockaddr_in remaddr; /* remote address */ + socklen_t addrlen = sizeof(remaddr); /* length of addresses */ + int recvlen; /* # bytes received */ + unsigned char buf[BUFSIZE]; /* receive buffer */ + + /* create a UDP socket */ + + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("cannot create socket\n"); + return 0; + } + + /* bind the socket to any valid IP address and a specific port */ + + memset((char *)&myaddr, 0, sizeof(myaddr)); + myaddr.sin_family = AF_INET; + myaddr.sin_addr.s_addr = htonl(INADDR_ANY); + myaddr.sin_port = htons(PORT); + + if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { + perror("bind failed"); + return 0; + } + + /* now loop, receiving data and printing what we received */ + for (;;) { + recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen); + printf("received %d bytes\n", recvlen); + if (recvlen > 0) { + buf[recvlen] = 0; + printf("received message: \"%s\"\n", buf); + } + } + /* never exits */ + } + + +//ic +string UDP_Server::receive() { + string data; + //enter UDP code to receive + + if (!validate(data)) + return ; + + timestamp(&data, true); + buffer.push_back(data); + return; +} + +//ic +bool UDP_Server::validate() { + bool validation = true; //change default to false after receiving data format + + return validation; +} + +string UDP_Server::empty() { + string data = buffer.front(); + buffer.pop(); + timestamp(&data, false); + + return data; +} + +void UDP_Server::timestamp(string* data, bool add) { + string time; + + if (add) + time = "; ARB: "; //stands for added to retrieve buffer + else + time = "; ERB: "; //stands for emptied from retrieve buffer + + //time += get time here + time += ";" + *data += time; +} + +/* +sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen) */ \ No newline at end of file From b905209ada4425719a9bf30aaf5911b4b11d0e93 Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Tue, 16 Feb 2016 15:53:34 -0800 Subject: [PATCH 6/9] Fix errors --- src/Header Files/Buffer.h | 2 +- src/Header Files/Protocol.h | 10 +++------- src/Source Files/Buffer.cpp | 21 +++++++++++---------- src/Source Files/Protocol.cpp | 8 ++++++++ 4 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 src/Source Files/Protocol.cpp diff --git a/src/Header Files/Buffer.h b/src/Header Files/Buffer.h index 5c9eadd..52d5e3a 100644 --- a/src/Header Files/Buffer.h +++ b/src/Header Files/Buffer.h @@ -21,7 +21,7 @@ class Buffer{ private: std::string identifier; - std::deque data; + std::queue data; unsigned short size; }; diff --git a/src/Header Files/Protocol.h b/src/Header Files/Protocol.h index 07bb4b4..5727d9e 100644 --- a/src/Header Files/Protocol.h +++ b/src/Header Files/Protocol.h @@ -9,17 +9,13 @@ * @author Marcus Plutowski */ -class Handler; - class Protocol{ public: - virtual Protocol(std::string name, void (*retData) (std::string), int port) = 0; - - virtual void setName(std::string newName) = 0; - virtual std::string getName() = 0; + Protocol(int port); + std::string getName(); protected: - void (*retData) (std::string); + //void (*retData) (std::string); boost::asio::io_service io_service; std::string name; int targetPort; diff --git a/src/Source Files/Buffer.cpp b/src/Source Files/Buffer.cpp index 5d71877..c5aa6a5 100644 --- a/src/Source Files/Buffer.cpp +++ b/src/Source Files/Buffer.cpp @@ -1,27 +1,27 @@ #include "..\\Header Files\Buffer.h" -Buffer::Buffer(std::int identifer, unsigned short initSize){ +Buffer::Buffer(std::int identifer, unsigned short initSize){ this->identifier = identifier; size = initSize; } -std::string Buffer::getIdentifier(){ +std::string Buffer::getIdentifier(){ return identifier; } -void Buffer::setIdentifier(std::string identifier){ +void Buffer::setIdentifier(std::string identifier){ this->identifier = identifier; } -void Buffer::push(T newData){ - if(size = 0){return;} +void Buffer::push(T newData){ + if(size == 0){return;} //using assignment operator, wrong - while(data.getSize() = size){ + while(data.getSize() == size){ //using assignment operator, wrong this->pop(); } data.push(newData); } -T Buffer::pop(){ +T Buffer::pop(){ if(!data.empty()){ T buff; buff = data.front() @@ -29,15 +29,16 @@ T Buffer::pop(){ return buff; } } -T Buffer::get(){ +T Buffer::get(){ if(!data.empty()){ return data.front(); } } -void Buffer::resize(unsigned short newSize){ +void Buffer::resize(unsigned short newSize){ data.resize(newSize); } -unsigned short getSize(){ + +unsigned short Buffer::getSize(){ return size; } diff --git a/src/Source Files/Protocol.cpp b/src/Source Files/Protocol.cpp new file mode 100644 index 0000000..b591a6f --- /dev/null +++ b/src/Source Files/Protocol.cpp @@ -0,0 +1,8 @@ +Protocol::Protocol(int port) { + name = " "; + targetPort = port; +} + +std::string Protocol::getName() { + return name; +} From fa4506f02d0140fab2340273a3e0e076fb5618a8 Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Tue, 16 Feb 2016 19:43:19 -0800 Subject: [PATCH 7/9] Implement getSendable() which returns the data members concactinated --- src/Header Files/Datum.h | 5 ++++ src/Header Files/Protocol.h | 6 +++-- src/Source Files/Datum.cpp | 17 ++++++++++++++ src/Source Files/Protocol.cpp | 8 ++++++- src/Source Files/UDP/UDP_Client.cpp | 36 ++++++++++++++--------------- 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/Header Files/Datum.h b/src/Header Files/Datum.h index 41a8230..f5613ce 100644 --- a/src/Header Files/Datum.h +++ b/src/Header Files/Datum.h @@ -5,6 +5,10 @@ #include "Timer.h" #include +/* +* @author Marcus Plutowski +*/ + class Datum{ public: Datum(std::string initData, std::string target, std::string protocol); @@ -14,6 +18,7 @@ class Datum{ void setData(std::string newData); std::string getData(); + std::string getSendable(); std::string getTarget(); diff --git a/src/Header Files/Protocol.h b/src/Header Files/Protocol.h index 5727d9e..18bc002 100644 --- a/src/Header Files/Protocol.h +++ b/src/Header Files/Protocol.h @@ -6,7 +6,7 @@ #include /* -* @author Marcus Plutowski +* @author Marcus Plutowski, Arushi Rai */ class Protocol{ @@ -15,10 +15,12 @@ class Protocol{ Protocol(int port); std::string getName(); protected: - //void (*retData) (std::string); boost::asio::io_service io_service; std::string name; int targetPort; + + virtual send(std::string data) = 0; + virtual receive() = 0; }; #endif diff --git a/src/Source Files/Datum.cpp b/src/Source Files/Datum.cpp index 94630b5..763c259 100644 --- a/src/Source Files/Datum.cpp +++ b/src/Source Files/Datum.cpp @@ -1,5 +1,9 @@ #include "..\\Header Files\Datum.h" +/* +* @author Marcus Plutowski, Arushi Rai +*/ + Datum::Datum(std::string initData, std::string target){ data = initData; targetName = target; @@ -24,6 +28,19 @@ std::string Datum::getData(){ return data; } +std::string getSendable(){ + std::string sendable; + sendable = protocolTransited; + sendable += "; "; //seperation characters + sendable += data; + + if (protocolTransited[0] == 'T') { + //enter TCP data string format + } + + return sendable; +} + void Datum::setProtocol(std::string newProtocol){ protocolTransited = newProtocol; } diff --git a/src/Source Files/Protocol.cpp b/src/Source Files/Protocol.cpp index b591a6f..f6ecd2f 100644 --- a/src/Source Files/Protocol.cpp +++ b/src/Source Files/Protocol.cpp @@ -1,4 +1,10 @@ -Protocol::Protocol(int port) { +#include "..\\Header Files\Protocol.h" + +/* +* @author Arushi Rai +*/ + +Protocol ::Protocol(int port) { name = " "; targetPort = port; } diff --git a/src/Source Files/UDP/UDP_Client.cpp b/src/Source Files/UDP/UDP_Client.cpp index c4a7885..e019a2a 100644 --- a/src/Source Files/UDP/UDP_Client.cpp +++ b/src/Source Files/UDP/UDP_Client.cpp @@ -4,14 +4,15 @@ * @author Arushi Rai */ -UDP_Client::UDP_Client(char recipientAddr[128]) { +UDP_Client::UDP_Client(char recipientAddr[32], int port) { + buffer = new Buffer("UDP Send Buffer", BUFFER_SIZE); memcpy(this->recipientAddr, recipientAddr, sizeof(recipientAddr)); setup(); } //ic void UDP_Client::setup() { - //binds socket and sets upp server address + //binds socket and sets udp server address int udp_socket; if ((udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { @@ -33,17 +34,17 @@ void UDP_Client::setup() { void UDP_Client::addToSendBuffer(string message) { if (!validate(message)) //if the data fails the validation, then don't send return; - - timestamp(&message, true); - buffer.push_back(message); + Datum temp = Datum(message, "std::string target", "UDP Protocol"); + timestamp(&temp, true); + buffer.push(buffer.push()); } void UDP_Client::send() { - string data; + Datum data; data = empty(); - char* message = data; + char* message = data.getSendable(); if (sendto(udp_socket, message, strlen(message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { - perror("sendto failed"); + std::perror("sendto failed"); return; } @@ -52,28 +53,27 @@ void UDPClient::bindSocket (int s, struct sockaddr_in* myaddr) { *myaddr.sin_family = AF_INET; *myaddr.sin_addr = htonl(INADDR_ANY); //IP address - *myaddr.sin_port = htons(7848); //socket + *myaddr.sin_port = htons(PORT); //socket if((bind(s, (struct sockaddr *)myaddr, sizeof(*myaddr))) < 0) - perror("cannot bind"); + std::perror("cannot bind"); } //ic -bool UDP_Client::validate() { +bool UDP_Client::validate(string data) { bool validation = true; //change default to false after receiving data format return validation; } //ic -string UDP_Client::empty() { - string data = buffer.front(); - buffer.pop(); - timestamp(&data, false); +Datum UDP_Client::empty() { + timestamp(&(buffer.get()), false); + Datum data = buffer.pop(); return data; } -void UDP_Client::timestamp(string* data, bool add) { - string time; +void UDP_Client::timestamp(Datum* data, bool add) { + /*string time; if (add) time = "; ASB: "; //stands for added to send buffer @@ -82,7 +82,7 @@ void UDP_Client::timestamp(string* data, bool add) { //time += get time here time += ";" - *data += time; + *data += time; */ } From e8becdb750af8f0b604e9b0cf360908a72b180b8 Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Wed, 17 Feb 2016 13:20:18 -0800 Subject: [PATCH 8/9] Complete the implementation of incomplete functions --- src/Header Files/UDP/UDP_Client.h | 15 ++-- src/Header Files/UDP/UDP_Server.h | 15 +++- src/Header Files/UDP_Protocol.h | 4 +- src/Source Files/Buffer.cpp | 4 +- src/Source Files/Datum.cpp | 2 +- src/Source Files/UDP/UDP_Client.cpp | 18 ++-- src/Source Files/UDP/UDP_Server.cpp | 126 +++++++++++----------------- src/Source Files/UDP_Protocol.cpp | 6 +- 8 files changed, 90 insertions(+), 100 deletions(-) diff --git a/src/Header Files/UDP/UDP_Client.h b/src/Header Files/UDP/UDP_Client.h index 1ddd5cc..a976a18 100644 --- a/src/Header Files/UDP/UDP_Client.h +++ b/src/Header Files/UDP/UDP_Client.h @@ -2,12 +2,16 @@ #define __INTERCHANGE_UDP_CLIENT_H #include -#include #include #include #include #include //used for fprintf +#include "..\Buffer.h" +#include "..\Datum.h" + +#define BUFFER_SIZE 64; //needs to be changed + using namespace std; /* @@ -17,19 +21,20 @@ using namespace std; class UDP_Client { private: char recipientAddr[32]; - queue buffer; + Buffer buffer; + int udp_socket; int PORT; struct sockaddr_in servaddr; void setup(); void bindSocket(int s, struct sockaddr_in* myaddr); - void timestamp(string* data, bool add); + void timestamp(Datum* data, bool add); bool validate(string data); //validates the buffer - string empty(); //empties buffer + Datum empty(); //empties buffer public: - UDP_Client(); + UDP_Client(string address, int port); void send(); //sends the next item in the buffer, empties it as well void addToSendBuffer(string message); }; diff --git a/src/Header Files/UDP/UDP_Server.h b/src/Header Files/UDP/UDP_Server.h index 2155a7d..d646b8e 100644 --- a/src/Header Files/UDP/UDP_Server.h +++ b/src/Header Files/UDP/UDP_Server.h @@ -2,12 +2,16 @@ #define __INTERCHANGE_UDP_SERVER_H #include -#include #include #include #include #include //used for fprintf +#include "..\Buffer.h" +#include "..\Datum.h" + +#define BUFFER_SIZE 64; + using namespace std; /* @@ -17,18 +21,21 @@ using namespace std; class UDP_Server { private: char recipientAddr[32]; + Buffer buffer; + int server_socket; int PORT; - queue buffer; void start(); - void timestamp(string* data, bool add); + void timestamp(Datum* data, bool add); bool validate(string data); //validates the buffer + struct sockaddr_in remaddr; // remote address + socklen_t addrlen = sizeof(remaddr); //length of addresses public: UDP_Server(string address, int port); void receive(); string empty(); //empties buffer //use a more intuitive name }; - + #endif \ No newline at end of file diff --git a/src/Header Files/UDP_Protocol.h b/src/Header Files/UDP_Protocol.h index 0dbf56a..cea492f 100644 --- a/src/Header Files/UDP_Protocol.h +++ b/src/Header Files/UDP_Protocol.h @@ -19,11 +19,11 @@ class UDP_Protocol :: public Protocol { UDP_Client client; //make static? IPAddr address; public: - UDP_Protocol(std::string name, void (*retData) (std::string), int port); + UDP_Protocol(int port); std::string getName(); void start(); //will start thread to run client and server simultaniously std::string receive(); - void send(string message); + void send(string message); //might not need this function }; #endif \ No newline at end of file diff --git a/src/Source Files/Buffer.cpp b/src/Source Files/Buffer.cpp index c5aa6a5..8c9d507 100644 --- a/src/Source Files/Buffer.cpp +++ b/src/Source Files/Buffer.cpp @@ -14,9 +14,9 @@ void Buffer::setIdentifier(std::string identifier){ } void Buffer::push(T newData){ - if(size == 0){return;} //using assignment operator, wrong + if(size == 0){return;} - while(data.getSize() == size){ //using assignment operator, wrong + while(data.getSize() == size){ this->pop(); } data.push(newData); diff --git a/src/Source Files/Datum.cpp b/src/Source Files/Datum.cpp index 763c259..b650881 100644 --- a/src/Source Files/Datum.cpp +++ b/src/Source Files/Datum.cpp @@ -4,7 +4,7 @@ * @author Marcus Plutowski, Arushi Rai */ -Datum::Datum(std::string initData, std::string target){ +Datum::Datum(std::string initData, std::string target, std::string protocol){ data = initData; targetName = target; protocolTransited = protocol; diff --git a/src/Source Files/UDP/UDP_Client.cpp b/src/Source Files/UDP/UDP_Client.cpp index e019a2a..505a863 100644 --- a/src/Source Files/UDP/UDP_Client.cpp +++ b/src/Source Files/UDP/UDP_Client.cpp @@ -4,19 +4,19 @@ * @author Arushi Rai */ -UDP_Client::UDP_Client(char recipientAddr[32], int port) { +UDP_Client::UDP_Client(string address, int port) { buffer = new Buffer("UDP Send Buffer", BUFFER_SIZE); - memcpy(this->recipientAddr, recipientAddr, sizeof(recipientAddr)); + PORT = port; + *char temp[] = address; + memcpy(recipientAddr, temp, sizeof(recipientAddr)); setup(); } //ic void UDP_Client::setup() { - //binds socket and sets udp server address - int udp_socket; - - if ((udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - std::perror("cannot create socket"); + //binds socket and sets udp server address + if ((udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("cannot create socket"); } struct sockaddr_in myaddr; @@ -48,7 +48,7 @@ void UDP_Client::send() { return; } -void UDPClient::bindSocket (int s, struct sockaddr_in* myaddr) { +void UDP_Client::bindSocket (int s, struct sockaddr_in* myaddr) { memset((char *)myaddr, 0, sizeof(*myaddr)); *myaddr.sin_family = AF_INET; @@ -66,8 +66,8 @@ bool UDP_Client::validate(string data) { //ic Datum UDP_Client::empty() { - timestamp(&(buffer.get()), false); Datum data = buffer.pop(); + timestamp(&data, false); return data; } diff --git a/src/Source Files/UDP/UDP_Server.cpp b/src/Source Files/UDP/UDP_Server.cpp index a520753..a59fef3 100644 --- a/src/Source Files/UDP/UDP_Server.cpp +++ b/src/Source Files/UDP/UDP_Server.cpp @@ -4,100 +4,76 @@ * @author Arushi Rai */ -#include "udp_server.h" - -/* -#define PORT 5789 -#define BUFFER_LENGTH 64 //enter bytes -*/ - -UDP_Server::UDP_Server( char recipientAddr[128]) { - memcpy(this->recipientAddr, recipientAddr, sizeof(recipientAddr)); - start(); +UDP_Server::UDP_Server(string address, int port) { + buffer = new Buffer("UDP Receive Buffer", BUFFER_SIZE); + PORT = port; + char temp[] = address; + memcpy(recipientAddr, temp, sizeof(recipientAddr)); + start(); } -//ic void UDP_Server::start() { - //set up server here - struct sockaddr_in myaddr; /* our address */ - struct sockaddr_in remaddr; /* remote address */ - socklen_t addrlen = sizeof(remaddr); /* length of addresses */ - int recvlen; /* # bytes received */ - unsigned char buf[BUFSIZE]; /* receive buffer */ - - /* create a UDP socket */ - - if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - perror("cannot create socket\n"); - return 0; - } - - /* bind the socket to any valid IP address and a specific port */ - - memset((char *)&myaddr, 0, sizeof(myaddr)); - myaddr.sin_family = AF_INET; - myaddr.sin_addr.s_addr = htonl(INADDR_ANY); - myaddr.sin_port = htons(PORT); - - if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { - perror("bind failed"); - return 0; - } - - /* now loop, receiving data and printing what we received */ - for (;;) { - recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen); - printf("received %d bytes\n", recvlen); - if (recvlen > 0) { - buf[recvlen] = 0; - printf("received message: \"%s\"\n", buf); - } - } - /* never exits */ - } - + struct sockaddr_in myaddr; // our address + + if ((server_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("cannot create socket\n"); + return 0; + } + + memset((char *)&myaddr, 0, sizeof(myaddr)); + myaddr.sin_family = AF_INET; + myaddr.sin_addr.s_addr = htonl(INADDR_ANY); + myaddr.sin_port = htons(PORT); + + if (bind(server_socket, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { + perror("bind failed"); + return 0; + } +} -//ic -string UDP_Server::receive() { - string data; - //enter UDP code to receive +void UDP_Server::receive() { + unsigned char buf[BUFFER_SIZE]; //temp receive buffer + int recvlen; //# bytes received - if (!validate(data)) - return ; + recvlen = recvfrom(server_socket, buf, BUFFER_SIZE, 0, (struct sockaddr *)&remaddr, &addrlen); + if (recvlen > 0) { + buf[recvlen] = 0; + } - timestamp(&data, true); - buffer.push_back(data); + string data(buff); + if (!validate(data)) return; + + Datum temp(data, "target", "UDP Protocol"); + timestamp(&temp, true); + buffer.push(temp); + return; } //ic bool UDP_Server::validate() { - bool validation = true; //change default to false after receiving data format + bool validation = true; //change default to false after receiving data format - return validation; + return validation; } string UDP_Server::empty() { - string data = buffer.front(); - buffer.pop(); - timestamp(&data, false); + Datum data = buffer.pop(); + timestamp(&data, false); - return data; + return data; } +//ic void UDP_Server::timestamp(string* data, bool add) { - string time; + /*string time; - if (add) - time = "; ARB: "; //stands for added to retrieve buffer - else - time = "; ERB: "; //stands for emptied from retrieve buffer + if (add) + time = "; ARB: "; //stands for added to retrieve buffer + else + time = "; ERB: "; //stands for emptied from retrieve buffer - //time += get time here - time += ";" - *data += time; + //time += get time here + time += ";" + *data += time; */ } - -/* -sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen) -*/ \ No newline at end of file diff --git a/src/Source Files/UDP_Protocol.cpp b/src/Source Files/UDP_Protocol.cpp index b93ef43..79825a8 100644 --- a/src/Source Files/UDP_Protocol.cpp +++ b/src/Source Files/UDP_Protocol.cpp @@ -4,10 +4,12 @@ * @author Arushi Rai */ -UDP_Protocol::UDP_Protocol (std::string name, void (*retData) (std::string), string addr, int port) : Protocol(name, *retData, port) { +UDP_Protocol::UDP_Protocol (int port) : Protocol(port){ + std::string address = " "; + name = "UDP Protocol"; address = new IPAddr(addr); client = new UDP_Client(address.getIPString(), port); - server = new UDP_Server(); + server = new UDP_Server(address.getIPString(), port); } std::string UDP_Protocol::getName() { From 26972cdba037ed4cfacdb465df9f9f5db762a8c2 Mon Sep 17 00:00:00 2001 From: Arushi Rai Date: Sun, 21 Feb 2016 11:22:32 -0800 Subject: [PATCH 9/9] Fix all bugs --- src/Header Files/UDP/UDP_Client.h | 4 +++- src/Header Files/UDP/UDP_Server.h | 5 +++-- src/Source Files/UDP/UDP_Client.cpp | 29 +++++++++++++++-------------- src/Source Files/UDP/UDP_Server.cpp | 22 +++++++++++----------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/Header Files/UDP/UDP_Client.h b/src/Header Files/UDP/UDP_Client.h index a976a18..44b3dc0 100644 --- a/src/Header Files/UDP/UDP_Client.h +++ b/src/Header Files/UDP/UDP_Client.h @@ -6,6 +6,7 @@ #include #include #include //used for fprintf +#include #include "..\Buffer.h" #include "..\Datum.h" @@ -26,9 +27,10 @@ class UDP_Client { int udp_socket; int PORT; struct sockaddr_in servaddr; + struct sockaddr_in myaddr; void setup(); - void bindSocket(int s, struct sockaddr_in* myaddr); + void bindSocket(); void timestamp(Datum* data, bool add); bool validate(string data); //validates the buffer Datum empty(); //empties buffer diff --git a/src/Header Files/UDP/UDP_Server.h b/src/Header Files/UDP/UDP_Server.h index d646b8e..19519a7 100644 --- a/src/Header Files/UDP/UDP_Server.h +++ b/src/Header Files/UDP/UDP_Server.h @@ -6,6 +6,7 @@ #include #include #include //used for fprintf +#include #include "..\Buffer.h" #include "..\Datum.h" @@ -30,8 +31,8 @@ class UDP_Server { void timestamp(Datum* data, bool add); bool validate(string data); //validates the buffer struct sockaddr_in remaddr; // remote address - socklen_t addrlen = sizeof(remaddr); //length of addresses - + socklen_t addrlen; //length of addresses + public: UDP_Server(string address, int port); void receive(); diff --git a/src/Source Files/UDP/UDP_Client.cpp b/src/Source Files/UDP/UDP_Client.cpp index 505a863..fe1d8c1 100644 --- a/src/Source Files/UDP/UDP_Client.cpp +++ b/src/Source Files/UDP/UDP_Client.cpp @@ -7,7 +7,7 @@ UDP_Client::UDP_Client(string address, int port) { buffer = new Buffer("UDP Send Buffer", BUFFER_SIZE); PORT = port; - *char temp[] = address; + char* temp = (char*)address.c_str(); memcpy(recipientAddr, temp, sizeof(recipientAddr)); setup(); } @@ -21,14 +21,15 @@ void UDP_Client::setup() { struct sockaddr_in myaddr; - bindSocket(udp_socket, myaddr); - + bindSocket(); + struct hostent *hp; /* host information */ + char *host = "localhost"; + hp = gethostbyname(host); //initializes the servaddr struct memset((char*)&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); - memcpy((void *)&servaddr.sin_addr, recipientAddr[0], recipientAddr_length); - + memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length); } void UDP_Client::addToSendBuffer(string message) { @@ -36,26 +37,26 @@ void UDP_Client::addToSendBuffer(string message) { return; Datum temp = Datum(message, "std::string target", "UDP Protocol"); timestamp(&temp, true); - buffer.push(buffer.push()); + buffer.push(temp); } void UDP_Client::send() { Datum data; data = empty(); - char* message = data.getSendable(); + char* message = (char*) data.getSendable().c_str(); if (sendto(udp_socket, message, strlen(message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { - std::perror("sendto failed"); + perror("sendto failed"); return; } void UDP_Client::bindSocket (int s, struct sockaddr_in* myaddr) { - memset((char *)myaddr, 0, sizeof(*myaddr)); + memset((char *)&myaddr, 0, sizeof(myaddr)); - *myaddr.sin_family = AF_INET; - *myaddr.sin_addr = htonl(INADDR_ANY); //IP address - *myaddr.sin_port = htons(PORT); //socket - if((bind(s, (struct sockaddr *)myaddr, sizeof(*myaddr))) < 0) - std::perror("cannot bind"); + myaddr.sin_family = AF_INET; + myaddr.sin_addr.s_addr = htonl(INADDR_ANY); //IP address + myaddr.sin_port = htons(8934); //socket + if((bind(udp_socket, (struct sockaddr*)&myaddr, sizeof(myaddr))) < 0) + perror("cannot bind"); } //ic bool UDP_Client::validate(string data) { diff --git a/src/Source Files/UDP/UDP_Server.cpp b/src/Source Files/UDP/UDP_Server.cpp index a59fef3..29cb81e 100644 --- a/src/Source Files/UDP/UDP_Server.cpp +++ b/src/Source Files/UDP/UDP_Server.cpp @@ -7,7 +7,7 @@ UDP_Server::UDP_Server(string address, int port) { buffer = new Buffer("UDP Receive Buffer", BUFFER_SIZE); PORT = port; - char temp[] = address; + char* temp = (char*)address.c_str(); memcpy(recipientAddr, temp, sizeof(recipientAddr)); start(); } @@ -15,19 +15,19 @@ UDP_Server::UDP_Server(string address, int port) { void UDP_Server::start() { struct sockaddr_in myaddr; // our address + cout << "Test" << endl; if ((server_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("cannot create socket\n"); - return 0; + return; } memset((char *)&myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); myaddr.sin_port = htons(PORT); - if (bind(server_socket, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { perror("bind failed"); - return 0; + return; } } @@ -38,15 +38,15 @@ void UDP_Server::receive() { recvlen = recvfrom(server_socket, buf, BUFFER_SIZE, 0, (struct sockaddr *)&remaddr, &addrlen); if (recvlen > 0) { buf[recvlen] = 0; - } + string data = reinterpret_cast(buf); + if (!validate(data)) + return; - string data(buff); - if (!validate(data)) - return; + Datum temp(data, "target", "UDP Protocol"); + timestamp(&temp, true); + buffer.push(temp); + } - Datum temp(data, "target", "UDP Protocol"); - timestamp(&temp, true); - buffer.push(temp); return; }