/*
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);
*/