kaytat / stoker_socket_test

Stoker Socket Test

This URL has Read+Write access

stoker_socket_test / socket_test.cpp
100644 274 lines (223 sloc) 8.769 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Stoker socket test
*/
#include <winsock.h>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
 
#include "stoker_common.h"
#include "stream_util.h"
#include "stoker_command.h"
#include "stoker_response.h"
#include "stoker_command_stream.h"
 
using namespace std;
 
int usage() {
    cerr << "socket_test stoker_ip_address" << endl;
    return -1;
}
 
void clean_up(SOCKET s) {
    if (s != INVALID_SOCKET) {
        closesocket(s);
    }
 
    WSACleanup();
}
 
void initialize_network(const char* server_addr, SOCKET& sckt) {
    WSADATA winsock_data;
    int ret;
    
    sckt = INVALID_SOCKET;
 
    // Initialize Winsock
    ret = WSAStartup(MAKEWORD(2,2), &winsock_data);
    if (ret != 0) {
        cerr << "WSAStartup failed: " << ret << endl;
        clean_up(sckt);
        throw 0;
    }
 
    // Create a SOCKET for connecting to server
    sckt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
    if (sckt == INVALID_SOCKET) {
        cerr << "Error at socket(): " << WSAGetLastError() << endl;
        clean_up(sckt);
        throw 0;
    }
 
    //
    // Generate the client addr
    //
    sockaddr_in stoker_addr;
 
    stoker_addr.sin_family = AF_INET;
    stoker_addr.sin_addr.s_addr = inet_addr(server_addr);
    stoker_addr.sin_port = htons(44444);
 
    ret = connect(sckt, (SOCKADDR*)&stoker_addr, (int)sizeof(stoker_addr));
    if (ret == SOCKET_ERROR) {
        clean_up(sckt);
        throw 0;
    }
}
 
bool stream_send_recv(Stoker_Command_Stream& info_stream) {
    if (info_stream.send() == false) {
        cerr << "command stream send failed" << endl;
        return false;
    }
    if (info_stream.recv() == false) {
        cerr << "command stream send failed" << endl;
        return false;
    }
 
    return true;
}
 
int main(int argc, const char* argv[]) {
    SOCKET stoker_command_socket = NULL;
 
    // stream object
    Stoker_Command_Stream stream;
 
    // allocate response objects to be used by the command objects
    Stoker_Response ping_rsp;
    Stoker_Response disable_services_rsp;
    Stoker_Response_Dump_All_Info info_rsp;
    Stoker_Response temp_out_rsp;
 
    // allocate command objects
    Stoker_Command ping(STOKER_CMD_ID_PING, ping_rsp);
    Stoker_Command_Byte_Param disable_services(STOKER_CMD_ID_SERVICES, false, disable_services_rsp);
    Stoker_Command info(STOKER_CMD_ID_DUMP_ALL, info_rsp);
    Stoker_Command_Short_Param temp_out(STOKER_CMD_ID_TEMP_OUTPUT_ENABLE, 55555, temp_out_rsp);
 
    // Organize the command stream. All of the commands added by add_cmd will
    // be sent to the Stoker once the stream is sent. The order is also preserved.
    stream.add_cmd(ping);
    stream.add_cmd(disable_services);
    stream.add_cmd(info);
    stream.add_cmd(temp_out);
 
    cout << "Dumping stream before the stream is sent" << endl;
    cout << stream;
 
    if (argc < 2) {
        cerr << "Stoker hostname/IP address required" << endl;
        return usage();
    }
 
    try {
        initialize_network(argv[1], stoker_command_socket);
    } catch (...) {
        cerr << "initialize network failed" << endl;
        return usage();
    }
 
    stream.set_socket(stoker_command_socket);
 
    cout << "========================================" << endl;
    cout << "========================================" << endl;
    cout << "Sending the command stream" << endl;
    if (stream.send() == false) {
        cerr << "command stream send failed" << endl;
        return usage();
    }
 
    cout << "========================================" << endl;
    cout << "========================================" << endl;
    cout << "Processing return" << endl;
    if (stream.recv() == false) {
        cerr << "command stream receive failed" << endl;
        return usage();
    }
 
    cout << "========================================" << endl;
    cout << "========================================" << endl;
    cout << "Dumping stream" << endl;
    cout << stream;
 
    // Ok, parse out the info.
    if (info_rsp.sw.size() == 0) {
        cout << "ERROR: no switches detected" << endl;
        return 0;
    }
 
    Stoker_Command_Stream info_stream;
 
    info_stream.set_socket(stoker_command_socket);
    info_stream.add_cmd(info);
 
    cout << "**********************" << endl;
    cout << endl;
    cout << endl;
    cout << endl;
 
    bool cont = true;
    while (cont) {
        char c;
 
        cout << "Hit the key to get info" << endl;
        cin >> c;
        info_rsp.clear_info();
        if (stream_send_recv(info_stream) == false) {
            return usage();
        }
    }
 
    return 0;
}
 
