automotive-message-broker  0.14.803
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
websockets.h
1 /*
2 Copyright (C) 2012 Intel Corporation
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 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef _WEBSOCKETS_H_
20 #define _WEBSOCKETS_H_
21 
22 #include <map>
23 #include <memory>
24 #include <libwebsockets.h>
25 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
26 #include <boost/interprocess/sync/scoped_lock.hpp>
27 #include "abstractpropertytype.h"
28 
29 using namespace boost::interprocess;
30 
42 public:
43  virtual ~WebSocketsObserver() { } /*LCOV_EXCL_LINE*/
44 
52  virtual void dataReceived(lws* socket, const char* data, size_t len) = 0;
53 };
54 
63 class WebSockets {
64 public:
65  enum Type {
66  Server,
67  Client
68  };
69 
73  WebSockets(WebSocketsObserver& observer, Type t=Server, int port = 23001, std::string ip="");
74 
79  WebSockets(const WebSockets&) = delete;
85  WebSockets& operator=(const WebSockets&) = delete;
90  WebSockets(WebSockets&&) = delete;
96  WebSockets& operator=(WebSockets&&) = delete;
97 
98  ~WebSockets();
99 
107  static int Write(lws *lws, const std::string& strToWrite);
108 
115  void addPoll(int fd);
116 
123  void removePoll(int fd);
124 
131  inline WebSocketsObserver& getObserver() { return observer; }
132 
139  inline lws_context* getContext() { return context.get(); }
140 
141 private:
142 
147  WebSocketsObserver& observer;
148 
149 
155  struct lws_protocols protocollist[2];
156 
161  typedef std::unique_ptr<lws_context, decltype(&lws_context_destroy)> lwsContextPtr;
162 
168  lwsContextPtr context;
169 
175  std::map<int,GIOChannel*> m_ioChannelMap;
176 
182  std::map<int,guint> m_ioSourceMap;
183 
189  interprocess_recursive_mutex mutex;
190 };
191 
192 #endif // _WEBSOCKETS_H_
193