Moving RPi folder.
[iotcloud.git] / version2 / src / others / RPi / IoTUDP.h
1 #ifndef _IOTUDP_H__
2 #define _IOTUDP_H__
3 #include <iostream>
4
5 #include "IoTDeviceAddress.h"
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 final
17 {
18         // IoTUDP class properties
19         private:
20                 //UDPSocket *socket;
21                 UDP socket;
22                 IPAddress strHostAddress;
23                 int iSrcPort;
24                 int iDstPort;
25                 bool didClose;
26                 int timeOut;
27
28         public:
29
30                 // Constructor
31                 IoTUDP(IoTDeviceAddress* iotDevAdd) {
32
33                         strHostAddress = iotDevAdd->getAddress();
34                         iSrcPort = iotDevAdd->getSourcePortNumber();
35                         iDstPort = iotDevAdd->getDestinationPortNumber();
36                         timeOut = 0;
37
38                         socket.begin(iSrcPort);
39                         didClose = false;
40                 }
41
42
43                 ~IoTUDP() {
44                 }
45
46
47                 IPAddress getHostAddress() {
48                         return strHostAddress;
49                 }
50
51
52                 int getSourcePort() {
53                         return iSrcPort;
54                 }
55
56
57                 int getDestinationPort() {
58                         return iDstPort;
59                 }
60
61
62                 void setTimeOut(int interval) {
63
64                         timeOut = interval;
65                 }
66
67
68                 // Send data packet
69                 void sendData(const char* buffer, int bufferLen) {
70
71                         unsigned short destinationPort = (unsigned short) iDstPort;
72                         IPAddress ipFromBytes(strHostAddress);
73                         socket.sendPacket(buffer, bufferLen, strHostAddress, destinationPort);
74                 }
75
76
77                 // Receive data packet
78                 int receiveData(char* buffer, int iMaxDataLength) {
79
80                         return socket.receivePacket(buffer, iMaxDataLength);                    
81                 }
82 };
83 #endif