tntnet.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2007 Tommi Maekitalo
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 
30 #ifndef TNT_TNTNET_H
31 #define TNT_TNTNET_H
32 
33 #include <tnt/job.h>
34 #include <tnt/poller.h>
35 #include <tnt/dispatcher.h>
36 #include <tnt/maptarget.h>
37 #include <tnt/scopemanager.h>
38 #include <cxxtools/condition.h>
39 #include <cxxtools/mutex.h>
40 #include <set>
41 #include <fstream>
42 
43 namespace tnt
44 {
45  class ListenerBase;
46  struct TntConfig;
47 
61  class Tntnet
62  {
63  friend class Worker;
64 
65  unsigned minthreads;
66  unsigned maxthreads;
67 
68  Jobqueue queue;
69 
70  static bool stop;
71  typedef std::set<ListenerBase*> listeners_type;
72  listeners_type listeners;
73  static listeners_type allListeners;
74 
75  cxxtools::AttachedThread pollerthread;
76  Poller poller;
77  Dispatcher dispatcher;
78 
79  ScopeManager scopemanager;
80  std::string appname;
81 
82  std::ofstream accessLog;
83  cxxtools::Mutex accessLogMutex;
84 
85  // noncopyable
86  Tntnet(const Tntnet&);
87  Tntnet& operator= (const Tntnet&);
88 
89  void timerTask();
90 
91  static cxxtools::Condition timerStopCondition;
92  static cxxtools::Mutex timeStopMutex;
93 
94  public:
96  Tntnet();
97 
99  void init(const TntConfig& config);
103  void listen(const std::string& ipaddr, unsigned short int port);
105  void listen(unsigned short int port)
106  { listen(std::string(), port); }
107 
111  void sslListen(const std::string& certificateFile, const std::string& keyFile, const std::string& ipaddr, unsigned short int port);
113  void sslListen(const std::string& certificateFile, const std::string& keyFile, unsigned short int port)
114  { sslListen(certificateFile, keyFile, std::string(), port); }
115 
121  void run();
122 
124  static void shutdown();
126  static bool shouldStop() { return stop; }
127 
129  Jobqueue& getQueue() { return queue; }
131  Poller& getPoller() { return poller; }
133  const Dispatcher& getDispatcher() const { return dispatcher; }
135  ScopeManager& getScopemanager() { return scopemanager; }
136 
138  unsigned getMinThreads() const { return minthreads; }
140  void setMinThreads(unsigned n);
141 
143  unsigned getMaxThreads() const { return maxthreads; }
145  void setMaxThreads(unsigned n) { maxthreads = n; }
146 
150  Mapping& mapUrl(const std::string& url, const std::string& ci)
151  { return dispatcher.addUrlMapEntry(std::string(), url, Maptarget(ci)); }
152  void mapUrl(const std::string& url, const std::string& pathinfo, const std::string& ci_)
153  {
154  Maptarget ci(ci_);
155  ci.setPathInfo(pathinfo);
156  dispatcher.addUrlMapEntry(std::string(), url, ci);
157  }
158  Mapping& mapUrl(const std::string& url, const Maptarget& ci)
159  { return dispatcher.addUrlMapEntry(std::string(), url, ci); }
160  Mapping& vMapUrl(const std::string& vhost, const std::string& url, const Maptarget& ci)
161  { return dispatcher.addUrlMapEntry(vhost, url, ci); }
162 
176  void setAppName(const std::string& appname_)
177  { appname = appname_; }
178  const std::string& getAppName() const
179  { return appname; }
180 
181  void setAccessLog(const std::string& accesslog)
182  { accessLog.open(accesslog.c_str(), std::ios::out | std::ios::app); }
183  };
184 
185 }
186 
187 #endif // TNT_TNTNET_H
188