d712d4b3e6e5494b67d846eaf3fbec444bde128a
[iot2.git] / iotjava / iotruntime / cpp / IoTUDP.hpp
1 #ifndef _IOTUDP_HPP__
2 #define _IOTUDP_HPP__
3 #include <iostream>
4
5 #include "IoTDeviceAddress.hpp"
6
7 using namespace std;
8
9 // IoTUDP class for iotruntime
10 // Implemented based on IoTUDP.java that is used to wrap communication socket for UDP
11 //
12 // @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
13 // @version     1.0
14 // @since       2017-01-09
15
16 class IoTUDP
17 {
18         // IoTUDP class properties
19         private:
20                 UDPSocket *socket;
21                 string strHostAddress;
22                 int iSrcPort;
23                 int iDstPort;
24                 bool didClose;
25                 int timeOut;
26
27         public:
28
29                 // Constructor
30                 IoTUDP(IoTDeviceAddress* iotDevAdd) {
31
32                         strHostAddress = iotDevAdd->getAddress();
33                         iSrcPort = iotDevAdd->getSourcePortNumber();
34                         iDstPort = iotDevAdd->getDestinationPortNumber();
35                         timeOut = 0;
36
37                         socket = new UDPSocket(iSrcPort);
38                         if (socket == NULL) {
39                                 perror("IoTUDP: UDP socket isn't initialized!");
40                         }
41                         didClose = false;
42                 }
43
44
45                 ~IoTUDP() {
46                         // Clean up
47                         if (socket != NULL) {
48                 
49                                 delete socket;
50                                 socket = NULL;          
51                         }
52                 }
53
54
55                 string getHostAddress() {
56                         return strHostAddress;
57                 }
58
59
60                 int getSourcePort() {
61                         return iSrcPort;
62                 }
63
64
65                 int getDestinationPort() {
66                         return iDstPort;
67                 }
68
69
70                 void setTimeOut(int interval) {
71
72                         timeOut = interval;
73                 }
74
75
76                 // Send data packet
77                 void sendData(const void* buffer, int bufferLen) {
78                         unsigned short destinationPort = (unsigned short) iDstPort;
79                         socket->sendTo(buffer, bufferLen, strHostAddress, destinationPort);
80                 }
81
82
83                 // Receive data packet
84                 int receiveData(void* buffer, int iMaxDataLength) {
85                         unsigned short destinationPort = (unsigned short) iDstPort;
86                         //return socket->recvFrom(buffer, iMaxDataLength, strHostAddress, destinationPort);
87                         return socket->recvFrom(buffer, iMaxDataLength, strHostAddress, destinationPort, timeOut);
88                 }
89 };
90 #endif