Bredbandskollen CLI  1.2
Asynchronous network task engine
bridgetask.h
1 // Copyright (c) 2019 Internetstiftelsen
2 // Written by Göran Andersson <initgoran@gmail.com>
3 
4 #pragma once
5 
6 #include <string>
7 #include "task.h"
8 
43 class BridgeTask : public Task {
44 
45 public:
51  BridgeTask(Task *agent = nullptr) : Task("Bridge"), the_agent(agent) {
53  }
54 
60  double start() override;
61 
63  void taskFinished(Task *task) override {
64  if (task == the_agent) {
65  if (!task->result().empty()) {
66  log() << "Agent terminated";
68  the_agent = nullptr;
69  }
70  die();
71  }
72  }
73 
76  void setAgent(Task *agent) {
77  if (!the_agent && !hasStarted())
78  the_agent = agent;
79  else
80  err_log() << "cannot set agent";
81  }
82 
84  void die() {
85  setResult("");
86  }
87 
89  virtual void sendMsgToClient(const std::string &msg) = 0;
90 
97  static std::string msgToAgent(const std::string &method,
98  const std::string &jsonobj = "{}") {
99  return "{\"method\": \"" + method + "\", \"args\": " + jsonobj + "}";
100  }
101 
104  static bool isAgentTerminatedMessage(const std::string &msg) {
105  return (msg.substr(0, 12) == "AGENT EXIT: ");
106  }
107 
109  static std::string agentTerminatedMessage(const std::string &err_msg) {
110  return "AGENT EXIT: " + err_msg;
111  }
112 
119  void sendMsgToClient(const std::string &method, const std::string &jsonobj) {
120  sendMsgToClient("{\"event\": \"" + method +
121  "\", \"args\": " + jsonobj + "}");
122  }
123 
124  virtual ~BridgeTask() override;
125 
126 protected:
128  void sendMsgToAgent(const std::string &msg) {
129  executeHandler(the_agent, msg);
130  if (msg.substr(0, terminate_msg.size()) == terminate_msg)
131  die();
132  }
133 
135  void sendMsgToAgent(const std::string &method, const std::string &jsonobj) {
136  sendMsgToAgent(msgToAgent(method, jsonobj));
137  }
138 private:
139  Task *the_agent;
140  //const std::string quit_msg = "quit";
141  const std::string terminate_msg = "{\"method\": \"terminate\"";
142 };
Tasks may use a bridge to communicate with an application running outside the event loop.
Definition: bridgetask.h:43
static std::string agentTerminatedMessage(const std::string &err_msg)
Format a message to signal that the Agent is gone.
Definition: bridgetask.h:109
void setAgent(Task *agent)
Definition: bridgetask.h:76
void sendMsgToAgent(const std::string &msg)
Send message to the agent.
Definition: bridgetask.h:128
virtual void sendMsgToClient(const std::string &msg)=0
The agent will call this to pass messages to the client.
void sendMsgToClient(const std::string &method, const std::string &jsonobj)
Format a message to the client.
Definition: bridgetask.h:119
static std::string msgToAgent(const std::string &method, const std::string &jsonobj="{}")
Format a message to the agent.
Definition: bridgetask.h:97
void die()
Terminate the bridge task.
Definition: bridgetask.h:84
double start() override
Will add the agent task to the EventLoop.
Definition: bridgetask.cpp:11
void taskFinished(Task *task) override
If the agent dies, a special message will be sent to notify the client.
Definition: bridgetask.h:63
BridgeTask(Task *agent=nullptr)
Create a bridge to the given agent task.
Definition: bridgetask.h:51
void sendMsgToAgent(const std::string &method, const std::string &jsonobj)
Format and send message to the agent.
Definition: bridgetask.h:135
static bool isAgentTerminatedMessage(const std::string &msg)
Definition: bridgetask.h:104
std::ostream & log() const
Write a line of info log.
Definition: logger.h:328
std::ostream & err_log() const
Write a line of error log.
Definition: logger.h:292
The purpose of a task is to manage socket connections, and/or to execute timers.
Definition: task.h:39
void setResult(const std::string &res)
Definition: task.cpp:18
void executeHandler(Task *receiver, const std::string &message)
Definition: task.h:222
bool hasStarted() const
Definition: task.h:115
void killChildTaskWhenFinished()
Definition: task.h:121
std::string result() const
To get the "result" of the task after it has finished.
Definition: task.h:167