Bredbandskollen CLI  1.2
Asynchronous network task engine
websocketbridge.h
1 // Copyright (c) 2018 IIS (The Internet Foundation in Sweden)
2 // Written by Göran Andersson <initgoran@gmail.com>
3 
4 #pragma once
5 
6 #include <deque>
7 #include <atomic>
8 
9 #include "../framework/bridgetask.h"
10 #include "../http/webservertask.h"
11 #include "../framework/serversocket.h"
12 
13 class WSBlistener;
14 
15 // Open a web server, wait for one (and only one) client to open a websocket
16 // connection to the server. The websocket connection will then be used to
17 // pass text messages between the agent and the client.
18 // The client must connect to the URL /wsbridge
19 //
20 // Config parameter - ValIT
21 //
22 // listen - port number for the web server (0 to use a random port)
23 // listen_addr - IP number to listen on, e.g. 127.0.0.1 (or 0.0.0.0 for any)
24 // listen_pw - (optional) password; cilent must supply the password as value
25 // of URL parameter pwd when connecting to /wsbridge
26 // browser - (optional) value 0, 1, 2, or 3. If 0, write an URL to stderr;
27 // user may open the URL in a browser to load a web interface.
28 // If 1, an attempt to fire up the web interface in a new browser
29 // tab will be made. If that fails, write the URL to stderr.
30 // If 2, only generate the URL to the web interface. The client
31 // program must open the URL in a web view.
32 // If 3, generate the URL to the next generation web interface. The
33 // client program must open the URL in a web view. The web interface
34 // uses HTTPS, but must connect using an unencrypted web socket.
35 // Note that most webview components will refuse to do so.
36 // url - (optional) domain name to the server containing the legacy web
37 // interface; ignored if value of "browser" is "3".
38 
39 class WebsocketBridge : public BridgeTask {
40 public:
41  WebsocketBridge(Task *agent, const TaskConfig &cfg);
42 
43  virtual ~WebsocketBridge() override;
44 
45  double start() override;
46 
47  void sendMsgToClient(const std::string &msg) override;
48 
49  void handleExecution(Task *sender, const std::string &msg) override;
50 
51  void taskFinished(Task *task) override;
52 
53  // Returns empty string unless ready. Thread safe.
54  std::string url() const;
55  std::string port() const;
56 
57  // Return true if client is connected:
58  bool clientConnected() const;
59 
60 private:
61  WSBlistener *listen_task = nullptr;
62  std::atomic<uint16_t> listen_port;
63 
64 
65 };
Tasks may use a bridge to communicate with an application running outside the event loop.
Definition: bridgetask.h:43
Read configuration from file or string.
Definition: taskconfig.h:44
The purpose of a task is to manage socket connections, and/or to execute timers.
Definition: task.h:39
Definition: websocketbridge.cpp:16
Definition: websocketbridge.h:39
void sendMsgToClient(const std::string &msg) override
The agent will call this to pass messages to the client.
Definition: websocketbridge.cpp:131
void handleExecution(Task *sender, const std::string &msg) override
Callback to execute code on behalf of another Task.
Definition: websocketbridge.cpp:90
void taskFinished(Task *task) override
If the agent dies, a special message will be sent to notify the client.
Definition: websocketbridge.cpp:96
double start() override
Will add the agent task to the EventLoop.
Definition: websocketbridge.cpp:80