From ca108ac6930dbbbce3471238964393c3f092f736 Mon Sep 17 00:00:00 2001 From: Daniel Pocock Date: Fri, 31 May 2013 14:45:54 +0200 Subject: [PATCH] Add examples/sip_client based on examples/echo_client --- examples/sip_client/CMakeLists.txt | 11 ++++ examples/sip_client/SConscript | 23 ++++++++ examples/sip_client/echo_client.cpp | 82 +++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 examples/sip_client/CMakeLists.txt create mode 100644 examples/sip_client/SConscript create mode 100644 examples/sip_client/echo_client.cpp diff --git a/examples/sip_client/CMakeLists.txt b/examples/sip_client/CMakeLists.txt new file mode 100644 index 0000000000..a3d6d3b2e5 --- /dev/null +++ b/examples/sip_client/CMakeLists.txt @@ -0,0 +1,11 @@ + +file (GLOB SOURCE_FILES *.cpp) +file (GLOB HEADER_FILES *.hpp) + +init_target (echo_client) + +build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES}) + +link_boost () +final_target () + diff --git a/examples/sip_client/SConscript b/examples/sip_client/SConscript new file mode 100644 index 0000000000..291ade5df0 --- /dev/null +++ b/examples/sip_client/SConscript @@ -0,0 +1,23 @@ +## Main development example +## + +Import('env') +Import('env_cpp11') +Import('boostlibs') +Import('platform_libs') +Import('polyfill_libs') + +env = env.Clone () +env_cpp11 = env_cpp11.Clone () + +prgs = [] + +# if a C++11 environment is avaliable build using that, otherwise use boost +if env_cpp11.has_key('WSPP_CPP11_ENABLED'): + ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs] + prgs += env_cpp11.Program('echo_client', ["echo_client.cpp"], LIBS = ALL_LIBS) +else: + ALL_LIBS = boostlibs(['system','regex','random'],env) + [platform_libs] + [polyfill_libs] + prgs += env.Program('echo_client', ["echo_client.cpp"], LIBS = ALL_LIBS) + +Return('prgs') diff --git a/examples/sip_client/echo_client.cpp b/examples/sip_client/echo_client.cpp new file mode 100644 index 0000000000..5ce319a9d8 --- /dev/null +++ b/examples/sip_client/echo_client.cpp @@ -0,0 +1,82 @@ +#include + +#include + +#include + +typedef websocketpp::client client; + +using websocketpp::lib::placeholders::_1; +using websocketpp::lib::placeholders::_2; +using websocketpp::lib::bind; + +// pull out the type of messages sent by our config +typedef websocketpp::config::asio_client::message_type::ptr message_ptr; + +int case_count = 0; + +void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) { + client::connection_ptr con = c->get_con_from_hdl(hdl); + + if (con->get_resource() == "/getCaseCount") { + std::cout << "Detected " << msg->get_payload() << " test cases." << std::endl; + case_count = atoi(msg->get_payload().c_str()); + } else { + c->send(hdl, msg->get_payload(), msg->get_opcode()); + } +} + +int main(int argc, char* argv[]) { + // Create a server endpoint + client echo_client; + + std::string uri = "ws://localhost:9001"; + + if (argc == 2) { + uri = argv[1]; + } + + try { + // We expect there to be a lot of errors, so suppress them + echo_client.clear_access_channels(websocketpp::log::alevel::all); + echo_client.clear_error_channels(websocketpp::log::elevel::all); + + // Initialize ASIO + echo_client.init_asio(); + + // Register our handlers + echo_client.set_message_handler(bind(&on_message,&echo_client,::_1,::_2)); + + websocketpp::lib::error_code ec; + client::connection_ptr con = echo_client.get_connection(uri+"/getCaseCount", ec); + echo_client.connect(con); + + // Start the ASIO io_service run loop + echo_client.run(); + + std::cout << "case count: " << case_count << std::endl; + + for (int i = 1; i <= case_count; i++) { + echo_client.reset(); + + std::stringstream url; + + url << uri << "/runCase?case=" << i << "&agent=WebSocket++/0.3.0-dev"; + + con = echo_client.get_connection(url.str(), ec); + + echo_client.connect(con); + + echo_client.run(); + } + + std::cout << "done" << std::endl; + + } catch (const std::exception & e) { + std::cout << e.what() << std::endl; + } catch (websocketpp::lib::error_code e) { + std::cout << e.message() << std::endl; + } catch (...) { + std::cout << "other exception" << std::endl; + } +}