increased chunk size

Dependencies:   HTTPClient-SSL

Fork of MTS-Socket by MultiTech

Committer:
mfiore
Date:
Tue Jun 03 14:49:26 2014 +0000
Revision:
7:08b474178245
Child:
8:a3b41ec82e63
add HTTPClient; add tests for HTTPClient and TCPSocketConnection - still need work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 7:08b474178245 1 #ifndef TESTTCPSOCKETCONNECTION_H
mfiore 7:08b474178245 2 #define TESTTCPSOCKETCONNECTION_H
mfiore 7:08b474178245 3
mfiore 7:08b474178245 4 #include "mtsas.h"
mfiore 7:08b474178245 5 #include <string>
mfiore 7:08b474178245 6
mfiore 7:08b474178245 7 using namespace mts;
mfiore 7:08b474178245 8
mfiore 7:08b474178245 9 class TestTCPSocketConnection : public TestCollection
mfiore 7:08b474178245 10 {
mfiore 7:08b474178245 11 public:
mfiore 7:08b474178245 12 TestTCPSocketConnection();
mfiore 7:08b474178245 13 virtual void run();
mfiore 7:08b474178245 14
mfiore 7:08b474178245 15 private:
mfiore 7:08b474178245 16 bool runIteration(const string name);
mfiore 7:08b474178245 17 MTSSerialFlowControl* io;
mfiore 7:08b474178245 18 Cellular* radio;
mfiore 7:08b474178245 19 TCPSocketConnection* sock;
mfiore 7:08b474178245 20 };
mfiore 7:08b474178245 21
mfiore 7:08b474178245 22 const char PATTERN_LINE1[] = "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|";
mfiore 7:08b474178245 23 const char PATTERN[] = "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"
mfiore 7:08b474178245 24 "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/"
mfiore 7:08b474178245 25 "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-"
mfiore 7:08b474178245 26 "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\"
mfiore 7:08b474178245 27 "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"
mfiore 7:08b474178245 28 "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/"
mfiore 7:08b474178245 29 "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-"
mfiore 7:08b474178245 30 "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\"
mfiore 7:08b474178245 31 "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*";
mfiore 7:08b474178245 32
mfiore 7:08b474178245 33 const char MENU_LINE1[] = "1 send ascii pattern until keypress";
mfiore 7:08b474178245 34 const char MENU[] = "1 send ascii pattern until keypress"
mfiore 7:08b474178245 35 "2 send ascii pattern (numbered)"
mfiore 7:08b474178245 36 "3 send pattern and close socket"
mfiore 7:08b474178245 37 "4 send [ETX] and wait for keypress"
mfiore 7:08b474178245 38 "5 send [DLE] and wait for keypress"
mfiore 7:08b474178245 39 "6 send all hex values (00-FF)"
mfiore 7:08b474178245 40 "q quit"
mfiore 7:08b474178245 41 ">:";
mfiore 7:08b474178245 42
mfiore 7:08b474178245 43 const char TEST_SERVER[] = "204.26.122.5";
mfiore 7:08b474178245 44 const int TEST_PORT = 7000;
mfiore 7:08b474178245 45
mfiore 7:08b474178245 46 TestTCPSocketConnection::TestTCPSocketConnection() : TestCollection("TestTCPSocketConnection") {}
mfiore 7:08b474178245 47
mfiore 7:08b474178245 48 void TestTCPSocketConnection::run() {
mfiore 7:08b474178245 49 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
mfiore 7:08b474178245 50
mfiore 7:08b474178245 51 Test::start("Setup");
mfiore 7:08b474178245 52 io = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
mfiore 7:08b474178245 53 io->baud(115200);
mfiore 7:08b474178245 54 radio = CellularFactory::create(io);
mfiore 7:08b474178245 55 if (! radio) {
mfiore 7:08b474178245 56 Test::assertTrue(false);
mfiore 7:08b474178245 57 }
mfiore 7:08b474178245 58 radio->configureSignals(PTA4, PTC9, PTA20);
mfiore 7:08b474178245 59 Transport::setTransport(radio);
mfiore 7:08b474178245 60
mfiore 7:08b474178245 61 for (int i = 0; i < 10; i++) {
mfiore 7:08b474178245 62 if (i >= 10) {
mfiore 7:08b474178245 63 Test::assertTrue(false);
mfiore 7:08b474178245 64 }
mfiore 7:08b474178245 65 if (radio->setApn("wap.cingular") == SUCCESS) {
mfiore 7:08b474178245 66 break;
mfiore 7:08b474178245 67 } else {
mfiore 7:08b474178245 68 wait(1);
mfiore 7:08b474178245 69 }
mfiore 7:08b474178245 70 }
mfiore 7:08b474178245 71 for (int i = 0; i < 3; i++) {
mfiore 7:08b474178245 72 if (i >= 3) {
mfiore 7:08b474178245 73 Test::assertTrue(false);
mfiore 7:08b474178245 74 }
mfiore 7:08b474178245 75 if (radio->connect()) {
mfiore 7:08b474178245 76 break;
mfiore 7:08b474178245 77 } else {
mfiore 7:08b474178245 78 wait(1);
mfiore 7:08b474178245 79 }
mfiore 7:08b474178245 80 }
mfiore 7:08b474178245 81
mfiore 7:08b474178245 82 for (int i = 0; i < 5; i++) {
mfiore 7:08b474178245 83 if (i >= 5) {
mfiore 7:08b474178245 84 Test::assertTrue(false);
mfiore 7:08b474178245 85 }
mfiore 7:08b474178245 86 if (radio->ping()) {
mfiore 7:08b474178245 87 break;
mfiore 7:08b474178245 88 } else {
mfiore 7:08b474178245 89 wait(1);
mfiore 7:08b474178245 90 }
mfiore 7:08b474178245 91 }
mfiore 7:08b474178245 92
mfiore 7:08b474178245 93 sock = new TCPSocketConnection();
mfiore 7:08b474178245 94 sock->set_blocking(false);
mfiore 7:08b474178245 95 Test::end();
mfiore 7:08b474178245 96
mfiore 7:08b474178245 97 for (int i = 0; i < 5; i++) {
mfiore 7:08b474178245 98 Test::assertTrue(runIteration("Iteration " + i));
mfiore 7:08b474178245 99 }
mfiore 7:08b474178245 100 }
mfiore 7:08b474178245 101
mfiore 7:08b474178245 102 bool TestTCPSocketConnection::runIteration(const string iteration) {
mfiore 7:08b474178245 103 Timer tmr;
mfiore 7:08b474178245 104 int bytesRead = 0;
mfiore 7:08b474178245 105 const int readSize = 1024;
mfiore 7:08b474178245 106 char buffer[readSize] = {0};
mfiore 7:08b474178245 107 string result;
mfiore 7:08b474178245 108
mfiore 7:08b474178245 109 for (int i = 0; i < 5; i++) {
mfiore 7:08b474178245 110 if (i >= 5) {
mfiore 7:08b474178245 111 return false;
mfiore 7:08b474178245 112 }
mfiore 7:08b474178245 113 if (! sock->connect(TEST_SERVER, TEST_PORT)) {
mfiore 7:08b474178245 114 break;
mfiore 7:08b474178245 115 } else {
mfiore 7:08b474178245 116 wait(1);
mfiore 7:08b474178245 117 }
mfiore 7:08b474178245 118 }
mfiore 7:08b474178245 119
mfiore 7:08b474178245 120 logInfo("Receiving Data");
mfiore 7:08b474178245 121 tmr.reset();
mfiore 7:08b474178245 122 tmr.start();
mfiore 7:08b474178245 123 do {
mfiore 7:08b474178245 124 bytesRead = sock->receive(buffer, readSize);
mfiore 7:08b474178245 125
mfiore 7:08b474178245 126 if (bytesRead > 0) {
mfiore 7:08b474178245 127 result.append(buffer, bytesRead);
mfiore 7:08b474178245 128 } else if (bytesRead <= 0) {
mfiore 7:08b474178245 129 break;
mfiore 7:08b474178245 130 }
mfiore 7:08b474178245 131
mfiore 7:08b474178245 132 logInfo("Total Bytes Read: %d", result.size());
mfiore 7:08b474178245 133 } while(tmr.read() <= 15 && result.size() < readSize);
mfiore 7:08b474178245 134
mfiore 7:08b474178245 135 size_t pos = result.find(MENU_LINE1);
mfiore 7:08b474178245 136 if(pos != string::npos) {
mfiore 7:08b474178245 137 int patternSize = sizeof(MENU) - 1;
mfiore 7:08b474178245 138 const char* ptr = &result.data()[pos];
mfiore 7:08b474178245 139 bool match = true;
mfiore 7:08b474178245 140 for(int i = 0; i < patternSize; i++) {
mfiore 7:08b474178245 141 if(MENU[i] != ptr[i]) {
mfiore 7:08b474178245 142 logError("1st Pattern Doesn't Match At [%d]", i);
mfiore 7:08b474178245 143 logError("Pattern [%02X] Buffer [%02X]", MENU[i], ptr[i]);
mfiore 7:08b474178245 144 match = false;
mfiore 7:08b474178245 145 break;
mfiore 7:08b474178245 146 }
mfiore 7:08b474178245 147 }
mfiore 7:08b474178245 148 if(match) {
mfiore 7:08b474178245 149 logError("Found Menu");
mfiore 7:08b474178245 150 }
mfiore 7:08b474178245 151 }
mfiore 7:08b474178245 152
mfiore 7:08b474178245 153 result.clear();
mfiore 7:08b474178245 154
mfiore 7:08b474178245 155 logInfo("Writing To Socket: 2");
mfiore 7:08b474178245 156 if(sock->send("2\r\n", 3) == 3) {
mfiore 7:08b474178245 157 logInfo("Successfully Wrote '2'");
mfiore 7:08b474178245 158 } else {
mfiore 7:08b474178245 159 logError("Failed To Write '2'");
mfiore 7:08b474178245 160 return false;
mfiore 7:08b474178245 161 }
mfiore 7:08b474178245 162 logInfo("Expecting 'how many ? >:");
mfiore 7:08b474178245 163 bytesRead = sock->receive(buffer, readSize);
mfiore 7:08b474178245 164 if(bytesRead > 0) {
mfiore 7:08b474178245 165 result.append(buffer, bytesRead);
mfiore 7:08b474178245 166 logInfo("Received: [%d] [%s]", bytesRead, result.c_str());
mfiore 7:08b474178245 167 if(result.find("how many") != std::string::npos) {
mfiore 7:08b474178245 168 logInfo("Successfully Found 'how many'");
mfiore 7:08b474178245 169 logInfo("Writing To Socket: 2");
mfiore 7:08b474178245 170 if(sock->send("2\r\n", 3) == 3) {
mfiore 7:08b474178245 171 logInfo("Successfully wrote '2'");
mfiore 7:08b474178245 172 } else {
mfiore 7:08b474178245 173 logError("Failed to write '2'");
mfiore 7:08b474178245 174 return false;
mfiore 7:08b474178245 175 }
mfiore 7:08b474178245 176 } else {
mfiore 7:08b474178245 177 logInfo("Missing second option to menu item 2");
mfiore 7:08b474178245 178 }
mfiore 7:08b474178245 179 } else {
mfiore 7:08b474178245 180 logError("Error reading from socket");
mfiore 7:08b474178245 181 return false;
mfiore 7:08b474178245 182 }
mfiore 7:08b474178245 183
mfiore 7:08b474178245 184 result.clear();
mfiore 7:08b474178245 185
mfiore 7:08b474178245 186 logInfo("Receiving Data");
mfiore 7:08b474178245 187 tmr.reset();
mfiore 7:08b474178245 188 tmr.start();
mfiore 7:08b474178245 189 do {
mfiore 7:08b474178245 190 bytesRead = sock->receive(buffer, readSize);
mfiore 7:08b474178245 191
mfiore 7:08b474178245 192 if (bytesRead > 0) {
mfiore 7:08b474178245 193 result.append(buffer, bytesRead);
mfiore 7:08b474178245 194 } else if (bytesRead <= 0) {
mfiore 7:08b474178245 195 break;
mfiore 7:08b474178245 196 }
mfiore 7:08b474178245 197
mfiore 7:08b474178245 198 logInfo("Total Bytes Read: %d", result.size());
mfiore 7:08b474178245 199 } while(tmr.read() <= 15 && result.size() < readSize);
mfiore 7:08b474178245 200
mfiore 7:08b474178245 201 logInfo("Received Data: [%d] [%s]", result.size(), result.c_str());
mfiore 7:08b474178245 202
mfiore 7:08b474178245 203 pos = result.find(PATTERN_LINE1);
mfiore 7:08b474178245 204 if(pos != string::npos) {
mfiore 7:08b474178245 205 int patternSize = sizeof(PATTERN) - 1;
mfiore 7:08b474178245 206 const char* ptr = &result.data()[pos];
mfiore 7:08b474178245 207 bool match = true;
mfiore 7:08b474178245 208 for(int i = 0; i < patternSize; i++) {
mfiore 7:08b474178245 209 if(PATTERN[i] != ptr[i]) {
mfiore 7:08b474178245 210 logError("1st Pattern Doesn't Match At [%d]", i);
mfiore 7:08b474178245 211 logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]);
mfiore 7:08b474178245 212 match = false;
mfiore 7:08b474178245 213 break;
mfiore 7:08b474178245 214 }
mfiore 7:08b474178245 215 }
mfiore 7:08b474178245 216 if(match) {
mfiore 7:08b474178245 217 logError("Found 1st Pattern");
mfiore 7:08b474178245 218 }
mfiore 7:08b474178245 219
mfiore 7:08b474178245 220 pos = result.find(PATTERN_LINE1, pos + patternSize);
mfiore 7:08b474178245 221 if(pos != std::string::npos) {
mfiore 7:08b474178245 222 ptr = &result.data()[pos];
mfiore 7:08b474178245 223 match = true;
mfiore 7:08b474178245 224 for(int i = 0; i < patternSize; i++) {
mfiore 7:08b474178245 225 if(PATTERN[i] != ptr[i]) {
mfiore 7:08b474178245 226 logError("2nd Pattern Doesn't Match At [%d]", i);
mfiore 7:08b474178245 227 logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]);
mfiore 7:08b474178245 228 match = false;
mfiore 7:08b474178245 229 break;
mfiore 7:08b474178245 230 }
mfiore 7:08b474178245 231 }
mfiore 7:08b474178245 232 if(match) {
mfiore 7:08b474178245 233 logError("Found 2nd Pattern");
mfiore 7:08b474178245 234 }
mfiore 7:08b474178245 235 }
mfiore 7:08b474178245 236 }
mfiore 7:08b474178245 237
mfiore 7:08b474178245 238 result.clear();
mfiore 7:08b474178245 239
mfiore 7:08b474178245 240 sock->close();
mfiore 7:08b474178245 241
mfiore 7:08b474178245 242 return true;
mfiore 7:08b474178245 243 }
mfiore 7:08b474178245 244
mfiore 7:08b474178245 245 #endif