Home | History | Annotate | Download | only in server
      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:
     31   INJECT(ServerImpl()) {}
     32 
     33   ~ServerImpl() {
     34     for (std::thread& t : threads) {
     35       t.join();
     36     }
     37   }
     38 
     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:
     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 
     76   static string getTime() {
     77     time_t now = time(nullptr);
     78     tm* localTime = localtime(&now);
     79     string result = asctime(localTime);
     80     if (result.size() != 0 && result.back() == '\n') {
     81       result.pop_back();
     82     }
     83     return result;
     84   }
     85 
     86   static Component<Request> getRequestComponent(Request* request) {
     87     return createComponent().bindInstance(*request);
     88   }
     89 
     90   static Component<Required<Request>, RequestDispatcher> getRequestDispatcherComponentWithContext(
     91       Component<Required<Request, ServerContext>, RequestDispatcher> (*getRequestDispatcherComponent)(),
     92       ServerContext* serverContext) {
     93     return createComponent().install(getRequestDispatcherComponent).bindInstance(*serverContext);
     94   }
     95 };
     96 
     97 fruit::Component<Server> getServerComponent() {
     98   return fruit::createComponent().bind<Server, ServerImpl>();
     99 }
    100