Commit 9d85cfbf12347d9165a4a99a53bbe30adfc1c694
Committed by
Moritz Wirger
1 parent
9c0dada5
Refine debug messages in all HttpHandlers
Showing
3 changed files
with
61 additions
and
61 deletions
hueplusplus/include/IHttpHandler.h
| ... | ... | @@ -56,8 +56,8 @@ public: |
| 56 | 56 | size_t start = response.find("\r\n\r\n"); |
| 57 | 57 | if (start == std::string::npos) |
| 58 | 58 | { |
| 59 | - std::cerr << "Failed to find body in response\n"; | |
| 60 | - throw(std::runtime_error("Failed to find body in response")); | |
| 59 | + std::cerr << "IHttpHandler: Failed to find body in response\n"; | |
| 60 | + throw(std::runtime_error("IHttpHandler: Failed to find body in response")); | |
| 61 | 61 | } |
| 62 | 62 | response.erase(0, start + 4); |
| 63 | 63 | return response; |
| ... | ... | @@ -93,7 +93,7 @@ public: |
| 93 | 93 | request.append(" "); // Separation |
| 94 | 94 | request.append("HTTP/1.0"); // HTTP-Version |
| 95 | 95 | request.append("\r\n"); // Ending |
| 96 | - // Entities | |
| 96 | + // Entities | |
| 97 | 97 | request.append("Content-Type:"); // entity-header |
| 98 | 98 | request.append(" "); // Separation |
| 99 | 99 | request.append(content_type); // media-type |
| ... | ... | @@ -183,8 +183,8 @@ public: |
| 183 | 183 | std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); |
| 184 | 184 | if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) |
| 185 | 185 | { |
| 186 | - std::cout << "Error while parsing JSON in function GETJson() of linHttpHandler: " << error << std::endl; | |
| 187 | - throw(std::runtime_error("Error while parsing JSON in function GETJson() of linHttpHandler")); | |
| 186 | + std::cout << "IHttpHandler: Error while parsing JSON in function GETJson(): " << error << std::endl; | |
| 187 | + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function GETJson()")); | |
| 188 | 188 | } |
| 189 | 189 | return result; |
| 190 | 190 | }; |
| ... | ... | @@ -208,8 +208,8 @@ public: |
| 208 | 208 | std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); |
| 209 | 209 | if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) |
| 210 | 210 | { |
| 211 | - std::cout << "Error while parsing JSON in function POSTJson() of linHttpHandler: " << error << std::endl; | |
| 212 | - throw(std::runtime_error("Error while parsing JSON in function POSTJson() of linHttpHandler")); | |
| 211 | + std::cout << "IHttpHandler: Error while parsing JSON in function POSTJson(): " << error << std::endl; | |
| 212 | + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function POSTJson()")); | |
| 213 | 213 | } |
| 214 | 214 | return result; |
| 215 | 215 | } |
| ... | ... | @@ -233,8 +233,8 @@ public: |
| 233 | 233 | std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); |
| 234 | 234 | if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) |
| 235 | 235 | { |
| 236 | - std::cout << "Error while parsing JSON in function PUTJson() of linHttpHandler: " << error << std::endl; | |
| 237 | - throw(std::runtime_error("Error while parsing JSON in function PUTJson() of linHttpHandler")); | |
| 236 | + std::cout << "IHttpHandler: Error while parsing JSON in function PUTJson(): " << error << std::endl; | |
| 237 | + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function PUTJson()")); | |
| 238 | 238 | } |
| 239 | 239 | return result; |
| 240 | 240 | }; |
| ... | ... | @@ -258,8 +258,8 @@ public: |
| 258 | 258 | std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); |
| 259 | 259 | if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) |
| 260 | 260 | { |
| 261 | - std::cout << "Error while parsing JSON in function PUTJson() of linHttpHandler: " << error << std::endl; | |
| 262 | - throw(std::runtime_error("Error while parsing JSON in function PUTJson() of linHttpHandler")); | |
| 261 | + std::cout << "IHttpHandler: Error while parsing JSON in function PUTJson(): " << error << std::endl; | |
| 262 | + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function PUTJson()")); | |
| 263 | 263 | } |
| 264 | 264 | return result; |
| 265 | 265 | }; | ... | ... |
hueplusplus/linHttpHandler.cpp
| ... | ... | @@ -48,8 +48,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 48 | 48 | SocketCloser closeMySocket(socketFD); |
| 49 | 49 | if (socketFD < 0) |
| 50 | 50 | { |
| 51 | - std::cerr << "Failed to open socket\n"; | |
| 52 | - throw(std::runtime_error("Failed to open socket")); | |
| 51 | + std::cerr << "linHttpHandler: Failed to open socket\n"; | |
| 52 | + throw(std::runtime_error("linHttpHandler: Failed to open socket")); | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | // lookup ip address |
| ... | ... | @@ -57,8 +57,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 57 | 57 | server = gethostbyname(adr.c_str()); |
| 58 | 58 | if (server == NULL) |
| 59 | 59 | { |
| 60 | - std::cerr << "Failed to find host with address " << adr << "\n"; | |
| 61 | - throw(std::runtime_error("Failed to find host")); | |
| 60 | + std::cerr << "linHttpHandler: Failed to find host with address " << adr << "\n"; | |
| 61 | + throw(std::runtime_error("linHttpHandler: Failed to find host")); | |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | // fill in the structure |
| ... | ... | @@ -71,8 +71,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 71 | 71 | // connect the socket |
| 72 | 72 | if (connect(socketFD, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) |
| 73 | 73 | { |
| 74 | - std::cerr << "Failed to connect socket\n"; | |
| 75 | - throw(std::runtime_error("Failed to connect socket")); | |
| 74 | + std::cerr << "linHttpHandler: Failed to connect socket\n"; | |
| 75 | + throw(std::runtime_error("linHttpHandler: Failed to connect socket")); | |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // send the request |
| ... | ... | @@ -83,8 +83,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 83 | 83 | ssize_t bytes = write(socketFD, msg.c_str() + sent, total - sent); |
| 84 | 84 | if (bytes < 0) |
| 85 | 85 | { |
| 86 | - std::cerr << "Failed to write message to socket\n"; | |
| 87 | - throw(std::runtime_error("Failed to write message to socket")); | |
| 86 | + std::cerr << "linHttpHandler: Failed to write message to socket\n"; | |
| 87 | + throw(std::runtime_error("linHttpHandler: Failed to write message to socket")); | |
| 88 | 88 | } |
| 89 | 89 | if (bytes == 0) |
| 90 | 90 | { |
| ... | ... | @@ -106,8 +106,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 106 | 106 | ssize_t bytes = read(socketFD, buffer, 127); |
| 107 | 107 | if (bytes < 0) |
| 108 | 108 | { |
| 109 | - std::cerr << "Failed to read response from socket: " << errno << std::endl; | |
| 110 | - throw(std::runtime_error("Failed to read response from socket")); | |
| 109 | + std::cerr << "linHttpHandler: Failed to read response from socket: " << errno << std::endl; | |
| 110 | + throw(std::runtime_error("linHttpHandler: Failed to read response from socket")); | |
| 111 | 111 | } |
| 112 | 112 | if (bytes == 0) |
| 113 | 113 | { |
| ... | ... | @@ -122,8 +122,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad |
| 122 | 122 | |
| 123 | 123 | if (received == total) |
| 124 | 124 | { |
| 125 | - std::cerr << "Failed to store complete response from socket\n"; | |
| 126 | - throw(std::runtime_error("Failed to store complete response from socket")); | |
| 125 | + std::cerr << "linHttpHandler: Failed to store complete response from socket\n"; | |
| 126 | + throw(std::runtime_error("linHttpHandler: Failed to store complete response from socket")); | |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | return response; |
| ... | ... | @@ -143,8 +143,8 @@ std::vector<std::string> linHttpHandler::sendMulticast(const std::string & msg, |
| 143 | 143 | server = gethostbyname(adr.c_str()); |
| 144 | 144 | if (!server) |
| 145 | 145 | { |
| 146 | - std::cerr << "Failed to obtain address of " << msg << "\n"; | |
| 147 | - throw(std::runtime_error("Failed to obtain address of host")); | |
| 146 | + std::cerr << "linHttpHandler: sendMulticast: Failed to obtain address of " << msg << "\n"; | |
| 147 | + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to obtain address of host")); | |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | // put the host's address into the server address structure |
| ... | ... | @@ -155,15 +155,15 @@ std::vector<std::string> linHttpHandler::sendMulticast(const std::string & msg, |
| 155 | 155 | SocketCloser closeMySendSocket(socketFD); |
| 156 | 156 | if (socketFD < 0) |
| 157 | 157 | { |
| 158 | - std::cerr << "Failed to open socket\n"; | |
| 159 | - throw(std::runtime_error("Failed to open socket")); | |
| 158 | + std::cerr << "linHttpHandler: sendMulticast: Failed to open socket\n"; | |
| 159 | + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to open socket")); | |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | // send a message to the server |
| 163 | 163 | if (sendto(socketFD, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) |
| 164 | 164 | { |
| 165 | - std::cerr << "Failed to send message\n"; | |
| 166 | - throw(std::runtime_error("Failed to send message")); | |
| 165 | + std::cerr << "linHttpHandler: sendMulticast: Failed to send message\n"; | |
| 166 | + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to send message")); | |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | std::string response; |
| ... | ... | @@ -178,8 +178,8 @@ std::vector<std::string> linHttpHandler::sendMulticast(const std::string & msg, |
| 178 | 178 | { |
| 179 | 179 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
| 180 | 180 | { |
| 181 | - std::cerr << "Failed to read response from socket\n"; | |
| 182 | - throw(std::runtime_error("Failed to read response from socket")); | |
| 181 | + std::cerr << "linHttpHandler: sendMulticast: Failed to read response from socket\n"; | |
| 182 | + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to read response from socket")); | |
| 183 | 183 | } |
| 184 | 184 | continue; |
| 185 | 185 | } | ... | ... |
hueplusplus/winHttpHandler.cpp
| ... | ... | @@ -34,8 +34,8 @@ winHttpHandler::winHttpHandler() |
| 34 | 34 | // Initialize Winsock |
| 35 | 35 | if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) |
| 36 | 36 | { |
| 37 | - std::cerr << "Failed to open socket: " << WSAGetLastError() << std::endl; | |
| 38 | - throw(std::runtime_error("Failed to open socket")); | |
| 37 | + std::cerr << "winHttpHandler: Failed to open socket: " << WSAGetLastError() << std::endl; | |
| 38 | + throw(std::runtime_error("winHttpHandler: Failed to open socket")); | |
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | 41 | |
| ... | ... | @@ -57,8 +57,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 57 | 57 | int res = getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result); |
| 58 | 58 | if (res != 0) |
| 59 | 59 | { |
| 60 | - std::cerr << "getaddrinfo failed: " << res << std::endl; | |
| 61 | - throw(std::runtime_error("getaddrinfo failed")); | |
| 60 | + std::cerr << "winHttpHandler: getaddrinfo failed: " << res << std::endl; | |
| 61 | + throw(std::runtime_error("winHttpHandler: getaddrinfo failed")); | |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | SOCKET connect_socket = INVALID_SOCKET; |
| ... | ... | @@ -74,8 +74,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 74 | 74 | if (connect_socket == INVALID_SOCKET) |
| 75 | 75 | { |
| 76 | 76 | freeaddrinfo(result); |
| 77 | - std::cerr << "Error at socket(): " << WSAGetLastError() << std::endl; | |
| 78 | - throw(std::runtime_error("Error at socket()")); | |
| 77 | + std::cerr << "winHttpHandler: Error at socket(): " << WSAGetLastError() << std::endl; | |
| 78 | + throw(std::runtime_error("winHttpHandler: Error at socket()")); | |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | // Connect to server. |
| ... | ... | @@ -95,16 +95,16 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 95 | 95 | |
| 96 | 96 | if (connect_socket == INVALID_SOCKET) |
| 97 | 97 | { |
| 98 | - std::cerr << "Unable to connect to server!" << std::endl; | |
| 99 | - throw(std::runtime_error("Unable to connect to server!")); | |
| 98 | + std::cerr << "winHttpHandler: Unable to connect to server!" << std::endl; | |
| 99 | + throw(std::runtime_error("winHttpHandler: Unable to connect to server!")); | |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // Send an initial buffer |
| 103 | 103 | res = ::send(connect_socket, msg.c_str(), msg.size(), 0); |
| 104 | 104 | if (res == SOCKET_ERROR) |
| 105 | 105 | { |
| 106 | - std::cerr << "send failed: " << WSAGetLastError() << std::endl; | |
| 107 | - throw(std::runtime_error("send failed")); | |
| 106 | + std::cerr << "winHttpHandler: send failed: " << WSAGetLastError() << std::endl; | |
| 107 | + throw(std::runtime_error("winHttpHandler: send failed")); | |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // shutdown the connection for sending since no more data will be sent |
| ... | ... | @@ -113,8 +113,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 113 | 113 | if (res == SOCKET_ERROR) |
| 114 | 114 | { |
| 115 | 115 | closesocket(connect_socket); |
| 116 | - std::cerr << "shutdown failed: " << WSAGetLastError() << std::endl; | |
| 117 | - throw(std::runtime_error("shutdown failed")); | |
| 116 | + std::cerr << "winHttpHandler: shutdown failed: " << WSAGetLastError() << std::endl; | |
| 117 | + throw(std::runtime_error("winHttpHandler: shutdown failed")); | |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | const int recvbuflen = 128; |
| ... | ... | @@ -128,17 +128,17 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 128 | 128 | res = recv(connect_socket, recvbuf, recvbuflen, 0); |
| 129 | 129 | if (res > 0) |
| 130 | 130 | { |
| 131 | - std::cout << "Bytes received: " << res << std::endl; | |
| 131 | + std::cout << "winHttpHandler: Bytes received: " << res << std::endl; | |
| 132 | 132 | response.append(recvbuf, res); |
| 133 | 133 | } |
| 134 | 134 | else if (res == 0) |
| 135 | 135 | { |
| 136 | - std::cout << "Connection closed " << std::endl; | |
| 136 | + std::cout << "winHttpHandler: Connection closed " << std::endl; | |
| 137 | 137 | } |
| 138 | 138 | else |
| 139 | 139 | { |
| 140 | - std::cerr << "recv failed: " << WSAGetLastError() << std::endl; | |
| 141 | - throw(std::runtime_error("recv failed")); | |
| 140 | + std::cerr << "winHttpHandler: recv failed: " << WSAGetLastError() << std::endl; | |
| 141 | + throw(std::runtime_error("winHttpHandler: recv failed")); | |
| 142 | 142 | } |
| 143 | 143 | } while (res > 0); |
| 144 | 144 | |
| ... | ... | @@ -159,8 +159,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 159 | 159 | int res = getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result); |
| 160 | 160 | if (res != 0) |
| 161 | 161 | { |
| 162 | - std::cerr << "sendMulticast: getaddrinfo failed: " << res << std::endl; | |
| 163 | - throw(std::runtime_error("getaddrinfo failed")); | |
| 162 | + std::cerr << "winHttpHandler: sendMulticast: getaddrinfo failed: " << res << std::endl; | |
| 163 | + throw(std::runtime_error("winHttpHandler: sendMulticast: getaddrinfo failed")); | |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | SOCKET connect_socket = INVALID_SOCKET; |
| ... | ... | @@ -174,8 +174,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 174 | 174 | if ((connect_socket = socket(ptr->ai_family, ptr->ai_socktype, 0)) == INVALID_SOCKET) |
| 175 | 175 | { |
| 176 | 176 | freeaddrinfo(result); |
| 177 | - std::cerr << "sendMulticast: Error at socket(): " << WSAGetLastError() << std::endl; | |
| 178 | - throw(std::runtime_error("Error at socket()")); | |
| 177 | + std::cerr << "winHttpHandler: sendMulticast: Error at socket(): " << WSAGetLastError() << std::endl; | |
| 178 | + throw(std::runtime_error("winHttpHandler: sendMulticast: Error at socket()")); | |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // Fill out source socket's address information. |
| ... | ... | @@ -187,8 +187,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 187 | 187 | if (bind(connect_socket, (struct sockaddr FAR *) &source_sin, sizeof(source_sin)) == SOCKET_ERROR) |
| 188 | 188 | { |
| 189 | 189 | closesocket(connect_socket); |
| 190 | - std::cerr << "sendMulticast: Binding socket failed: " << WSAGetLastError() << std::endl; | |
| 191 | - throw(std::runtime_error("Binding socket failed")); | |
| 190 | + std::cerr << "winHttpHandler: sendMulticast: Binding socket failed: " << WSAGetLastError() << std::endl; | |
| 191 | + throw(std::runtime_error("winHttpHandler: sendMulticast: Binding socket failed")); | |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | u_long sock_mode = 1; |
| ... | ... | @@ -202,8 +202,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 202 | 202 | if (setsockopt(connect_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char FAR *)&iOptVal, sizeof(int)) == SOCKET_ERROR) |
| 203 | 203 | { |
| 204 | 204 | closesocket(connect_socket); |
| 205 | - std::cerr << "sendMulticast: setsockopt failed: " << WSAGetLastError() << std::endl; | |
| 206 | - throw(std::runtime_error("setsockopt failed")); | |
| 205 | + std::cerr << "winHttpHandler: sendMulticast: setsockopt failed: " << WSAGetLastError() << std::endl; | |
| 206 | + throw(std::runtime_error("winHttpHandler: sendMulticast: setsockopt failed")); | |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | // Fill out the desination socket's address information. |
| ... | ... | @@ -214,9 +214,9 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 214 | 214 | // Send a message to the multicasting address. |
| 215 | 215 | if (sendto(connect_socket, msg.c_str(), msg.size(), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin)) == SOCKET_ERROR) |
| 216 | 216 | { |
| 217 | - std::cerr << "sendMulticast: sendto failed: " << WSAGetLastError() << std::endl; | |
| 217 | + std::cerr << "winHttpHandler: sendMulticast: sendto failed: " << WSAGetLastError() << std::endl; | |
| 218 | 218 | closesocket(connect_socket); |
| 219 | - throw(std::runtime_error("sendto failed")); | |
| 219 | + throw(std::runtime_error("winHttpHandler: sendMulticast: sendto failed")); | |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | // shutdown the connection for sending since no more data will be sent |
| ... | ... | @@ -225,8 +225,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 225 | 225 | if (res == SOCKET_ERROR) |
| 226 | 226 | { |
| 227 | 227 | closesocket(connect_socket); |
| 228 | - std::cerr << "sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl; | |
| 229 | - throw(std::runtime_error("shutdown failed")); | |
| 228 | + std::cerr << "winHttpHandler: sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl; | |
| 229 | + throw(std::runtime_error("winHttpHandler: sendMulticast: shutdown failed")); | |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | std::string response; |
| ... | ... | @@ -238,12 +238,12 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 238 | 238 | res = recv(connect_socket, recvbuf, recvbuflen, 0); |
| 239 | 239 | if (res > 0) |
| 240 | 240 | { |
| 241 | - std::cout << "sendMulticast: Bytes received: " << res << std::endl; | |
| 241 | + std::cout << "winHttpHandler: sendMulticast: Bytes received: " << res << std::endl; | |
| 242 | 242 | response.append(recvbuf, res); |
| 243 | 243 | } |
| 244 | 244 | else if (res == 0) |
| 245 | 245 | { |
| 246 | - std::cout << "sendMulticast: Connection closed " << std::endl; | |
| 246 | + std::cout << "winHttpHandler: sendMulticast: Connection closed " << std::endl; | |
| 247 | 247 | } |
| 248 | 248 | else |
| 249 | 249 | { | ... | ... |