xref: /aosp_15_r20/external/google-fruit/examples/server/server.cpp (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "server.h"
18 
19 #include <ctime>
20 #include <iostream>
21 #include <thread>
22 
23 using namespace std;
24 using namespace fruit;
25 
26 class ServerImpl : public Server {
27 private:
28   std::vector<std::thread> threads;
29 
30 public:
INJECT(ServerImpl ())31   INJECT(ServerImpl()) {}
32 
~ServerImpl()33   ~ServerImpl() {
34     for (std::thread& t : threads) {
35       t.join();
36     }
37   }
38 
run(Component<Required<Request,ServerContext>,RequestDispatcher> (* getRequestDispatcherComponent)())39   void run(Component<Required<Request, ServerContext>, RequestDispatcher> (*getRequestDispatcherComponent)()) override {
40     ServerContext serverContext;
41     serverContext.startupTime = getTime();
42 
43     const NormalizedComponent<Required<Request>, RequestDispatcher> requestDispatcherNormalizedComponent(
44         getRequestDispatcherComponentWithContext, getRequestDispatcherComponent, &serverContext);
45 
46     cerr << "Server started." << endl;
47 
48     while (1) {
49       cerr << endl;
50       cerr << "Enter the request (absolute path starting with \"/foo/\" or \"/bar/\"), or an empty line to exit."
51            << endl;
52       Request request;
53       getline(cin, request.path);
54       cerr << "Server received request: " + request.path << endl;
55       if (request.path.empty()) {
56         cerr << "Server received empty line, shutting down." << endl;
57         break;
58       }
59 
60       // In production code we would use a thread pool.
61       // Here we spawn a new thread each time to keep it simple.
62       threads.push_back(std::thread(worker_thread_main, std::ref(requestDispatcherNormalizedComponent), request));
63     }
64   }
65 
66 private:
worker_thread_main(const NormalizedComponent<Required<Request>,RequestDispatcher> & requestDispatcherNormalizedComponent,Request request)67   static void worker_thread_main(
68       const NormalizedComponent<Required<Request>, RequestDispatcher>& requestDispatcherNormalizedComponent,
69       Request request) {
70     Injector<RequestDispatcher> injector(requestDispatcherNormalizedComponent, getRequestComponent, &request);
71 
72     RequestDispatcher* requestDispatcher(injector);
73     requestDispatcher->handleRequest();
74   }
75 
getTime()76   static string getTime() {
77     time_t now = time(nullptr);
78     tm* localTime = localtime(&now);
79     char buffer[100];
80     std::size_t num_written_chars = strftime(buffer, sizeof(buffer), "%A %B %e %T %Y", localTime);
81     if (num_written_chars == 0) {
82         return "";
83     }
84     string result(buffer);
85     if (result.size() != 0 && result.back() == '\n') {
86       result.pop_back();
87     }
88     return result;
89   }
90 
getRequestComponent(Request * request)91   static Component<Request> getRequestComponent(Request* request) {
92     return createComponent().bindInstance(*request);
93   }
94 
getRequestDispatcherComponentWithContext(Component<Required<Request,ServerContext>,RequestDispatcher> (* getRequestDispatcherComponent)(),ServerContext * serverContext)95   static Component<Required<Request>, RequestDispatcher> getRequestDispatcherComponentWithContext(
96       Component<Required<Request, ServerContext>, RequestDispatcher> (*getRequestDispatcherComponent)(),
97       ServerContext* serverContext) {
98     return createComponent().install(getRequestDispatcherComponent).bindInstance(*serverContext);
99   }
100 };
101 
getServerComponent()102 fruit::Component<Server> getServerComponent() {
103   return fruit::createComponent().bind<Server, ServerImpl>();
104 }
105