OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <signal.h> |
| 6 #include <stdlib.h> |
| 7 #ifndef _WIN32 |
| 8 #include <sys/time.h> |
| 9 #include <sys/types.h> |
| 10 #include <sys/wait.h> |
| 11 #else |
| 12 #include <time.h> |
| 13 #endif |
| 14 #include <iostream> |
| 15 #include <fstream> |
| 16 |
| 17 #include "base/at_exit.h" |
| 18 #include "base/command_line.h" |
| 19 #include "base/logging.h" |
| 20 #include "base/scoped_ptr.h" |
| 21 #include "base/string_util.h" |
| 22 |
| 23 #include "chrome/test/webdriver/dispatch.h" |
| 24 #include "chrome/test/webdriver/session_manager.h" |
| 25 #include "chrome/test/webdriver/utility_functions.h" |
| 26 #include "chrome/test/webdriver/commands/create_session.h" |
| 27 #include "chrome/test/webdriver/commands/session_with_id.h" |
| 28 |
| 29 #include "third_party/mongoose/mongoose.h" |
| 30 |
| 31 // Make sure we have ho zombies from CGIs |
| 32 static void |
| 33 signal_handler(int sig_num) { |
| 34 switch (sig_num) { |
| 35 #ifdef OS_POSIX |
| 36 case SIGCHLD: |
| 37 while (waitpid(-1, &sig_num, WNOHANG) > 0) { } |
| 38 break; |
| 39 #elif OS_WIN |
| 40 case 0: // The win compiler demands at least 1 case statement |
| 41 #endif |
| 42 default: |
| 43 break; |
| 44 } |
| 45 } |
| 46 |
| 47 namespace webdriver { |
| 48 template <typename CommandType> |
| 49 void SetCallback(struct mg_context* ctx, const char* pattern) { |
| 50 mg_set_uri_callback(ctx, pattern, &Dispatch<CommandType>, NULL); |
| 51 } |
| 52 |
| 53 void InitCallbacks(struct mg_context* ctx) { |
| 54 SetCallback<CreateSession>(ctx, "/session"); |
| 55 |
| 56 // Since the /session/* is a wild card that would match the above URIs, this |
| 57 // line MUST be the last registered URI with the server |
| 58 SetCallback<SessionWithID>(ctx, "/session/*"); |
| 59 } |
| 60 } // namespace webdriver |
| 61 |
| 62 // Sets up and runs the Mongoose HTTP server for the JSON over HTTP |
| 63 // protcol of webdriver. The spec is located at: |
| 64 // http://code.google.com/p/selenium/wiki/JsonWireProtocol |
| 65 int main(int argc, char *argv[]) { |
| 66 struct mg_context *ctx; |
| 67 base::AtExitManager exit; |
| 68 std::string port = "8080"; |
| 69 #ifdef OS_POSIX |
| 70 CommandLine cmd_line = CommandLine(argc, argv); |
| 71 #elif OS_WIN |
| 72 std::string c; |
| 73 for (int i = 0; i < argc; ++i) { |
| 74 c += std::string(argv[i]); |
| 75 } |
| 76 CommandLine& cmd_line = CommandLine::FromString(ASCIIToWide(c)); |
| 77 #endif |
| 78 |
| 79 #if OS_POSIX |
| 80 signal(SIGPIPE, SIG_IGN); |
| 81 signal(SIGCHLD, &signal_handler); |
| 82 #endif |
| 83 srand((unsigned int)time(NULL)); |
| 84 |
| 85 if (cmd_line.HasSwitch(std::string("port"))) { |
| 86 port = cmd_line.GetSwitchValueASCII(std::string("port")); |
| 87 } |
| 88 |
| 89 LOG(INFO) << "Using port: " << port; |
| 90 webdriver::SessionManager* session = |
| 91 Singleton<webdriver::SessionManager>::get(); |
| 92 session->SetIPAddress(port); |
| 93 |
| 94 // Initialize SHTTPD context. |
| 95 // Listen on port 8080 or port specified on command line. |
| 96 // TODO(jmikhail) Maybe add port 8081 as a secure connection |
| 97 ctx = mg_start(); |
| 98 mg_set_option(ctx, "ports", port.c_str()); |
| 99 |
| 100 webdriver::InitCallbacks(ctx); |
| 101 |
| 102 std::cout << "Starting server" << std::endl; |
| 103 // the default behavior is to run this service forever |
| 104 while (true) { |
| 105 #ifdef OS_POSIX |
| 106 sleep(3600); |
| 107 #elif OS_WIN |
| 108 Sleep(3600); |
| 109 #endif |
| 110 } |
| 111 |
| 112 // we should not reach here since the service should never quit |
| 113 // TODO(jmikhail): register a listener for SIGTERM and break the |
| 114 // message loop gracefully. |
| 115 mg_stop(ctx); |
| 116 return (EXIT_SUCCESS); |
| 117 } |
| 118 |
OLD | NEW |