/*
Some test code.
 
SOCKET stoker_command_socket = NULL;
Stoker_Command_Stream on_stream;
Stoker_Command_Stream off_stream;
 
Stoker_Response sw_on_rsp;
Stoker_Response sw_off_rsp;
 
Stoker_Command_Switch_Enable sw_on(sw_on_rsp);
Stoker_Command_Switch_Enable sw_off(sw_off_rsp);
 
Ow_Id fake_ow_id1;
 
fake_ow_id2.b31_00 = 0x1234abcd;
fake_ow_id2.b63_32 = 0x9876cccf;
 
on_stream.set_socket(stoker_command_socket);
off_stream.set_socket(stoker_command_socket);
 
sw_on.set_params(fake_ow_id1, true);
sw_off.set_params(fake_ow_id1, false);
 
on_stream.add_cmd(sw_on);
off_stream.add_cmd(sw_off);
*/
 
/*
More test code
// allocate response objects to be used by the command objects
Stoker_Response manual_mode_on_rsp;
Stoker_Response manual_mode_off_rsp;
Stoker_Response enable_services_rsp;
Stoker_Response_Version ver_rsp;
Stoker_Response name_change_rsp;
Stoker_Response alarm_mode_rsp;
Stoker_Response temp_change_rsp;
Stoker_Response sw_change_rsp;
Stoker_Response_Get_Error_Code get_error_code_rsp;
Stoker_Response db_sv_rsp;
Stoker_Response reboot_rsp;
Stoker_Response http_port_rsp;
Stoker_Response min_max_rsp;
Stoker_Response mult_rsp;
 
// allocate command objects
Stoker_Command_Byte_Param manual_mode_on(STOKER_CMD_ID_SET_MANUAL_MODE, true, manual_mode_on_rsp);
Stoker_Command_Byte_Param manual_mode_off(STOKER_CMD_ID_SET_MANUAL_MODE, false, manual_mode_off_rsp);
Stoker_Command_Byte_Param enable_services(STOKER_CMD_ID_SERVICES, true, enable_services_rsp);
Stoker_Command ver(STOKER_CMD_ID_VERSION, ver_rsp);
Stoker_Command_Name_Change name_change(name_change_rsp);
Stoker_Command_Alarm_Mode alarm_mode(alarm_mode_rsp);
Stoker_Command_Temp_Change temp_change(temp_change_rsp);
Stoker_Command_Switch_Change sw_change(sw_change_rsp);
Stoker_Command get_error_code(STOKER_CMD_ID_GET_ERROR_CODE, get_error_code_rsp);
Stoker_Command db_sv(STOKER_CMD_ID_DB_SAVE, db_sv_rsp);
Stoker_Command reboot(STOKER_CMD_ID_REBOOT, reboot_rsp);
Stoker_Command_Short_Param http_change(STOKER_CMD_ID_HTTP_PORT, 8080, http_port_rsp);
Stoker_Command_Duty_Cycle_Min_Max min_max(min_max_rsp);
Stoker_Command_Duty_Cycle_Time_Slice_Multiplier mult(mult_rsp);
 
// Just for testing, fake some one-wire ID's. These really don't exist.
Ow_Id fake_sr_id;
Ow_Id fake_sw_id;
 
fake_sr_id.b31_00 = 0x1234abcd;
fake_sr_id.b63_32 = 0x9876cccf;
 
fake_sw_id.b31_00 = 0xfedcba98;
fake_sw_id.b63_32 = 0x76543210;
 
// Setup some of the more complicated parameters not set in the constructor
name_change.set_params(fake_sr_id, "test name12");
alarm_mode.set_params(fake_sr_id, STOKER_ALARM_MODE_TARGET);
temp_change.set_params(fake_sr_id, STOKER_TEMP_ID_TARGET, 500.0);
sw_change.set_params(fake_sr_id, fake_sw_id);
 
min_max.set_params(fake_sw_id, 30, 70);
mult.set_params(fake_sw_id, 3);
 
// Organize the command stream. All of the commands added by add_cmd will
// be sent to the Stoker once the stream is sent. The order is also preserved.
stream.add_cmd(manual_mode_on);
stream.add_cmd(manual_mode_off);
stream.add_cmd(enable_services);
stream.add_cmd(ver);
stream.add_cmd(name_change);
stream.add_cmd(alarm_mode);
stream.add_cmd(temp_change);
stream.add_cmd(sw_change);
stream.add_cmd(get_error_code);
stream.add_cmd(db_sv);
stream.add_cmd(reboot);
stream.add_cmd(http_change);
stream.add_cmd(min_max);
stream.add_cmd(mult);
*/