Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: src/main.cc

Issue 3528016: Cashew: implement backend usage API (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/cashew.git
Patch Set: Fix code review nits and remove hardcoded usage URLs Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/libcurl_http_fetcher.cc ('k') | src/policy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium OS 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 #include <config.h> 5 #include <config.h>
6 6
7 #include <signal.h> 7 #include <signal.h>
8 #include <stdio.h> 8 #include <stdio.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <string.h> 10 #include <string.h>
11 11
12 #include <glib.h>
13
12 #include <dbus-c++/dbus.h> 14 #include <dbus-c++/dbus.h>
15 #include <dbus-c++/glib-integration.h>
13 #include <gflags/gflags.h> 16 #include <gflags/gflags.h>
14 #include <glog/logging.h> 17 #include <glog/logging.h>
15 18
16 #include "src/cashew_server.h" 19 #include "src/cashew_server.h"
17 #include "src/service_manager.h" 20 #include "src/service_manager.h"
18 21
19 namespace cashew { 22 namespace cashew {
20 23
21 typedef int Signal; 24 typedef int Signal;
22 typedef void (*SignalHandler)(Signal signal); 25 typedef void (*SignalHandler)(Signal signal);
23 26
24 int InstallSignalHandler(const Signal signal, 27 int InstallSignalHandler(const Signal signal,
25 const SignalHandler signal_handler, 28 const SignalHandler signal_handler,
26 const sigset_t& mask) { 29 const sigset_t& mask) {
27 DLOG(INFO) << "installing handler for signal " << signal; 30 DLOG(INFO) << "installing handler for signal " << signal;
28 struct sigaction action; 31 struct sigaction action;
29 memset(&action, 0, sizeof(action)); 32 memset(&action, 0, sizeof(action));
30 action.sa_handler = signal_handler; 33 action.sa_handler = signal_handler;
31 action.sa_mask = mask; 34 action.sa_mask = mask;
32 return sigaction(signal, &action, NULL); 35 return sigaction(signal, &action, NULL);
33 } 36 }
34 37
35 static DBus::BusDispatcher *dispatcher = NULL; 38 static DBus::Glib::BusDispatcher *dispatcher = NULL;
36 39 static GMainLoop *main_loop = NULL;
37 static CashewServer *server = NULL; 40 static CashewServer *server = NULL;
38
39 static ServiceManager *service_manager = NULL; 41 static ServiceManager *service_manager = NULL;
40 42
41 static void OnSignal(int signal) { 43 static void OnSignal(int signal) {
42 DLOG(INFO) << "received signal " << signal; 44 DLOG(INFO) << "received signal " << signal;
43 if (dispatcher) { 45 if (main_loop != NULL) {
44 dispatcher->leave(); 46 g_main_loop_quit(main_loop);
45 } 47 }
46 } 48 }
47 49
48 int InstallSignalHandlers() { 50 int InstallSignalHandlers() {
49 sigset_t mask; 51 sigset_t mask;
50 if (sigemptyset(&mask) < 0) { 52 if (sigemptyset(&mask) < 0) {
51 PLOG(ERROR) << "sigemptyset failed"; 53 PLOG(ERROR) << "sigemptyset failed";
52 return -1; 54 return -1;
53 } 55 }
54 if (sigaddset(&mask, SIGINT) < 0) { 56 if (sigaddset(&mask, SIGINT) < 0) {
(...skipping 16 matching lines...) Expand all
71 } 73 }
72 74
73 // should not be called while event loop is running 75 // should not be called while event loop is running
74 static void CleanUpForExit() { 76 static void CleanUpForExit() {
75 DLOG(INFO) << "cleaning up"; 77 DLOG(INFO) << "cleaning up";
76 delete server; 78 delete server;
77 server = NULL; 79 server = NULL;
78 delete service_manager; 80 delete service_manager;
79 service_manager = NULL; 81 service_manager = NULL;
80 DBus::default_dispatcher = NULL; 82 DBus::default_dispatcher = NULL;
81 DBus::BusDispatcher *dispatcher_to_delete = dispatcher; 83 delete dispatcher;
82 dispatcher = NULL; 84 dispatcher = NULL;
83 delete dispatcher_to_delete; 85 if (main_loop != NULL) {
86 g_main_loop_unref(main_loop);
87 main_loop = NULL;
88 }
84 // TODO(vlaviano): uncomment when we get a newer version of glog 89 // TODO(vlaviano): uncomment when we get a newer version of glog
85 // google::ShutdownGoogleLogging(); 90 // google::ShutdownGoogleLogging();
86 } 91 }
87 92
88 } // namespace cashew 93 } // namespace cashew
89 94
90 int main(int argc, char *argv[]) { 95 int main(int argc, char *argv[]) {
91 google::SetUsageMessage(argv[0]); 96 google::SetUsageMessage(argv[0]);
92 google::ParseCommandLineFlags(&argc, &argv, true); 97 google::ParseCommandLineFlags(&argc, &argv, true);
93 google::InitGoogleLogging(argv[0]); 98 google::InitGoogleLogging(argv[0]);
94 google::InstallFailureSignalHandler(); 99 google::InstallFailureSignalHandler();
95 LOG(INFO) << PACKAGE_STRING; 100 LOG(INFO) << PACKAGE_STRING;
96 101
102 DLOG(INFO) << "creating main loop";
103 cashew::main_loop = g_main_loop_new(NULL, false);
104 if (cashew::main_loop == NULL) {
105 LOG(ERROR) << "couldn't create main loop";
106 cashew::CleanUpForExit();
107 exit(EXIT_FAILURE);
108 }
109
97 DLOG(INFO) << "creating dbus dispatcher"; 110 DLOG(INFO) << "creating dbus dispatcher";
98 cashew::dispatcher = new(std::nothrow) DBus::BusDispatcher(); 111 cashew::dispatcher = new(std::nothrow) DBus::Glib::BusDispatcher();
99 if (cashew::dispatcher == NULL) { 112 if (cashew::dispatcher == NULL) {
100 LOG(ERROR) << "couldn't create dbus dispatcher"; 113 LOG(ERROR) << "couldn't create dbus dispatcher";
101 cashew::CleanUpForExit(); 114 cashew::CleanUpForExit();
102 exit(EXIT_FAILURE); 115 exit(EXIT_FAILURE);
103 } 116 }
104 DBus::default_dispatcher = cashew::dispatcher; 117 DBus::default_dispatcher = cashew::dispatcher;
105 118 cashew::dispatcher->attach(NULL);
106 // TODO(vlaviano): policies
107
108 // TODO(vlaviano): back-end carrier apis
109 119
110 DLOG(INFO) << "creating service manager"; 120 DLOG(INFO) << "creating service manager";
111 DBus::Connection client_conn = DBus::Connection::SystemBus(); 121 DBus::Connection client_conn = DBus::Connection::SystemBus();
112 cashew::service_manager = new(std::nothrow) cashew::ServiceManager( 122 cashew::service_manager = new(std::nothrow) cashew::ServiceManager(
113 client_conn); 123 client_conn);
114 if (cashew::service_manager == NULL) { 124 if (cashew::service_manager == NULL) {
115 LOG(ERROR) << "couldn't create service manager"; 125 LOG(ERROR) << "couldn't create service manager";
116 cashew::CleanUpForExit(); 126 cashew::CleanUpForExit();
117 exit(EXIT_FAILURE); 127 exit(EXIT_FAILURE);
118 } 128 }
119 129
120 DLOG(INFO) << "creating cashew server"; 130 DLOG(INFO) << "creating cashew server";
121 // TODO(vlaviano): can we share a single conn for client and server roles? 131 // TODO(vlaviano): can we share a single conn for client and server roles?
122 DBus::Connection server_conn = DBus::Connection::SystemBus(); 132 DBus::Connection server_conn = DBus::Connection::SystemBus();
123 server_conn.request_name(cashew::CashewServer::kServiceName); 133 server_conn.request_name(cashew::CashewServer::kServiceName);
124 cashew::server = new(std::nothrow) cashew::CashewServer(server_conn, 134 cashew::server = new(std::nothrow) cashew::CashewServer(server_conn,
125 *cashew::service_manager); 135 cashew::service_manager);
126 if (cashew::server == NULL) { 136 if (cashew::server == NULL) {
127 LOG(ERROR) << "couldn't create cashew server"; 137 LOG(ERROR) << "couldn't create cashew server";
128 cashew::CleanUpForExit(); 138 cashew::CleanUpForExit();
129 exit(EXIT_FAILURE); 139 exit(EXIT_FAILURE);
130 } 140 }
131 141
132 if (cashew::InstallSignalHandlers() < 0) { 142 if (cashew::InstallSignalHandlers() < 0) {
133 LOG(ERROR) << "couldn't install signal handlers"; 143 LOG(ERROR) << "couldn't install signal handlers";
134 cashew::CleanUpForExit(); 144 cashew::CleanUpForExit();
135 exit(EXIT_FAILURE); 145 exit(EXIT_FAILURE);
136 } 146 }
137 147
138 DLOG(INFO) << "entering event loop"; 148 DLOG(INFO) << "entering event loop";
139 cashew::dispatcher->enter(); 149 g_main_loop_run(cashew::main_loop);
140 DLOG(INFO) << "exited event loop"; 150 DLOG(INFO) << "exited event loop";
141 cashew::CleanUpForExit(); 151 cashew::CleanUpForExit();
142 exit(EXIT_SUCCESS); 152 exit(EXIT_SUCCESS);
143 } 153 }
OLDNEW
« no previous file with comments | « src/libcurl_http_fetcher.cc ('k') | src/policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698