From 0402515d35d78717f4a02ef6daed8036127f48aa Mon Sep 17 00:00:00 2001 From: "kjellander@webrtc.org" Date: Tue, 1 Jul 2014 16:28:13 +0000 Subject: [PATCH] Implement command line flags for peerconnection client example on Windows Adding the flags and functionality for 'autoconnect', 'autocall', 'server', 'port', and 'help' like in the linux example. BUG=3459 R=tommi@webrtc.org Review URL: https://webrtc-codereview.appspot.com/13609004 Patch from Vicken Simonian . git-svn-id: http://webrtc.googlecode.com/svn/trunk@6573 4adac7df-926f-26a2-2b94-8c16560cd09d --- AUTHORS | 1 + talk/examples/peerconnection/client/main.cc | 20 +++++++++++- .../peerconnection/client/main_wnd.cc | 31 ++++++++++++++++--- .../examples/peerconnection/client/main_wnd.h | 6 +++- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/AUTHORS b/AUTHORS index d9bb54d55..4cabdefe9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -14,6 +14,7 @@ Paul Kapustin Rafael Lopez Diez Robert Nagy Silviu Caragea +Vicken Simonian Victor Costan Google Inc. diff --git a/talk/examples/peerconnection/client/main.cc b/talk/examples/peerconnection/client/main.cc index e68c78ef8..765dfaaa8 100644 --- a/talk/examples/peerconnection/client/main.cc +++ b/talk/examples/peerconnection/client/main.cc @@ -26,6 +26,7 @@ */ #include "talk/examples/peerconnection/client/conductor.h" +#include "talk/examples/peerconnection/client/flagdefs.h" #include "talk/examples/peerconnection/client/main_wnd.h" #include "talk/examples/peerconnection/client/peer_connection_client.h" #include "talk/base/ssladapter.h" @@ -39,7 +40,24 @@ int PASCAL wWinMain(HINSTANCE instance, HINSTANCE prev_instance, talk_base::Win32Thread w32_thread; talk_base::ThreadManager::Instance()->SetCurrentThread(&w32_thread); - MainWnd wnd; + WindowsCommandLineArguments win_args; + int argc = win_args.argc(); + char **argv = win_args.argv(); + + FlagList::SetFlagsFromCommandLine(&argc, argv, true); + if (FLAG_help) { + FlagList::Print(NULL, false); + return 0; + } + + // Abort if the user specifies a port that is outside the allowed + // range [1, 65535]. + if ((FLAG_port < 1) || (FLAG_port > 65535)) { + printf("Error: %i is not a valid port.\n", FLAG_port); + return -1; + } + + MainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall); if (!wnd.Create()) { ASSERT(false); return -1; diff --git a/talk/examples/peerconnection/client/main_wnd.cc b/talk/examples/peerconnection/client/main_wnd.cc index 6211e9947..cef1da706 100644 --- a/talk/examples/peerconnection/client/main_wnd.cc +++ b/talk/examples/peerconnection/client/main_wnd.cc @@ -36,6 +36,8 @@ ATOM MainWnd::wnd_class_ = 0; const wchar_t MainWnd::kClassName[] = L"WebRTC_MainWnd"; +using talk_base::sprintfn; + namespace { const char kConnecting[] = "Connecting... "; @@ -79,10 +81,15 @@ void AddListBoxItem(HWND listbox, const std::string& str, LPARAM item_data) { } // namespace -MainWnd::MainWnd() +MainWnd::MainWnd(const char* server, int port, bool auto_connect, + bool auto_call) : ui_(CONNECT_TO_SERVER), wnd_(NULL), edit1_(NULL), edit2_(NULL), label1_(NULL), label2_(NULL), button_(NULL), listbox_(NULL), - destroyed_(false), callback_(NULL), nested_msg_(NULL) { + destroyed_(false), callback_(NULL), nested_msg_(NULL), + server_(server), auto_connect_(auto_connect), auto_call_(auto_call) { + char buffer[10] = {0}; + sprintfn(buffer, sizeof(buffer), "%i", port); + port_ = buffer; } MainWnd::~MainWnd() { @@ -158,6 +165,9 @@ void MainWnd::SwitchToConnectUI() { ui_ = CONNECT_TO_SERVER; LayoutConnectUI(true); ::SetFocus(edit1_); + + if (auto_connect_) + ::PostMessage(button_, BM_CLICK, 0, 0); } void MainWnd::SwitchToPeerList(const Peers& peers) { @@ -173,6 +183,19 @@ void MainWnd::SwitchToPeerList(const Peers& peers) { ui_ = LIST_PEERS; LayoutPeerListUI(true); ::SetFocus(listbox_); + + if (auto_call_ && peers.begin() != peers.end()) { + // Get the number of items in the list + LRESULT count = ::SendMessage(listbox_, LB_GETCOUNT, 0, 0); + if (count != LB_ERR) { + // Select the last item in the list + LRESULT selection = ::SendMessage(listbox_, LB_SETCURSEL , count - 1, 0); + if (selection != LB_ERR) + ::PostMessage(wnd_, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(listbox_), + LBN_DBLCLK), + reinterpret_cast(listbox_)); + } + } } void MainWnd::SwitchToStreamingUI() { @@ -465,8 +488,8 @@ void MainWnd::CreateChildWindows() { CreateChildWindow(&listbox_, LISTBOX_ID, L"ListBox", LBS_HASSTRINGS | LBS_NOTIFY, WS_EX_CLIENTEDGE); - ::SetWindowTextA(edit1_, GetDefaultServerName().c_str()); - ::SetWindowTextA(edit2_, "8888"); + ::SetWindowTextA(edit1_, server_.c_str()); + ::SetWindowTextA(edit2_, port_.c_str()); } void MainWnd::LayoutConnectUI(bool show) { diff --git a/talk/examples/peerconnection/client/main_wnd.h b/talk/examples/peerconnection/client/main_wnd.h index 6d2bf3eee..77da9f69a 100644 --- a/talk/examples/peerconnection/client/main_wnd.h +++ b/talk/examples/peerconnection/client/main_wnd.h @@ -93,7 +93,7 @@ class MainWnd : public MainWindow { UI_THREAD_CALLBACK = WM_APP + 1, }; - MainWnd(); + MainWnd(const char* server, int port, bool auto_connect, bool auto_call); ~MainWnd(); bool Create(); @@ -207,6 +207,10 @@ class MainWnd : public MainWindow { void* nested_msg_; MainWndCallback* callback_; static ATOM wnd_class_; + std::string server_; + std::string port_; + bool auto_connect_; + bool auto_call_; }; #endif // WIN32