OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TOOLS_ANDROID_FORWARDER2_DAEMON_H_ | 5 #ifndef TOOLS_ANDROID_FORWARDER2_DAEMON_H_ |
6 #define TOOLS_ANDROID_FORWARDER2_DAEMON_H_ | 6 #define TOOLS_ANDROID_FORWARDER2_DAEMON_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/at_exit.h" | |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/scoped_ptr.h" |
12 | 12 |
13 namespace forwarder2 { | 13 namespace forwarder2 { |
14 | 14 |
| 15 class Socket; |
| 16 |
| 17 // Provides a way to spawn a daemon and communicate with it. |
15 class Daemon { | 18 class Daemon { |
16 public: | 19 public: |
| 20 // Callback used by the daemon to shutdown properly. See pipe_notifier.h for |
| 21 // more details. |
| 22 typedef int (*GetExitNotifierFDCallback)(); |
| 23 |
| 24 class ClientDelegate { |
| 25 public: |
| 26 virtual ~ClientDelegate() {} |
| 27 |
| 28 // Called after the daemon is ready to receive commands. |
| 29 virtual void OnDaemonReady(Socket* daemon_socket) = 0; |
| 30 }; |
| 31 |
| 32 class ServerDelegate { |
| 33 public: |
| 34 virtual ~ServerDelegate() {} |
| 35 |
| 36 // Called after the daemon bound its Unix Domain Socket. This can be used to |
| 37 // setup signal handlers or perform global initialization. |
| 38 virtual void Init() = 0; |
| 39 |
| 40 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) = 0; |
| 41 |
| 42 virtual void OnServerExited() = 0; |
| 43 }; |
| 44 |
17 // |pid_file_path| is the file path to which the daemon's PID will be written. | 45 // |pid_file_path| is the file path to which the daemon's PID will be written. |
18 // Note that a lock on the file is also acquired to guarantee that a single | 46 // |identifier| should be a unique string identifier. It is used to |
19 // instance of daemon is running. | 47 // bind/connect the underlying Unix Domain Socket. |
20 explicit Daemon(const std::string& pid_file_path); | 48 // Note that this class does not take ownership of |client_delegate| and |
| 49 // |server_delegate|. |
| 50 Daemon(const std::string& log_file_path, |
| 51 const std::string& pid_file_path, |
| 52 const std::string& identifier, |
| 53 ClientDelegate* client_delegate, |
| 54 ServerDelegate* server_delegate, |
| 55 GetExitNotifierFDCallback get_exit_fd_callback); |
21 | 56 |
22 // Returns whether the daemon was successfully spawned. Use |is_daemon| to | 57 ~Daemon(); |
23 // distinguish the parent from the child (daemon) process. | |
24 bool Spawn(bool* is_daemon); | |
25 | 58 |
26 // Kills the daemon and blocks until it exited. | 59 // Returns whether the daemon was successfully spawned. Note that this does |
| 60 // not necessarily mean that the current process was forked in case the daemon |
| 61 // is already running. |
| 62 bool SpawnIfNeeded(); |
| 63 |
| 64 // Kills the daemon and blocks until it exited. Returns whether it succeeded. |
27 bool Kill(); | 65 bool Kill(); |
28 | 66 |
29 private: | 67 private: |
| 68 class PIDFile; |
| 69 |
| 70 const std::string log_file_path_; |
30 const std::string pid_file_path_; | 71 const std::string pid_file_path_; |
31 base::AtExitManager at_exit_manager; | 72 const std::string identifier_; |
| 73 ClientDelegate* const client_delegate_; |
| 74 ServerDelegate* const server_delegate_; |
| 75 const GetExitNotifierFDCallback get_exit_fd_callback_; |
| 76 scoped_ptr<PIDFile> pid_file_; |
32 | 77 |
33 DISALLOW_COPY_AND_ASSIGN(Daemon); | 78 DISALLOW_COPY_AND_ASSIGN(Daemon); |
34 }; | 79 }; |
35 | 80 |
36 } // namespace forwarder2 | 81 } // namespace forwarder2 |
37 | 82 |
38 #endif // TOOLS_ANDROID_FORWARDER2_DAEMON_H_ | 83 #endif // TOOLS_ANDROID_FORWARDER2_DAEMON_H_ |
OLD | NEW |