fix8  version 1.4.0
Open Source C++ FIX Framework
multisession.hpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-------------------------------------------------------------------------------------------------
37 #ifndef FIX8_MULTISESSSION_HPP_
38 #define FIX8_MULTISESSSION_HPP_
39 
40 //-------------------------------------------------------------------------------------------------
41 namespace FIX8 {
42 
43 //-------------------------------------------------------------------------------------------------
46 {
47  std::map<Poco::Net::ServerSocket, ServerSessionBase *> _servermap;
48 
49 public:
51  ServerManager() = default;
52 
54  virtual ~ServerManager()
55  { std::for_each (_servermap.begin(), _servermap.end(), [](decltype(_servermap)::value_type& pp){ delete pp.second; }); }
56 
60  bool add(ServerSessionBase *what)
61  {
62  if (!what || !what->_server_sock)
63  throw f8Exception("bad or missing server socket");
64  _servermap.insert({*what->_server_sock, what});
65  return true;
66  }
67 
72  ServerSessionBase *select(const Poco::Timespan& timeout=Poco::Timespan(250000)) const
73  {
74  Poco::Net::Socket::SocketList readList, writeList, exceptList;
75  for (auto& pp : _servermap)
76  readList.push_back(pp.first);
77  return Poco::Net::Socket::select(readList, writeList, exceptList, timeout) && !readList.empty()
78  ? _servermap.find(readList[0])->second : nullptr;
79  }
80 
86  size_t select_l(std::vector<ServerSessionBase *>& result, const Poco::Timespan& timeout=Poco::Timespan(250000)) const
87  {
88  result.clear();
89  Poco::Net::Socket::SocketList readList, writeList, exceptList;
90  for (auto& pp : _servermap)
91  readList.push_back(pp.first);
92  if (Poco::Net::Socket::select(readList, writeList, exceptList, timeout) && !readList.empty())
93  std::for_each(readList.begin(), readList.end(), [&](decltype(readList)::value_type& pp)
94  { result.push_back(_servermap.find(pp)->second); });
95  return result.size();
96  }
97 
100  unsigned size() const { return static_cast<unsigned>(_servermap.size()); }
101 };
102 
103 //-------------------------------------------------------------------------------------------------
105 template <typename T>
107 {
109  std::map<f8String, T*> _sessionmap;
110 
111 public:
113  SessionManager() = default;
114 
116  virtual ~SessionManager()
117  {
118  f8_scoped_lock guard(_mutex);
119  std::for_each (_sessionmap.begin(), _sessionmap.end(), [](typename decltype(_sessionmap)::value_type& pp){ delete pp.second; });
120  }
121 
126  bool add(const f8String& name, T *what)
127  {
128  if (!what)
129  throw f8Exception("bad or missing session");
130  f8_scoped_lock guard(_mutex);
131  return _sessionmap.insert({name, what}).second;
132  }
133 
137  T *for_each_if(std::function<bool(T*)> func)
138  {
139  for (auto& pp : _sessionmap)
140  {
141  f8_scoped_lock guard(_mutex);
142  if (!func(pp.second))
143  return pp.second;
144  }
145  return nullptr;
146  }
147 
151  bool remove(const f8String& name)
152  {
153  f8_scoped_lock guard(_mutex);
154  auto itr(_sessionmap.find(name));
155  if (itr == _sessionmap.end())
156  return false;
157  delete itr->second;
158  _sessionmap.erase(itr);
159  return true;
160  }
161 
165  T *find(const f8String& name)
166  {
167  f8_scoped_lock guard(_mutex);
168  const auto itr(_sessionmap.find(name));
169  return itr == _sessionmap.cend() ? nullptr : itr->second;
170  }
171 
175  T *operator[] (const f8String& name) const { return find(name); }
176 
179  unsigned size() const { return static_cast<unsigned>(_sessionmap.size()); }
180 };
181 
182 //-------------------------------------------------------------------------------------------------
187 
188 }
189 
190 #endif // FIX8_MULTISESSSION_HPP_
Session Manager.
Multi Server Manager.
generic pthread_mutex wrapper
Definition: thread.hpp:299
virtual ~SessionManager()
Dtor.
unsigned size() const
bool add(ServerSessionBase *what)
ServerManager()=default
Ctor.
Poco::Net::ServerSocket * _server_sock
std::map< Poco::Net::ServerSocket, ServerSessionBase * > _servermap
ServerSessionBase * select(const Poco::Timespan &timeout=Poco::Timespan(250000)) const
T * operator[](const f8String &name) const
std::map< f8String, T * > _sessionmap
T * for_each_if(std::function< bool(T *)> func)
Base exception class.
Definition: f8exception.hpp:49
unsigned size() const
SessionManager()=default
Ctor.
Base Server Session.
T * find(const f8String &name)
bool add(const f8String &name, T *what)
virtual ~ServerManager()
Dtor.
std::string f8String
Definition: f8types.hpp:47
size_t select_l(std::vector< ServerSessionBase * > &result, const Poco::Timespan &timeout=Poco::Timespan(250000)) const