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