Commit 8a0c7fe4b819b2dd9560585297531e4c7ca94cdb
1 parent
1e1caf35
Make IHttpHandler purely virtual
- add new class BaseHttpHandler - rename lin- and winHttpHandler to Lin- and WinHttpHandler - print error messages to std::cerr
Showing
10 changed files
with
338 additions
and
176 deletions
README.md
| ... | ... | @@ -15,14 +15,14 @@ A simple and easy to use library for Philips Hue Lights |
| 15 | 15 | |
| 16 | 16 | ## How to use |
| 17 | 17 | ### <a name="searchingBridges"></a>Searching for Bridges |
| 18 | -To start searching for a Hue Bridge you will need to choose an IHttpHandler and create one. The options are a "winHttpHandler" (for windows) or a "linHttpHandler" (for linux). | |
| 18 | +To start searching for a Hue Bridge you will need to choose an IHttpHandler and create one. The options are a "WinHttpHandler" (for windows) or a "LinHttpHandler" (for linux). | |
| 19 | 19 | Then create a HueFinder object with the handler. |
| 20 | 20 | The handler is needed, because it tells the finder which functions to use to communicate with a bridge or your local network. |
| 21 | 21 | After that you can call FindBridges(), which will return a vector containing the ip and mac address of all found Bridges. |
| 22 | 22 | If no Bridges were found the vector is empty, so make sure that in that case you provide an ip and mac address. |
| 23 | 23 | ```C++ |
| 24 | -// For windows use std::make_shared<winHttpHandler>(); | |
| 25 | -handler = std::make_shared<linHttpHandler>(); | |
| 24 | +// For windows use std::make_shared<WinHttpHandler>(); | |
| 25 | +handler = std::make_shared<LinHttpHandler>(); | |
| 26 | 26 | HueFinder finder(handler); |
| 27 | 27 | std::vector<HueFinder::HueIdentification> bridges = finder.FindBridges(); |
| 28 | 28 | if (bridges.empty()) |
| ... | ... | @@ -46,8 +46,8 @@ Hue bridge = finder.GetBridge(bridges[0]); |
| 46 | 46 | If you do not want to use the HueFinder or you already know the ip and username of your bridge you have the option to create your own Hue object. |
| 47 | 47 | Here you will need to provide the ip address, a username and an HttpHandler |
| 48 | 48 | ```C++ |
| 49 | -// For windows use std::make_shared<winHttpHandler>(); | |
| 50 | -handler = std::make_shared<linHttpHandler>(); | |
| 49 | +// For windows use std::make_shared<WinHttpHandler>(); | |
| 50 | +handler = std::make_shared<LinHttpHandler>(); | |
| 51 | 51 | Hue bridge("192.168.2.102", "<username>", handler); |
| 52 | 52 | ``` |
| 53 | 53 | ... | ... |
hueplusplus/linHttpHandler.cpp renamed to hueplusplus/LinHttpHandler.cpp
| 1 | 1 | /** |
| 2 | - \file linHttpHandler.cpp | |
| 2 | + \file LinHttpHandler.cpp | |
| 3 | 3 | Copyright Notice\n |
| 4 | 4 | Copyright (C) 2017 Jan Rogall - developer\n |
| 5 | 5 | Copyright (C) 2017 Moritz Wirger - developer\n |
| ... | ... | @@ -17,7 +17,7 @@ |
| 17 | 17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | 18 | **/ |
| 19 | 19 | |
| 20 | -#include "include/linHttpHandler.h" | |
| 20 | +#include "include/LinHttpHandler.h" | |
| 21 | 21 | |
| 22 | 22 | #include <chrono> |
| 23 | 23 | #include <netinet/in.h> // struct sockaddr_in, struct sockaddr |
| ... | ... | @@ -38,7 +38,7 @@ class SocketCloser { |
| 38 | 38 | private: int s; |
| 39 | 39 | }; |
| 40 | 40 | |
| 41 | -std::string linHttpHandler::send(const std::string & msg, const std::string & adr, int port) const | |
| 41 | +std::string LinHttpHandler::send(const std::string & msg, const std::string & adr, int port) const | |
| 42 | 42 | { |
| 43 | 43 | // create socket |
| 44 | 44 | int socketFD = socket(AF_INET, SOCK_STREAM, 0); |
| ... | ... | @@ -46,8 +46,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 46 | 46 | SocketCloser closeMySocket(socketFD); |
| 47 | 47 | if (socketFD < 0) |
| 48 | 48 | { |
| 49 | - std::cerr << "linHttpHandler: Failed to open socket\n"; | |
| 50 | - throw(std::runtime_error("linHttpHandler: Failed to open socket")); | |
| 49 | + std::cerr << "LinHttpHandler: Failed to open socket\n"; | |
| 50 | + throw(std::runtime_error("LinHttpHandler: Failed to open socket")); | |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // lookup ip address |
| ... | ... | @@ -55,8 +55,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 55 | 55 | server = gethostbyname(adr.c_str()); |
| 56 | 56 | if (server == NULL) |
| 57 | 57 | { |
| 58 | - std::cerr << "linHttpHandler: Failed to find host with address " << adr << "\n"; | |
| 59 | - throw(std::runtime_error("linHttpHandler: Failed to find host")); | |
| 58 | + std::cerr << "LinHttpHandler: Failed to find host with address " << adr << "\n"; | |
| 59 | + throw(std::runtime_error("LinHttpHandler: Failed to find host")); | |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // fill in the structure |
| ... | ... | @@ -69,8 +69,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 69 | 69 | // connect the socket |
| 70 | 70 | if (connect(socketFD, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) |
| 71 | 71 | { |
| 72 | - std::cerr << "linHttpHandler: Failed to connect socket\n"; | |
| 73 | - throw(std::runtime_error("linHttpHandler: Failed to connect socket")); | |
| 72 | + std::cerr << "LinHttpHandler: Failed to connect socket\n"; | |
| 73 | + throw(std::runtime_error("LinHttpHandler: Failed to connect socket")); | |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // send the request |
| ... | ... | @@ -81,8 +81,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 81 | 81 | ssize_t bytes = write(socketFD, msg.c_str() + sent, total - sent); |
| 82 | 82 | if (bytes < 0) |
| 83 | 83 | { |
| 84 | - std::cerr << "linHttpHandler: Failed to write message to socket\n"; | |
| 85 | - throw(std::runtime_error("linHttpHandler: Failed to write message to socket")); | |
| 84 | + std::cerr << "LinHttpHandler: Failed to write message to socket\n"; | |
| 85 | + throw(std::runtime_error("LinHttpHandler: Failed to write message to socket")); | |
| 86 | 86 | } |
| 87 | 87 | if (bytes == 0) |
| 88 | 88 | { |
| ... | ... | @@ -104,8 +104,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 104 | 104 | ssize_t bytes = read(socketFD, buffer, 127); |
| 105 | 105 | if (bytes < 0) |
| 106 | 106 | { |
| 107 | - std::cerr << "linHttpHandler: Failed to read response from socket: " << errno << std::endl; | |
| 108 | - throw(std::runtime_error("linHttpHandler: Failed to read response from socket")); | |
| 107 | + std::cerr << "LinHttpHandler: Failed to read response from socket: " << errno << std::endl; | |
| 108 | + throw(std::runtime_error("LinHttpHandler: Failed to read response from socket")); | |
| 109 | 109 | } |
| 110 | 110 | if (bytes == 0) |
| 111 | 111 | { |
| ... | ... | @@ -120,14 +120,14 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 120 | 120 | |
| 121 | 121 | if (received == total) |
| 122 | 122 | { |
| 123 | - std::cerr << "linHttpHandler: Failed to store complete response from socket\n"; | |
| 124 | - throw(std::runtime_error("linHttpHandler: Failed to store complete response from socket")); | |
| 123 | + std::cerr << "LinHttpHandler: Failed to store complete response from socket\n"; | |
| 124 | + throw(std::runtime_error("LinHttpHandler: Failed to store complete response from socket")); | |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | return response; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | -std::vector<std::string> linHttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout) const | |
| 130 | +std::vector<std::string> LinHttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout) const | |
| 131 | 131 | { |
| 132 | 132 | hostent *server; // host information |
| 133 | 133 | sockaddr_in server_addr; // server address |
| ... | ... | @@ -141,8 +141,8 @@ std::vector<std::string> linHttpHandler::sendMulticast(const std::string & msg, |
| 141 | 141 | server = gethostbyname(adr.c_str()); |
| 142 | 142 | if (!server) |
| 143 | 143 | { |
| 144 | - std::cerr << "linHttpHandler: sendMulticast: Failed to obtain address of " << msg << "\n"; | |
| 145 | - throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to obtain address of host")); | |
| 144 | + std::cerr << "LinHttpHandler: sendMulticast: Failed to obtain address of " << msg << "\n"; | |
| 145 | + throw(std::runtime_error("LinHttpHandler: sendMulticast: Failed to obtain address of host")); | |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | // put the host's address into the server address structure |
| ... | ... | @@ -153,15 +153,15 @@ std::vector<std::string> linHttpHandler::sendMulticast(const std::string & msg, |
| 153 | 153 | SocketCloser closeMySendSocket(socketFD); |
| 154 | 154 | if (socketFD < 0) |
| 155 | 155 | { |
| 156 | - std::cerr << "linHttpHandler: sendMulticast: Failed to open socket\n"; | |
| 157 | - throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to open socket")); | |
| 156 | + std::cerr << "LinHttpHandler: sendMulticast: Failed to open socket\n"; | |
| 157 | + throw(std::runtime_error("LinHttpHandler: sendMulticast: Failed to open socket")); | |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // send a message to the server |
| 161 | 161 | if (sendto(socketFD, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) |
| 162 | 162 | { |
| 163 | - std::cerr << "linHttpHandler: sendMulticast: Failed to send message\n"; | |
| 164 | - throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to send message")); | |
| 163 | + std::cerr << "LinHttpHandler: sendMulticast: Failed to send message\n"; | |
| 164 | + throw(std::runtime_error("LinHttpHandler: sendMulticast: Failed to send message")); | |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | std::string response; |
| ... | ... | @@ -176,8 +176,8 @@ std::vector<std::string> linHttpHandler::sendMulticast(const std::string & msg, |
| 176 | 176 | { |
| 177 | 177 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
| 178 | 178 | { |
| 179 | - std::cerr << "linHttpHandler: sendMulticast: Failed to read response from socket\n"; | |
| 180 | - throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to read response from socket")); | |
| 179 | + std::cerr << "LinHttpHandler: sendMulticast: Failed to read response from socket\n"; | |
| 180 | + throw(std::runtime_error("LinHttpHandler: sendMulticast: Failed to read response from socket")); | |
| 181 | 181 | } |
| 182 | 182 | continue; |
| 183 | 183 | } | ... | ... |
hueplusplus/winHttpHandler.cpp renamed to hueplusplus/WinHttpHandler.cpp
100644 โ 100755
| 1 | 1 | /** |
| 2 | -\file winHttpHandler.cpp | |
| 2 | +\file WinHttpHandler.cpp | |
| 3 | 3 | Copyright Notice\n |
| 4 | 4 | Copyright (C) 2017 Jan Rogall - developer\n |
| 5 | 5 | Copyright (C) 2017 Moritz Wirger - developer\n |
| ... | ... | @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, |
| 17 | 17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | 18 | **/ |
| 19 | 19 | |
| 20 | -#include "include/winHttpHandler.h" | |
| 20 | +#include "include/WinHttpHandler.h" | |
| 21 | 21 | |
| 22 | 22 | #include <chrono> |
| 23 | 23 | #include <iostream> |
| ... | ... | @@ -27,23 +27,23 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 | 27 | |
| 28 | 28 | #pragma comment(lib, "Ws2_32.lib") |
| 29 | 29 | |
| 30 | -winHttpHandler::winHttpHandler() | |
| 30 | +WinHttpHandler::WinHttpHandler() | |
| 31 | 31 | { |
| 32 | 32 | // Initialize Winsock |
| 33 | 33 | int return_code = WSAStartup(MAKEWORD(2, 2), &wsaData); |
| 34 | 34 | if (return_code != 0) |
| 35 | 35 | { |
| 36 | 36 | std::cerr << "Failed to open socket: " << return_code << std::endl; |
| 37 | - throw(std::runtime_error("winHttpHandler: Failed to open socket")); | |
| 37 | + throw(std::runtime_error("WinHttpHandler: Failed to open socket")); | |
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | -winHttpHandler::~winHttpHandler() | |
| 41 | +WinHttpHandler::~WinHttpHandler() | |
| 42 | 42 | { |
| 43 | 43 | WSACleanup(); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | -std::string winHttpHandler::send(const std::string & msg, const std::string & adr, int port) const | |
| 46 | +std::string WinHttpHandler::send(const std::string & msg, const std::string & adr, int port) const | |
| 47 | 47 | { |
| 48 | 48 | struct addrinfo hints = {}; |
| 49 | 49 | hints.ai_family = AF_INET; |
| ... | ... | @@ -54,8 +54,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 54 | 54 | struct addrinfo *result = nullptr; |
| 55 | 55 | if (getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result) != 0) |
| 56 | 56 | { |
| 57 | - std::cerr << "winHttpHandler: getaddrinfo failed: " << WSAGetLastError() << std::endl; | |
| 58 | - throw(std::runtime_error("winHttpHandler: getaddrinfo failed")); | |
| 57 | + std::cerr << "WinHttpHandler: getaddrinfo failed: " << WSAGetLastError() << std::endl; | |
| 58 | + throw(std::runtime_error("WinHttpHandler: getaddrinfo failed")); | |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | // Attempt to connect to the first address returned by |
| ... | ... | @@ -68,8 +68,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 68 | 68 | if (connect_socket == INVALID_SOCKET) |
| 69 | 69 | { |
| 70 | 70 | freeaddrinfo(result); |
| 71 | - std::cerr << "winHttpHandler: Error at socket(): " << WSAGetLastError() << std::endl; | |
| 72 | - throw(std::runtime_error("winHttpHandler: Error at socket()")); | |
| 71 | + std::cerr << "WinHttpHandler: Error at socket(): " << WSAGetLastError() << std::endl; | |
| 72 | + throw(std::runtime_error("WinHttpHandler: Error at socket()")); | |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // Connect to server. |
| ... | ... | @@ -88,16 +88,16 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 88 | 88 | |
| 89 | 89 | if (connect_socket == INVALID_SOCKET) |
| 90 | 90 | { |
| 91 | - std::cerr << "winHttpHandler: Unable to connect to server!" << std::endl; | |
| 92 | - throw(std::runtime_error("winHttpHandler: Unable to connect to server!")); | |
| 91 | + std::cerr << "WinHttpHandler: Unable to connect to server!" << std::endl; | |
| 92 | + throw(std::runtime_error("WinHttpHandler: Unable to connect to server!")); | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // Send an initial buffer |
| 96 | 96 | if (::send(connect_socket, msg.c_str(), msg.size(), 0) == SOCKET_ERROR) |
| 97 | 97 | { |
| 98 | 98 | closesocket(connect_socket); |
| 99 | - std::cerr << "winHttpHandler: send failed: " << WSAGetLastError() << std::endl; | |
| 100 | - throw(std::runtime_error("winHttpHandler: send failed")); | |
| 99 | + std::cerr << "WinHttpHandler: send failed: " << WSAGetLastError() << std::endl; | |
| 100 | + throw(std::runtime_error("WinHttpHandler: send failed")); | |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | // shutdown the connection for sending since no more data will be sent |
| ... | ... | @@ -105,8 +105,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 105 | 105 | if (shutdown(connect_socket, SD_SEND) == SOCKET_ERROR) |
| 106 | 106 | { |
| 107 | 107 | closesocket(connect_socket); |
| 108 | - std::cerr << "winHttpHandler: shutdown failed: " << WSAGetLastError() << std::endl; | |
| 109 | - throw(std::runtime_error("winHttpHandler: shutdown failed")); | |
| 108 | + std::cerr << "WinHttpHandler: shutdown failed: " << WSAGetLastError() << std::endl; | |
| 109 | + throw(std::runtime_error("WinHttpHandler: shutdown failed")); | |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | const int recvbuflen = 128; |
| ... | ... | @@ -120,18 +120,18 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 120 | 120 | res = recv(connect_socket, recvbuf, recvbuflen, 0); |
| 121 | 121 | if (res > 0) |
| 122 | 122 | { |
| 123 | - //std::cout << "winHttpHandler: Bytes received: " << res << std::endl; | |
| 123 | + //std::cout << "WinHttpHandler: Bytes received: " << res << std::endl; | |
| 124 | 124 | response.append(recvbuf, res); |
| 125 | 125 | } |
| 126 | 126 | else if (res == 0) |
| 127 | 127 | { |
| 128 | - //std::cout << "winHttpHandler: Connection closed " << std::endl; | |
| 128 | + //std::cout << "WinHttpHandler: Connection closed " << std::endl; | |
| 129 | 129 | } |
| 130 | 130 | else |
| 131 | 131 | { |
| 132 | 132 | closesocket(connect_socket); |
| 133 | - std::cerr << "winHttpHandler: recv failed: " << WSAGetLastError() << std::endl; | |
| 134 | - throw(std::runtime_error("winHttpHandler: recv failed")); | |
| 133 | + std::cerr << "WinHttpHandler: recv failed: " << WSAGetLastError() << std::endl; | |
| 134 | + throw(std::runtime_error("WinHttpHandler: recv failed")); | |
| 135 | 135 | } |
| 136 | 136 | } while (res > 0); |
| 137 | 137 | closesocket(connect_socket); |
| ... | ... | @@ -139,7 +139,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 139 | 139 | return response; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | -std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout) const | |
| 142 | +std::vector<std::string> WinHttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout) const | |
| 143 | 143 | { |
| 144 | 144 | struct addrinfo hints = {}; |
| 145 | 145 | hints.ai_family = AF_INET; |
| ... | ... | @@ -150,8 +150,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 150 | 150 | struct addrinfo *result = nullptr; |
| 151 | 151 | if (getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result) != 0) |
| 152 | 152 | { |
| 153 | - std::cerr << "winHttpHandler: sendMulticast: getaddrinfo failed: " << WSAGetLastError() << std::endl; | |
| 154 | - throw(std::runtime_error("winHttpHandler: sendMulticast: getaddrinfo failed")); | |
| 153 | + std::cerr << "WinHttpHandler: sendMulticast: getaddrinfo failed: " << WSAGetLastError() << std::endl; | |
| 154 | + throw(std::runtime_error("WinHttpHandler: sendMulticast: getaddrinfo failed")); | |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | // Attempt to connect to the first address returned by |
| ... | ... | @@ -163,8 +163,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 163 | 163 | if ((connect_socket = socket(ptr->ai_family, ptr->ai_socktype, 0)) == INVALID_SOCKET) |
| 164 | 164 | { |
| 165 | 165 | freeaddrinfo(result); |
| 166 | - std::cerr << "winHttpHandler: sendMulticast: Error at socket(): " << WSAGetLastError() << std::endl; | |
| 167 | - throw(std::runtime_error("winHttpHandler: sendMulticast: Error at socket()")); | |
| 166 | + std::cerr << "WinHttpHandler: sendMulticast: Error at socket(): " << WSAGetLastError() << std::endl; | |
| 167 | + throw(std::runtime_error("WinHttpHandler: sendMulticast: Error at socket()")); | |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | // Fill out source socket's address information. |
| ... | ... | @@ -177,8 +177,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 177 | 177 | if (bind(connect_socket, (struct sockaddr FAR *) &source_sin, sizeof(source_sin)) == SOCKET_ERROR) |
| 178 | 178 | { |
| 179 | 179 | closesocket(connect_socket); |
| 180 | - std::cerr << "winHttpHandler: sendMulticast: Binding socket failed: " << WSAGetLastError() << std::endl; | |
| 181 | - throw(std::runtime_error("winHttpHandler: sendMulticast: Binding socket failed")); | |
| 180 | + std::cerr << "WinHttpHandler: sendMulticast: Binding socket failed: " << WSAGetLastError() << std::endl; | |
| 181 | + throw(std::runtime_error("WinHttpHandler: sendMulticast: Binding socket failed")); | |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | u_long sock_mode = 1; |
| ... | ... | @@ -192,8 +192,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 192 | 192 | if (setsockopt(connect_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char FAR *)&iOptVal, sizeof(int)) == SOCKET_ERROR) |
| 193 | 193 | { |
| 194 | 194 | closesocket(connect_socket); |
| 195 | - std::cerr << "winHttpHandler: sendMulticast: setsockopt failed: " << WSAGetLastError() << std::endl; | |
| 196 | - throw(std::runtime_error("winHttpHandler: sendMulticast: setsockopt failed")); | |
| 195 | + std::cerr << "WinHttpHandler: sendMulticast: setsockopt failed: " << WSAGetLastError() << std::endl; | |
| 196 | + throw(std::runtime_error("WinHttpHandler: sendMulticast: setsockopt failed")); | |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | // Fill out the desination socket's address information. |
| ... | ... | @@ -206,8 +206,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 206 | 206 | if (sendto(connect_socket, msg.c_str(), msg.size(), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin)) == SOCKET_ERROR) |
| 207 | 207 | { |
| 208 | 208 | closesocket(connect_socket); |
| 209 | - std::cerr << "winHttpHandler: sendMulticast: sendto failed: " << WSAGetLastError() << std::endl; | |
| 210 | - throw(std::runtime_error("winHttpHandler: sendMulticast: sendto failed")); | |
| 209 | + std::cerr << "WinHttpHandler: sendMulticast: sendto failed: " << WSAGetLastError() << std::endl; | |
| 210 | + throw(std::runtime_error("WinHttpHandler: sendMulticast: sendto failed")); | |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | // shutdown the connection for sending since no more data will be sent |
| ... | ... | @@ -215,8 +215,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 215 | 215 | if (shutdown(connect_socket, SD_SEND) == SOCKET_ERROR) |
| 216 | 216 | { |
| 217 | 217 | closesocket(connect_socket); |
| 218 | - std::cerr << "winHttpHandler: sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl; | |
| 219 | - throw(std::runtime_error("winHttpHandler: sendMulticast: shutdown failed")); | |
| 218 | + std::cerr << "WinHttpHandler: sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl; | |
| 219 | + throw(std::runtime_error("WinHttpHandler: sendMulticast: shutdown failed")); | |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | std::string response; |
| ... | ... | @@ -229,12 +229,12 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 229 | 229 | res = recv(connect_socket, recvbuf, recvbuflen, 0); |
| 230 | 230 | if (res > 0) |
| 231 | 231 | { |
| 232 | - //std::cout << "winHttpHandler: sendMulticast: Bytes received: " << res << std::endl; | |
| 232 | + //std::cout << "WinHttpHandler: sendMulticast: Bytes received: " << res << std::endl; | |
| 233 | 233 | response.append(recvbuf, res); |
| 234 | 234 | } |
| 235 | 235 | else if (res == 0) |
| 236 | 236 | { |
| 237 | - //std::cout << "winHttpHandler: sendMulticast: Connection closed " << std::endl; | |
| 237 | + //std::cout << "WinHttpHandler: sendMulticast: Connection closed " << std::endl; | |
| 238 | 238 | } |
| 239 | 239 | else |
| 240 | 240 | { | ... | ... |
hueplusplus/include/BaseHttpHandler.h
0 โ 100755
| 1 | +/** | |
| 2 | + \file BaseHttpHandler.h | |
| 3 | + Copyright Notice\n | |
| 4 | + Copyright (C) 2017 Jan Rogall - developer\n | |
| 5 | + Copyright (C) 2017 Moritz Wirger - developer\n | |
| 6 | + | |
| 7 | + This program is free software; you can redistribute it and/or modify | |
| 8 | + it under the terms of the GNU General Public License as published by | |
| 9 | + the Free Software Foundation; either version 3 of the License, or | |
| 10 | + (at your option) any later version. | |
| 11 | + This program is distributed in the hope that it will be useful, | |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | + GNU General Public License for more details. | |
| 15 | + You should have received a copy of the GNU General Public License | |
| 16 | + along with this program; if not, write to the Free Software Foundation, | |
| 17 | + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 18 | +**/ | |
| 19 | + | |
| 20 | +#ifndef _BASE_HTTPHANDLER_H | |
| 21 | +#define _BASE_HTTPHANDLER_H | |
| 22 | + | |
| 23 | +#include <iostream> | |
| 24 | +#include <memory> | |
| 25 | +#include <string> | |
| 26 | +#include <vector> | |
| 27 | + | |
| 28 | +#include "IHttpHandler.h" | |
| 29 | +#include "json/json.h" | |
| 30 | + | |
| 31 | +//! Base class for classes that handle http requests and multicast requests | |
| 32 | +class BaseHttpHandler : public IHttpHandler | |
| 33 | +{ | |
| 34 | +public: | |
| 35 | + //! \brief Virtual dtor | |
| 36 | + virtual ~BaseHttpHandler() = default; | |
| 37 | + | |
| 38 | + //! \brief Virtual function that should send a given message to a specified host and return the response. | |
| 39 | + //! | |
| 40 | + //! \param msg String that contains the message that should be sent to the specified address | |
| 41 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 42 | + //! \param port Optional integer that specifies the port to which the request should be sent to. Default is 80 | |
| 43 | + //! \return String containing the response of the host | |
| 44 | + virtual std::string send(const std::string &msg, const std::string &adr, int port=80) const = 0; | |
| 45 | + | |
| 46 | + //! \brief Virtual function that should given message to a specified host and return the body of the response. | |
| 47 | + //! | |
| 48 | + //! Note if no body is found a runtime error is thrown! | |
| 49 | + //! \param msg String that contains the message that should sent to the specified address | |
| 50 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 51 | + //! \param port Optional integer that specifies the port to which the request should be sent. Default is 80 | |
| 52 | + //! \return String containing the body of the response of the host | |
| 53 | + virtual std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80) const | |
| 54 | + { | |
| 55 | + std::string response = send(msg, adr, port); | |
| 56 | + size_t start = response.find("\r\n\r\n"); | |
| 57 | + if (start == std::string::npos) | |
| 58 | + { | |
| 59 | + std::cerr << "IHttpHandler: Failed to find body in response\n"; | |
| 60 | + throw(std::runtime_error("IHttpHandler: Failed to find body in response")); | |
| 61 | + } | |
| 62 | + response.erase(0, start + 4); | |
| 63 | + return response; | |
| 64 | + }; | |
| 65 | + | |
| 66 | + //! \brief Virtual function that should send a multicast request with a specified message. | |
| 67 | + //! | |
| 68 | + //! \param msg String that contains the request that should be sent to the specified address | |
| 69 | + //! \param adr Optional String that contains an ip or hostname in dotted decimal notation, default is "239.255.255.250" | |
| 70 | + //! \param port Optional integer that specifies the port to which the request should be sent. Default is 1900 | |
| 71 | + //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5 | |
| 72 | + //! \return Vector containing strings of each answer received | |
| 73 | + virtual std::vector<std::string> sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const = 0; | |
| 74 | + | |
| 75 | + //! \brief Virtual function that should send a HTTP request with the given method to the specified host and return the body of the response. | |
| 76 | + //! | |
| 77 | + //! Note body can also be left empty! | |
| 78 | + //! \param method String that contains the HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... | |
| 79 | + //! \param uri String that contains the uniform resource identifier | |
| 80 | + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... | |
| 81 | + //! \param body String that contains the data of the request | |
| 82 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 83 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 84 | + //! \return String containing the body of the response of the host | |
| 85 | + virtual std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 86 | + { | |
| 87 | + std::string request; | |
| 88 | + // Protocol reference: https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html | |
| 89 | + // Request-Line | |
| 90 | + request.append(method); // Method | |
| 91 | + request.append(" "); // Separation | |
| 92 | + request.append(uri); // Request-URI | |
| 93 | + request.append(" "); // Separation | |
| 94 | + request.append("HTTP/1.0"); // HTTP-Version | |
| 95 | + request.append("\r\n"); // Ending | |
| 96 | + // Entities | |
| 97 | + request.append("Content-Type:"); // entity-header | |
| 98 | + request.append(" "); // Separation | |
| 99 | + request.append(content_type); // media-type | |
| 100 | + request.append("\r\n"); // Entity ending | |
| 101 | + request.append("Content-Length:"); // entity-header | |
| 102 | + request.append(" "); // Separation | |
| 103 | + request.append(std::to_string(body.size())); // length | |
| 104 | + request.append("\r\n\r\n"); // Entity ending & Request-Line ending | |
| 105 | + request.append(body); // message-body | |
| 106 | + request.append("\r\n\r\n"); // Ending | |
| 107 | + | |
| 108 | + return sendGetHTTPBody(request.c_str(), adr, port); | |
| 109 | + }; | |
| 110 | + | |
| 111 | + //! \brief Virtual function that should send a HTTP GET request to the specified host and return the body of the response. | |
| 112 | + //! | |
| 113 | + //! Note body can also be left empty! | |
| 114 | + //! \param uri String that contains the uniform resource identifier | |
| 115 | + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... | |
| 116 | + //! \param body String that contains the data of the request | |
| 117 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 118 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 119 | + //! \return String containing the body of the response of the host | |
| 120 | + virtual std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 121 | + { | |
| 122 | + return sendHTTPRequest("GET", uri, content_type, body, adr, port); | |
| 123 | + }; | |
| 124 | + | |
| 125 | + //! \brief Virtual function that should send a HTTP POST request to the specified host and returns the body of the response. | |
| 126 | + //! | |
| 127 | + //! Note body can also be left empty! | |
| 128 | + //! \param uri String that contains the uniform resource identifier | |
| 129 | + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... | |
| 130 | + //! \param body String that contains the data of the request | |
| 131 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 132 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 133 | + //! \return String containing the body of the response of the host | |
| 134 | + virtual std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 135 | + { | |
| 136 | + return sendHTTPRequest("POST", uri, content_type, body, adr, port); | |
| 137 | + }; | |
| 138 | + | |
| 139 | + //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response. | |
| 140 | + //! | |
| 141 | + //! Note body can also be left empty! | |
| 142 | + //! \param uri String that contains the uniform resource identifier | |
| 143 | + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... | |
| 144 | + //! \param body String that contains the data of the request | |
| 145 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 146 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 147 | + //! \return String containing the body of the response of the host | |
| 148 | + virtual std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 149 | + { | |
| 150 | + return sendHTTPRequest("PUT", uri, content_type, body, adr, port); | |
| 151 | + }; | |
| 152 | + | |
| 153 | + //! \brief Virtual function that should send a HTTP DELETE request to the specified host and return the body of the response. | |
| 154 | + //! | |
| 155 | + //! Note body can also be left empty! | |
| 156 | + //! \param uri String that contains the uniform resource identifier | |
| 157 | + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... | |
| 158 | + //! \param body String that contains the data of the request | |
| 159 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 160 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 161 | + //! \return String containing the body of the response of the host | |
| 162 | + virtual std::string DELETEString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 163 | + { | |
| 164 | + return sendHTTPRequest("DELETE", uri, content_type, body, adr, port); | |
| 165 | + }; | |
| 166 | + | |
| 167 | + //! \brief Virtual function that should send a HTTP GET request to the specified host and return the body of the response. | |
| 168 | + //! | |
| 169 | + //! Note body can also be left empty! | |
| 170 | + //! \param uri String that contains the uniform resource identifier | |
| 171 | + //! \param body Json::Value that contains the data of the request | |
| 172 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 173 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 174 | + //! \return Json::Value containing the parsed body of the response of the host | |
| 175 | + virtual Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const | |
| 176 | + { | |
| 177 | + return strToJsonValue(GETString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 178 | + }; | |
| 179 | + | |
| 180 | + //! \brief Virtual function that should send a HTTP POST request to the specified host and return the body of the response. | |
| 181 | + //! | |
| 182 | + //! Note body can also be left empty! | |
| 183 | + //! \param uri String that contains the uniform resource identifier | |
| 184 | + //! \param body Json::Value that contains the data of the request | |
| 185 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 186 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 187 | + //! \return Json::Value containing the parsed body of the response of the host | |
| 188 | + virtual Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const | |
| 189 | + { | |
| 190 | + return strToJsonValue(POSTString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 191 | + } | |
| 192 | + | |
| 193 | + //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response. | |
| 194 | + //! | |
| 195 | + //! Note body can also be left empty! | |
| 196 | + //! \param uri String that contains the uniform resource identifier | |
| 197 | + //! \param body Json::Value that contains the data of the request | |
| 198 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 199 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 200 | + //! \return Json::Value containing the parsed body of the response of the host | |
| 201 | + virtual Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const | |
| 202 | + { | |
| 203 | + return strToJsonValue(PUTString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 204 | + }; | |
| 205 | + | |
| 206 | + //! \brief Virtual function that should send a HTTP DELETE request to the specified host and return the body of the response. | |
| 207 | + //! | |
| 208 | + //! Note body can also be left empty! | |
| 209 | + //! \param uri String that contains the uniform resource identifier | |
| 210 | + //! \param body Json::Value that contains the data of the request | |
| 211 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 212 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 213 | + //! \return Json::Value containing the parsed body of the response of the host | |
| 214 | + virtual Json::Value DELETEJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const | |
| 215 | + { | |
| 216 | + return strToJsonValue(DELETEString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 217 | + }; | |
| 218 | + | |
| 219 | +private: | |
| 220 | + | |
| 221 | + //! \brief Function that converts a given string to a Json::Value | |
| 222 | + //! | |
| 223 | + //! \param str String that gets converted | |
| 224 | + //! \return Json::Value containing parsed string | |
| 225 | + Json::Value strToJsonValue(std::string str) const | |
| 226 | + { | |
| 227 | + std::string error; | |
| 228 | + Json::Value result; | |
| 229 | + Json::CharReaderBuilder builder; | |
| 230 | + builder["collectComments"] = false; | |
| 231 | + std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); | |
| 232 | + if (!reader->parse(str.c_str(), str.c_str() + str.length(), &result, &error)) | |
| 233 | + { | |
| 234 | + std::cerr << "IHttpHandler: Error while parsing JSON in function strToJsonValue(): " << error << std::endl; | |
| 235 | + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function strToJsonValue()")); | |
| 236 | + } | |
| 237 | + return result; | |
| 238 | + } | |
| 239 | +}; | |
| 240 | + | |
| 241 | +#endif | ... | ... |
hueplusplus/include/IHttpHandler.h
| ... | ... | @@ -49,18 +49,7 @@ public: |
| 49 | 49 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 50 | 50 | //! \param port Optional integer that specifies the port to which the request should be sent. Default is 80 |
| 51 | 51 | //! \return String containing the body of the response of the host |
| 52 | - virtual std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80) const | |
| 53 | - { | |
| 54 | - std::string response = send(msg, adr, port); | |
| 55 | - size_t start = response.find("\r\n\r\n"); | |
| 56 | - if (start == std::string::npos) | |
| 57 | - { | |
| 58 | - std::cerr << "IHttpHandler: Failed to find body in response\n"; | |
| 59 | - throw(std::runtime_error("IHttpHandler: Failed to find body in response")); | |
| 60 | - } | |
| 61 | - response.erase(0, start + 4); | |
| 62 | - return response; | |
| 63 | - }; | |
| 52 | + virtual std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80) const = 0; | |
| 64 | 53 | |
| 65 | 54 | //! \brief Virtual function that should send a multicast request with a specified message. |
| 66 | 55 | //! |
| ... | ... | @@ -81,31 +70,7 @@ public: |
| 81 | 70 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 82 | 71 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 83 | 72 | //! \return String containing the body of the response of the host |
| 84 | - virtual std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 85 | - { | |
| 86 | - std::string request; | |
| 87 | - // Protocol reference: https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html | |
| 88 | - // Request-Line | |
| 89 | - request.append(method); // Method | |
| 90 | - request.append(" "); // Separation | |
| 91 | - request.append(uri); // Request-URI | |
| 92 | - request.append(" "); // Separation | |
| 93 | - request.append("HTTP/1.0"); // HTTP-Version | |
| 94 | - request.append("\r\n"); // Ending | |
| 95 | - // Entities | |
| 96 | - request.append("Content-Type:"); // entity-header | |
| 97 | - request.append(" "); // Separation | |
| 98 | - request.append(content_type); // media-type | |
| 99 | - request.append("\r\n"); // Entity ending | |
| 100 | - request.append("Content-Length:"); // entity-header | |
| 101 | - request.append(" "); // Separation | |
| 102 | - request.append(std::to_string(body.size())); // length | |
| 103 | - request.append("\r\n\r\n"); // Entity ending & Request-Line ending | |
| 104 | - request.append(body); // message-body | |
| 105 | - request.append("\r\n\r\n"); // Ending | |
| 106 | - | |
| 107 | - return sendGetHTTPBody(request.c_str(), adr, port); | |
| 108 | - }; | |
| 73 | + virtual std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; | |
| 109 | 74 | |
| 110 | 75 | //! \brief Virtual function that should send a HTTP GET request to the specified host and return the body of the response. |
| 111 | 76 | //! |
| ... | ... | @@ -116,10 +81,7 @@ public: |
| 116 | 81 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 117 | 82 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 118 | 83 | //! \return String containing the body of the response of the host |
| 119 | - virtual std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 120 | - { | |
| 121 | - return sendHTTPRequest("GET", uri, content_type, body, adr, port); | |
| 122 | - }; | |
| 84 | + virtual std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; | |
| 123 | 85 | |
| 124 | 86 | //! \brief Virtual function that should send a HTTP POST request to the specified host and returns the body of the response. |
| 125 | 87 | //! |
| ... | ... | @@ -130,10 +92,7 @@ public: |
| 130 | 92 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 131 | 93 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 132 | 94 | //! \return String containing the body of the response of the host |
| 133 | - virtual std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 134 | - { | |
| 135 | - return sendHTTPRequest("POST", uri, content_type, body, adr, port); | |
| 136 | - }; | |
| 95 | + virtual std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; | |
| 137 | 96 | |
| 138 | 97 | //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response. |
| 139 | 98 | //! |
| ... | ... | @@ -144,10 +103,7 @@ public: |
| 144 | 103 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 145 | 104 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 146 | 105 | //! \return String containing the body of the response of the host |
| 147 | - virtual std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 148 | - { | |
| 149 | - return sendHTTPRequest("PUT", uri, content_type, body, adr, port); | |
| 150 | - }; | |
| 106 | + virtual std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; | |
| 151 | 107 | |
| 152 | 108 | //! \brief Virtual function that should send a HTTP DELETE request to the specified host and return the body of the response. |
| 153 | 109 | //! |
| ... | ... | @@ -158,10 +114,7 @@ public: |
| 158 | 114 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 159 | 115 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 160 | 116 | //! \return String containing the body of the response of the host |
| 161 | - virtual std::string DELETEString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const | |
| 162 | - { | |
| 163 | - return sendHTTPRequest("DELETE", uri, content_type, body, adr, port); | |
| 164 | - }; | |
| 117 | + virtual std::string DELETEString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; | |
| 165 | 118 | |
| 166 | 119 | //! \brief Virtual function that should send a HTTP GET request to the specified host and return the body of the response. |
| 167 | 120 | //! |
| ... | ... | @@ -171,10 +124,7 @@ public: |
| 171 | 124 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 172 | 125 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 173 | 126 | //! \return Json::Value containing the parsed body of the response of the host |
| 174 | - virtual Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const | |
| 175 | - { | |
| 176 | - return strToJsonValue(GETString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 177 | - }; | |
| 127 | + virtual Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0; | |
| 178 | 128 | |
| 179 | 129 | //! \brief Virtual function that should send a HTTP POST request to the specified host and return the body of the response. |
| 180 | 130 | //! |
| ... | ... | @@ -184,10 +134,7 @@ public: |
| 184 | 134 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 185 | 135 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 186 | 136 | //! \return Json::Value containing the parsed body of the response of the host |
| 187 | - virtual Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const | |
| 188 | - { | |
| 189 | - return strToJsonValue(POSTString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 190 | - } | |
| 137 | + virtual Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0; | |
| 191 | 138 | |
| 192 | 139 | //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response. |
| 193 | 140 | //! |
| ... | ... | @@ -197,10 +144,7 @@ public: |
| 197 | 144 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 198 | 145 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 199 | 146 | //! \return Json::Value containing the parsed body of the response of the host |
| 200 | - virtual Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const | |
| 201 | - { | |
| 202 | - return strToJsonValue(PUTString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 203 | - }; | |
| 147 | + virtual Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0; | |
| 204 | 148 | |
| 205 | 149 | //! \brief Virtual function that should send a HTTP DELETE request to the specified host and return the body of the response. |
| 206 | 150 | //! |
| ... | ... | @@ -210,31 +154,8 @@ public: |
| 210 | 154 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 211 | 155 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 212 | 156 | //! \return Json::Value containing the parsed body of the response of the host |
| 213 | - virtual Json::Value DELETEJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const | |
| 214 | - { | |
| 215 | - return strToJsonValue(DELETEString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 216 | - }; | |
| 157 | + virtual Json::Value DELETEJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0; | |
| 217 | 158 | |
| 218 | -private: | |
| 219 | - | |
| 220 | - //! \brief Function that converts a given string to a Json::Value | |
| 221 | - //! | |
| 222 | - //! \param str String that gets converted | |
| 223 | - //! \return Json::Value containing parsed string | |
| 224 | - Json::Value strToJsonValue(std::string str) const | |
| 225 | - { | |
| 226 | - std::string error; | |
| 227 | - Json::Value result; | |
| 228 | - Json::CharReaderBuilder builder; | |
| 229 | - builder["collectComments"] = false; | |
| 230 | - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); | |
| 231 | - if (!reader->parse(str.c_str(), str.c_str() + str.length(), &result, &error)) | |
| 232 | - { | |
| 233 | - std::cout << "IHttpHandler: Error while parsing JSON in function strToJsonValue(): " << error << std::endl; | |
| 234 | - throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function strToJsonValue()")); | |
| 235 | - } | |
| 236 | - return result; | |
| 237 | - } | |
| 238 | 159 | }; |
| 239 | 160 | |
| 240 | 161 | #endif | ... | ... |
hueplusplus/include/linHttpHandler.h renamed to hueplusplus/include/LinHttpHandler.h
| 1 | 1 | /** |
| 2 | - \file linHttpHandler.h | |
| 2 | + \file LinHttpHandler.h | |
| 3 | 3 | Copyright Notice\n |
| 4 | 4 | Copyright (C) 2017 Jan Rogall - developer\n |
| 5 | 5 | Copyright (C) 2017 Moritz Wirger - developer\n |
| ... | ... | @@ -23,12 +23,12 @@ |
| 23 | 23 | #include <string> |
| 24 | 24 | #include <vector> |
| 25 | 25 | |
| 26 | -#include "IHttpHandler.h" | |
| 26 | +#include "BaseHttpHandler.h" | |
| 27 | 27 | |
| 28 | 28 | #include "json/json.h" |
| 29 | 29 | |
| 30 | 30 | //! Class to handle http requests and multicast requests on linux systems |
| 31 | -class linHttpHandler : public IHttpHandler | |
| 31 | +class LinHttpHandler : public BaseHttpHandler | |
| 32 | 32 | { |
| 33 | 33 | public: |
| 34 | 34 | //! \brief Function that sends a given message to the specified host and returns the response. | ... | ... |
hueplusplus/include/winHttpHandler.h renamed to hueplusplus/include/WinHttpHandler.h
100644 โ 100755
| 1 | 1 | /** |
| 2 | -\file winHttpHandler.h | |
| 2 | +\file WinHttpHandler.h | |
| 3 | 3 | Copyright Notice\n |
| 4 | 4 | Copyright (C) 2017 Jan Rogall - developer\n |
| 5 | 5 | Copyright (C) 2017 Moritz Wirger - developer\n |
| ... | ... | @@ -24,19 +24,19 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | 24 | #include <vector> |
| 25 | 25 | #include <winsock2.h> |
| 26 | 26 | |
| 27 | -#include "IHttpHandler.h" | |
| 27 | +#include "BaseHttpHandler.h" | |
| 28 | 28 | |
| 29 | 29 | #include "json/json.h" |
| 30 | 30 | |
| 31 | 31 | //! Class to handle http requests and multicast requests on windows systems |
| 32 | -class winHttpHandler : public IHttpHandler | |
| 32 | +class WinHttpHandler : public BaseHttpHandler | |
| 33 | 33 | { |
| 34 | 34 | public: |
| 35 | 35 | //! \brief Ctor needed for initializing wsaData |
| 36 | - winHttpHandler(); | |
| 36 | + WinHttpHandler(); | |
| 37 | 37 | |
| 38 | 38 | //! \brief Dtor needed for wsaData cleanup |
| 39 | - ~winHttpHandler(); | |
| 39 | + ~WinHttpHandler(); | |
| 40 | 40 | |
| 41 | 41 | //! \brief Function that sends a given message to the specified host and returns the response. |
| 42 | 42 | //! | ... | ... |
hueplusplus/test/CMakeLists.txt
| ... | ... | @@ -34,11 +34,11 @@ endif() |
| 34 | 34 | |
| 35 | 35 | # define all test sources |
| 36 | 36 | set(TEST_SOURCES |
| 37 | + ${CMAKE_CURRENT_SOURCE_DIR}/test_BaseHttpHandler.cpp | |
| 37 | 38 | ${CMAKE_CURRENT_SOURCE_DIR}/test_ExtendedColorHueStrategy.cpp |
| 38 | 39 | ${CMAKE_CURRENT_SOURCE_DIR}/test_ExtendedColorTemperatureStrategy.cpp |
| 39 | 40 | ${CMAKE_CURRENT_SOURCE_DIR}/test_Hue.cpp |
| 40 | 41 | ${CMAKE_CURRENT_SOURCE_DIR}/test_HueLight.cpp |
| 41 | - ${CMAKE_CURRENT_SOURCE_DIR}/test_IHttpHandler.cpp | |
| 42 | 42 | ${CMAKE_CURRENT_SOURCE_DIR}/test_Main.cpp |
| 43 | 43 | ${CMAKE_CURRENT_SOURCE_DIR}/test_SimpleBrightnessStrategy.cpp |
| 44 | 44 | ${CMAKE_CURRENT_SOURCE_DIR}/test_SimpleColorHueStrategy.cpp | ... | ... |
hueplusplus/test/mocks/mock_linHttpHandler.h renamed to hueplusplus/test/mocks/mock_LinHttpHandler.h
| 1 | 1 | /** |
| 2 | - \file mock_linHttpHandler.h | |
| 2 | + \file mock_LinHttpHandler.h | |
| 3 | 3 | Copyright Notice\n |
| 4 | 4 | Copyright (C) 2017 Jan Rogall - developer\n |
| 5 | 5 | Copyright (C) 2017 Moritz Wirger - developer\n |
| ... | ... | @@ -25,11 +25,11 @@ |
| 25 | 25 | |
| 26 | 26 | #include <gmock/gmock.h> |
| 27 | 27 | |
| 28 | -#include "../hueplusplus/include/linHttpHandler.h" | |
| 28 | +#include "../hueplusplus/include/LinHttpHandler.h" | |
| 29 | 29 | #include "../hueplusplus/include/json/json.h" |
| 30 | 30 | |
| 31 | 31 | //! Mock Class |
| 32 | -class MockLinHttpHandler : public linHttpHandler | |
| 32 | +class MockLinHttpHandler : public LinHttpHandler | |
| 33 | 33 | { |
| 34 | 34 | public: |
| 35 | 35 | MOCK_CONST_METHOD3( send, std::string(const std::string &msg, const std::string &adr, int port) ); | ... | ... |
hueplusplus/test/test_IHttpHandler.cpp renamed to hueplusplus/test/test_BaseHttpHandler.cpp
| ... | ... | @@ -2,13 +2,13 @@ |
| 2 | 2 | #include <gmock/gmock.h> |
| 3 | 3 | |
| 4 | 4 | #include "../include/json/json.h" |
| 5 | -#include "mocks/mock_linHttpHandler.h" | |
| 5 | +#include "mocks/mock_LinHttpHandler.h" | |
| 6 | 6 | #include "testhelper.h" |
| 7 | 7 | |
| 8 | 8 | #include <memory> |
| 9 | 9 | #include <string> |
| 10 | 10 | |
| 11 | -TEST(IHttpHandler, sendGetHTTPBody) | |
| 11 | +TEST(BaseHttpHandler, sendGetHTTPBody) | |
| 12 | 12 | { |
| 13 | 13 | using namespace ::testing; |
| 14 | 14 | MockLinHttpHandler handler; |
| ... | ... | @@ -22,7 +22,7 @@ TEST(IHttpHandler, sendGetHTTPBody) |
| 22 | 22 | EXPECT_EQ("testreply", handler.sendGetHTTPBody("testmsg", "192.168.2.1", 90)); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | -TEST(IHttpHandler, sendHTTPRequest) | |
| 25 | +TEST(BaseHttpHandler, sendHTTPRequest) | |
| 26 | 26 | { |
| 27 | 27 | using namespace ::testing; |
| 28 | 28 | MockLinHttpHandler handler; |
| ... | ... | @@ -36,7 +36,7 @@ TEST(IHttpHandler, sendHTTPRequest) |
| 36 | 36 | EXPECT_EQ("testreply", handler.sendHTTPRequest("GET", "UrI", "text/html", "body", "192.168.2.1", 90)); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | -TEST(IHttpHandler, GETString) | |
| 39 | +TEST(BaseHttpHandler, GETString) | |
| 40 | 40 | { |
| 41 | 41 | using namespace ::testing; |
| 42 | 42 | MockLinHttpHandler handler; |
| ... | ... | @@ -50,7 +50,7 @@ TEST(IHttpHandler, GETString) |
| 50 | 50 | EXPECT_EQ("testreply", handler.GETString("UrI", "text/html", "body", "192.168.2.1", 90)); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | -TEST(IHttpHandler, POSTString) | |
| 53 | +TEST(BaseHttpHandler, POSTString) | |
| 54 | 54 | { |
| 55 | 55 | using namespace ::testing; |
| 56 | 56 | MockLinHttpHandler handler; |
| ... | ... | @@ -64,7 +64,7 @@ TEST(IHttpHandler, POSTString) |
| 64 | 64 | EXPECT_EQ("testreply", handler.POSTString("UrI", "text/html", "body", "192.168.2.1", 90)); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | -TEST(IHttpHandler, PUTString) | |
| 67 | +TEST(BaseHttpHandler, PUTString) | |
| 68 | 68 | { |
| 69 | 69 | using namespace ::testing; |
| 70 | 70 | MockLinHttpHandler handler; |
| ... | ... | @@ -78,7 +78,7 @@ TEST(IHttpHandler, PUTString) |
| 78 | 78 | EXPECT_EQ("testreply", handler.PUTString("UrI", "text/html", "body", "192.168.2.1", 90)); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | -TEST(IHttpHandler, DELETEString) | |
| 81 | +TEST(BaseHttpHandler, DELETEString) | |
| 82 | 82 | { |
| 83 | 83 | using namespace ::testing; |
| 84 | 84 | MockLinHttpHandler handler; |
| ... | ... | @@ -92,7 +92,7 @@ TEST(IHttpHandler, DELETEString) |
| 92 | 92 | EXPECT_EQ("testreply", handler.DELETEString("UrI", "text/html", "body", "192.168.2.1", 90)); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | -TEST(IHttpHandler, GETJson) | |
| 95 | +TEST(BaseHttpHandler, GETJson) | |
| 96 | 96 | { |
| 97 | 97 | using namespace ::testing; |
| 98 | 98 | MockLinHttpHandler handler; |
| ... | ... | @@ -118,7 +118,7 @@ TEST(IHttpHandler, GETJson) |
| 118 | 118 | EXPECT_EQ(expected, handler.GETJson("UrI", testval, "192.168.2.1", 90)); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | -TEST(IHttpHandler, POSTJson) | |
| 121 | +TEST(BaseHttpHandler, POSTJson) | |
| 122 | 122 | { |
| 123 | 123 | using namespace ::testing; |
| 124 | 124 | MockLinHttpHandler handler; |
| ... | ... | @@ -144,7 +144,7 @@ TEST(IHttpHandler, POSTJson) |
| 144 | 144 | EXPECT_EQ(expected, handler.POSTJson("UrI", testval, "192.168.2.1", 90)); |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | -TEST(IHttpHandler, PUTJson) | |
| 147 | +TEST(BaseHttpHandler, PUTJson) | |
| 148 | 148 | { |
| 149 | 149 | using namespace ::testing; |
| 150 | 150 | MockLinHttpHandler handler; |
| ... | ... | @@ -170,7 +170,7 @@ TEST(IHttpHandler, PUTJson) |
| 170 | 170 | EXPECT_EQ(expected, handler.PUTJson("UrI", testval, "192.168.2.1", 90)); |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | -TEST(IHttpHandler, DELETEJson) | |
| 173 | +TEST(BaseHttpHandler, DELETEJson) | |
| 174 | 174 | { |
| 175 | 175 | using namespace ::testing; |
| 176 | 176 | MockLinHttpHandler handler; | ... | ... |