Hung Nguyen
/
CIS541-HW-MQTT
This is the codebase provided for CIS541 - Homework MQTT.
MQTTNetwork.h@1:91e33a7fe0b5, 2017-10-07 (annotated)
- Committer:
- hungnguyenm
- Date:
- Sat Oct 07 14:52:56 2017 +0000
- Revision:
- 1:91e33a7fe0b5
Added HW6 codebase
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hungnguyenm | 1:91e33a7fe0b5 | 1 | #ifndef _MQTTNETWORK_H_ |
hungnguyenm | 1:91e33a7fe0b5 | 2 | #define _MQTTNETWORK_H_ |
hungnguyenm | 1:91e33a7fe0b5 | 3 | |
hungnguyenm | 1:91e33a7fe0b5 | 4 | #include "NetworkInterface.h" |
hungnguyenm | 1:91e33a7fe0b5 | 5 | |
hungnguyenm | 1:91e33a7fe0b5 | 6 | class MQTTNetwork { |
hungnguyenm | 1:91e33a7fe0b5 | 7 | public: |
hungnguyenm | 1:91e33a7fe0b5 | 8 | MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) { |
hungnguyenm | 1:91e33a7fe0b5 | 9 | socket = new TCPSocket(); |
hungnguyenm | 1:91e33a7fe0b5 | 10 | } |
hungnguyenm | 1:91e33a7fe0b5 | 11 | |
hungnguyenm | 1:91e33a7fe0b5 | 12 | ~MQTTNetwork() { |
hungnguyenm | 1:91e33a7fe0b5 | 13 | delete socket; |
hungnguyenm | 1:91e33a7fe0b5 | 14 | } |
hungnguyenm | 1:91e33a7fe0b5 | 15 | |
hungnguyenm | 1:91e33a7fe0b5 | 16 | int read(unsigned char* buffer, int len, int timeout) { |
hungnguyenm | 1:91e33a7fe0b5 | 17 | return socket->recv(buffer, len); |
hungnguyenm | 1:91e33a7fe0b5 | 18 | } |
hungnguyenm | 1:91e33a7fe0b5 | 19 | |
hungnguyenm | 1:91e33a7fe0b5 | 20 | int write(unsigned char* buffer, int len, int timeout) { |
hungnguyenm | 1:91e33a7fe0b5 | 21 | return socket->send(buffer, len); |
hungnguyenm | 1:91e33a7fe0b5 | 22 | } |
hungnguyenm | 1:91e33a7fe0b5 | 23 | |
hungnguyenm | 1:91e33a7fe0b5 | 24 | int connect(const char* hostname, int port) { |
hungnguyenm | 1:91e33a7fe0b5 | 25 | socket->open(network); |
hungnguyenm | 1:91e33a7fe0b5 | 26 | return socket->connect(hostname, port); |
hungnguyenm | 1:91e33a7fe0b5 | 27 | } |
hungnguyenm | 1:91e33a7fe0b5 | 28 | |
hungnguyenm | 1:91e33a7fe0b5 | 29 | int disconnect() { |
hungnguyenm | 1:91e33a7fe0b5 | 30 | return socket->close(); |
hungnguyenm | 1:91e33a7fe0b5 | 31 | } |
hungnguyenm | 1:91e33a7fe0b5 | 32 | |
hungnguyenm | 1:91e33a7fe0b5 | 33 | private: |
hungnguyenm | 1:91e33a7fe0b5 | 34 | NetworkInterface* network; |
hungnguyenm | 1:91e33a7fe0b5 | 35 | TCPSocket* socket; |
hungnguyenm | 1:91e33a7fe0b5 | 36 | }; |
hungnguyenm | 1:91e33a7fe0b5 | 37 | |
hungnguyenm | 1:91e33a7fe0b5 | 38 | #endif // _MQTTNETWORK_H_ |