Bredbandskollen CLI  1.2
Asynchronous network task engine
synchronousbridge.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 <map>
8 
9 #include "../framework/bridgetask.h"
10 
18 public:
23  virtual void initialMsgToAgent(std::deque<std::string> &return_msgs);
24 
32  virtual void newEventFromAgent(std::deque<std::string> &return_msgs,
33  const std::string &msg) = 0;
34 
35  virtual ~SynchronousClient() { }
36 };
37 
44 class SynchronousBridge : public BridgeTask {
45 public:
46  SynchronousBridge(Task *agent, SynchronousClient *client) :
47  BridgeTask(agent),
48  the_client(client) {
49  }
50 
52  double start() override;
53 
55  void sendMsgToClient(const std::string &msg) override;
56 
57  virtual ~SynchronousBridge() override;
58 private:
59  void clear_queue() {
60  while (!incoming_messages.empty()) {
61  std::string msg = incoming_messages.front();
62  incoming_messages.pop_front();
63  log() << "sendMsgToAgent " << msg;
64  sendMsgToAgent(msg);
65  }
66  }
67  SynchronousClient *the_client;
68  std::deque<std::string> incoming_messages;
69 };
Tasks may use a bridge to communicate with an application running outside the event loop.
Definition: bridgetask.h:43
void sendMsgToAgent(const std::string &msg)
Send message to the agent.
Definition: bridgetask.h:128
BridgeTask(Task *agent=nullptr)
Create a bridge to the given agent task.
Definition: bridgetask.h:51
std::ostream & log() const
Write a line of info log.
Definition: logger.h:328
A bridge that "owns" the client.
Definition: synchronousbridge.h:44
void sendMsgToClient(const std::string &msg) override
Will call the client's SynchronousClient::newEventFromAgent method.
Definition: synchronousbridge.cpp:23
double start() override
See Task::start.
Definition: synchronousbridge.cpp:12
Client that only exists (or, rather, executes code) from within the bridge.
Definition: synchronousbridge.h:17
virtual void newEventFromAgent(std::deque< std::string > &return_msgs, const std::string &msg)=0
Retrieve a new message from the agent.
virtual void initialMsgToAgent(std::deque< std::string > &return_msgs)
Send initial messages to the agent.
Definition: synchronousbridge.cpp:8
The purpose of a task is to manage socket connections, and/or to execute timers.
Definition: task.h:39