diff --git a/src/Header Files/AsynchHandler.h b/src/Header Files/AsynchHandler.h new file mode 100644 index 0000000..2d599fb --- /dev/null +++ b/src/Header Files/AsynchHandler.h @@ -0,0 +1,55 @@ +#ifndef __INTERCHANGE_ASYNCH_HANDLER_H +#define __INTERCHANGE_ASYNCH_HANDLER_H + +#include "Handler.h" + +#include +#include +#include +#include "Protocol.h" +#include "Daemon.h" +#include "IPAddr.h" + +#include +#include + +/* +* @author Marcus Plutowski +*/ + +class AsynchHandler : public Handler{ +friend class Daemon; +friend class Protocol; +public: + Handler(IPAddr targetAddress, Timer* timer); + + void addDaemon(Daemon *newDaemon); + void removeDaemon(std::string name); + + void addProtocol(Protocol *newProtocol); + void removeProtocol(std::string name); + + Daemon* getDaemon(std::string name); + Protocol* getProtocol(std::string name); + +private: + void stageData(Datum data); + void retData(Datum data); + + map Protocols; + map Daemons; + + IPAddr target; + + map> recieveBuffer; + map> sendBuffer; + + Timer* ticker; + pthread_t clock; + + void regulateHandler(); + void tickDaemons(); + void tickProtocols(); +}; + +#endif //__INTERCHANGE_ASYNCH_HANDLER_H diff --git a/src/Header Files/Buffer.h b/src/Header Files/Buffer.h new file mode 100644 index 0000000..52d5e3a --- /dev/null +++ b/src/Header Files/Buffer.h @@ -0,0 +1,28 @@ +#ifndef __INTERCHANGE_BUFFER_H +#define __INTERCHANGE_BUFFER_H + +#include +#include + +template +class Buffer{ +public: + Buffer(std::string identifier, unsigned short initSize); + + std::string getIdentifier(); + void setIdentifier(std::string identifier); + + void push(T newData); + T pop(); + T get(); + + void resize(unsigned short newSize); + unsigned short getSize(); + +private: + std::string identifier; + std::queue data; + unsigned short size; +}; + +#endif //__INTERCHANGE_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 95% rename from src/Datum.h rename to src/Header Files/Datum.h index ed00ff1..91b9845 100644 --- a/src/Datum.h +++ b/src/Header Files/Datum.h @@ -5,6 +5,7 @@ #include "Timer.h" #include + class Datum { public: Datum(); @@ -15,6 +16,7 @@ class Datum { void setData(std::string newData); std::string getData(); + std::string getSendable(); std::string getTarget(); 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/Header Files/Protocol.h b/src/Header Files/Protocol.h new file mode 100644 index 0000000..8213698 --- /dev/null +++ b/src/Header Files/Protocol.h @@ -0,0 +1,26 @@ +#ifndef _INTERCHANGE_PROTOCOL_H +#define _INTERCHANGE_PROTOCOL_H + +#include "Handler.h" +#include +#include + +/* +* @author Marcus Plutowski, Arushi Rai +*/ + +class Protocol{ + +public: + Protocol(int port); + std::string getName(); +protected: + 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/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/UDP_Client.h b/src/Header Files/UDP/UDP_Client.h new file mode 100644 index 0000000..44b3dc0 --- /dev/null +++ b/src/Header Files/UDP/UDP_Client.h @@ -0,0 +1,44 @@ +#ifndef __INTERCHANGE_UDP_CLIENT_H +#define __INTERCHANGE_UDP_CLIENT_H + +#include +#include +#include +#include +#include //used for fprintf +#include + +#include "..\Buffer.h" +#include "..\Datum.h" + +#define BUFFER_SIZE 64; //needs to be changed + +using namespace std; + +/* +* @author Arushi Rai +*/ + +class UDP_Client { +private: + char recipientAddr[32]; + Buffer buffer; + + int udp_socket; + int PORT; + struct sockaddr_in servaddr; + struct sockaddr_in myaddr; + + void setup(); + void bindSocket(); + void timestamp(Datum* data, bool add); + bool validate(string data); //validates the buffer + Datum empty(); //empties buffer + +public: + UDP_Client(string address, int port); + 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..19519a7 --- /dev/null +++ b/src/Header Files/UDP/UDP_Server.h @@ -0,0 +1,42 @@ +#ifndef __INTERCHANGE_UDP_SERVER_H +#define __INTERCHANGE_UDP_SERVER_H + +#include +#include +#include +#include +#include //used for fprintf +#include + +#include "..\Buffer.h" +#include "..\Datum.h" + +#define BUFFER_SIZE 64; + +using namespace std; + +/* +* @author Arushi Rai +*/ + +class UDP_Server { +private: + char recipientAddr[32]; + Buffer buffer; + + int server_socket; + int PORT; + + void start(); + void timestamp(Datum* data, bool add); + bool validate(string data); //validates the buffer + struct sockaddr_in remaddr; // remote address + socklen_t addrlen; //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 new file mode 100644 index 0000000..cea492f --- /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(int port); + std::string getName(); + void start(); //will start thread to run client and server simultaniously + std::string receive(); + void send(string message); //might not need this function +}; + +#endif \ No newline at end of file diff --git a/src/Protocol.h b/src/Protocol.h deleted file mode 100644 index f412c43..0000000 --- a/src/Protocol.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef _INTERCHANGE_PROTOCOL_H -#define _INTERCHANGE_PROTOCOL_H - -#include "Handler.h" -#include -#include - -/* -* @author Marcus Plutowski -*/ - -class Handler; - -class Protocol { - -public: - Protocol(std::string name, void (*retData)(std::string), int port); - - virtual void setName(std::string newName) = 0; - virtual std::string getName() = 0; - -protected: - std::string name; - int targetPort; - - void pullData(); -}; - -#endif diff --git a/src/Source Files/Buffer.cpp b/src/Source Files/Buffer.cpp new file mode 100644 index 0000000..8c9d507 --- /dev/null +++ b/src/Source Files/Buffer.cpp @@ -0,0 +1,44 @@ +#include "..\\Header Files\Buffer.h" + +Buffer::Buffer(std::int identifer, unsigned short initSize){ + this->identifier = identifier; + size = initSize; + +} + +std::string Buffer::getIdentifier(){ + return identifier; +} +void Buffer::setIdentifier(std::string identifier){ + this->identifier = identifier; +} + +void Buffer::push(T newData){ + if(size == 0){return;} + + while(data.getSize() == size){ + this->pop(); + } + data.push(newData); +} +T Buffer::pop(){ + if(!data.empty()){ + T buff; + buff = data.front() + data.pop(); + return buff; + } +} +T Buffer::get(){ + if(!data.empty()){ + return data.front(); + } +} + +void Buffer::resize(unsigned short newSize){ + data.resize(newSize); +} + +unsigned short Buffer::getSize(){ + return size; +} diff --git a/src/Daemon.cpp b/src/Source Files/Daemon.cpp similarity index 95% rename from src/Daemon.cpp rename to src/Source Files/Daemon.cpp index e4d4375..e0acc24 100644 --- a/src/Daemon.cpp +++ b/src/Source Files/Daemon.cpp @@ -1,3 +1,5 @@ +#include "..\\Header Files\Daemon.h" + #include "Daemon.h" #include "Datum.h" #include "Handler.h" diff --git a/src/Datum.cpp b/src/Source Files/Datum.cpp similarity index 54% rename from src/Datum.cpp rename to src/Source Files/Datum.cpp index 0329baa..4d4e6e6 100644 --- a/src/Datum.cpp +++ b/src/Source Files/Datum.cpp @@ -1,5 +1,8 @@ -#include "Datum.h" +#include "..\\Header Files\Datum.h" +/* +* @author Marcus Plutowski, Arushi Rai +*/ Datum::Datum() { // Use null protocol rather than this or check for empty protocol @@ -8,23 +11,31 @@ Datum::Datum() protocolTransited = ""; } -Datum::Datum(std::string initData, std::string target, std::string protocol) -{ - data = initData; - targetName = target; - protocolTransited = protocol; -} +Datum::Datum(std::string initData, std::string target, std::string protocol){ + data = initData; + targetName = target; + protocolTransited = protocol; std::string Datum::getTarget() { return targetName; } void Datum::timeStamp(Timer* timer) { timestamps.push_back(timer->getTime()); } Timer::milliseconds Datum::getTimeStamp(int num) { return timestamps[num]; } +std::string Datum::getSendable(){ + std::string sendable; + sendable = protocolTransited; + sendable += "; "; //seperation characters + sendable += data; + + if (protocolTransited[0] == 'T') { + //enter TCP data string format + } + + return sendable; +} + void Datum::setData(std::string newData) { data = newData; } std::string Datum::getData() { return data; } -void Datum::setProtocol(std::string newProtocol) -{ - protocolTransited = newProtocol; -} +void Datum::setProtocol(std::string newProtocol) { protocolTransited = newProtocol; } std::string Datum::getProtocol() { return protocolTransited; } 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 5e0e89f..2a00b34 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/Protocol.cpp b/src/Source Files/Protocol.cpp new file mode 100644 index 0000000..f6ecd2f --- /dev/null +++ b/src/Source Files/Protocol.cpp @@ -0,0 +1,14 @@ +#include "..\\Header Files\Protocol.h" + +/* +* @author Arushi Rai +*/ + +Protocol ::Protocol(int port) { + name = " "; + targetPort = port; +} + +std::string Protocol::getName() { + return name; +} diff --git a/src/Source Files/UDP/UDP_Client.cpp b/src/Source Files/UDP/UDP_Client.cpp new file mode 100644 index 0000000..fe1d8c1 --- /dev/null +++ b/src/Source Files/UDP/UDP_Client.cpp @@ -0,0 +1,89 @@ +#include "..\\Header Files\UDP_Client.h" + +/* +* @author Arushi Rai +*/ + +UDP_Client::UDP_Client(string address, int port) { + buffer = new Buffer("UDP Send Buffer", BUFFER_SIZE); + PORT = port; + char* temp = (char*)address.c_str(); + memcpy(recipientAddr, temp, sizeof(recipientAddr)); + setup(); +} + +//ic +void UDP_Client::setup() { + //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; + + 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, hp->h_addr_list[0], hp->h_length); +} + +void UDP_Client::addToSendBuffer(string message) { + if (!validate(message)) //if the data fails the validation, then don't send + return; + Datum temp = Datum(message, "std::string target", "UDP Protocol"); + timestamp(&temp, true); + buffer.push(temp); +} + +void UDP_Client::send() { + Datum data; + data = empty(); + char* message = (char*) data.getSendable().c_str(); + if (sendto(udp_socket, message, strlen(message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + perror("sendto failed"); + return; +} + +void UDP_Client::bindSocket (int s, struct sockaddr_in* myaddr) { + memset((char *)&myaddr, 0, sizeof(myaddr)); + + 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) { + bool validation = true; //change default to false after receiving data format + + return validation; +} + +//ic +Datum UDP_Client::empty() { + Datum data = buffer.pop(); + timestamp(&data, false); + + return data; +} + +void UDP_Client::timestamp(Datum* 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 new file mode 100644 index 0000000..29cb81e --- /dev/null +++ b/src/Source Files/UDP/UDP_Server.cpp @@ -0,0 +1,79 @@ +#include "..\\Header Files\UDP_Server.h" + +/* +* @author Arushi Rai +*/ + +UDP_Server::UDP_Server(string address, int port) { + buffer = new Buffer("UDP Receive Buffer", BUFFER_SIZE); + PORT = port; + char* temp = (char*)address.c_str(); + memcpy(recipientAddr, temp, sizeof(recipientAddr)); + start(); +} + +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; + } + + 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; + } +} + +void UDP_Server::receive() { + unsigned char buf[BUFFER_SIZE]; //temp receive buffer + int recvlen; //# bytes received + + 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; + + 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 + + return validation; +} + +string UDP_Server::empty() { + Datum data = buffer.pop(); + timestamp(&data, false); + + return data; +} + +//ic +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; */ +} diff --git a/src/Source Files/UDP_Protocol.cpp b/src/Source Files/UDP_Protocol.cpp new file mode 100644 index 0000000..79825a8 --- /dev/null +++ b/src/Source Files/UDP_Protocol.cpp @@ -0,0 +1,27 @@ +#include "..\\Header Files\UDP_Protocol" + +/* +* @author Arushi Rai +*/ + +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(address.getIPString(), port); +} + +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.addToSendBuffer(message); +} \ No newline at end of file