Service.h
1 /*
2  * Copyright (C) 2012 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_Http_Service_h
30 #define Pt_Http_Service_h
31 
32 #include <Pt/Http/Api.h>
33 #include <Pt/Http/Responder.h>
34 #include <Pt/Types.h>
35 #include <Pt/Allocator.h>
36 #include <Pt/NonCopyable.h>
37 
38 namespace Pt {
39 
40 namespace Http {
41 
42 class Request;
43 
46 class PT_HTTP_API Service : private NonCopyable
47 {
48  public:
51  Service();
52 
55  virtual ~Service();
56 
59  Responder* getResponder(const Request&);
60 
63  void releaseResponder(Responder*);
64 
65  protected:
68  virtual Responder* onGetResponder(const Request&) = 0;
69 
72  virtual void onReleaseResponder(Responder*) = 0;
73 
74  private:
75  // service specific options need to be set in Service ctor so it can
76  // be used concurrently by server threads without locking
77  Pt::varint_t _r0;
78  Pt::varint_t _r1;
79  Pt::varint_t _r2;
80 };
81 
84 template <typename R, typename Alloc = Allocator>
85 class BasicService : public Service
86 {
87  public:
91  { }
92 
96  { }
97 
98  protected:
99  virtual Responder* onGetResponder(const Request&)
100  {
101  void* r = _alloc.allocate( sizeof(R) );
102  return new(r) R(*this);
103  }
104 
105  virtual void onReleaseResponder(Responder* r)
106  {
107  r->~Responder();
108  _alloc.deallocate( r, sizeof(R) );
109  }
110 
111  private:
112  Alloc _alloc;
113 };
114 
115 } // namespace Http
116 
117 } // namespace Pt
118 
119 #endif // Pt_Http_Service_h
HTTP request message.
Definition: Request.h:43
Protects derived classes from being copied.
Definition: NonCopyable.h:54
virtual ~Responder()
Destructor.
HTTP service responder.
Definition: Responder.h:48
BasicService()
Default Constructor.
Definition: Service.h:90
HTTP service.
Definition: Service.h:46
virtual Responder * onGetResponder(const Request &)
Creates a responder to handle request received by a server.
Definition: Service.h:99
~BasicService()
Destructor.
Definition: Service.h:95
Basic HTTP service implementation.
Definition: Service.h:85
virtual void onReleaseResponder(Responder *r)
Destroys a responder created by a server.
Definition: Service.h:105