libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SCLangSubprocess.hpp
Go to the documentation of this file.
1 #ifndef SCLANGSUBPROCESS_HPP
2 #define SCLANGSUBPROCESS_HPP
3 /*
4 This file is part of AlgAudio.
5 
6 AlgAudio, Copyright (C) 2015 CeTA - Audiovisual Technology Center
7 
8 AlgAudio is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12 
13 AlgAudio is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with AlgAudio. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <atomic>
22 #include <list>
23 #include <thread>
24 #include <memory>
25 #include <mutex>
26 #include "Subprocess.hpp"
27 #include "Signal.hpp"
28 
29 namespace AlgAudio{
30 
35 public:
38  SCLangSubprocess(std::string command);
44  void Start();
46  void Stop();
47 
52  void TriggerSignals();
53 
56 
58  void SendInstruction(std::string);
62  void SendInstruction(std::string instruction, std::function<void(std::string)> reply_action);
63 private:
64  std::atomic<bool> started, run;
65  bool last_started = false; // Used by the MAIN thread only!
66  std::string buffer;
67  void ProcessBuffer();
68  int prompts = 0;
69  bool collecting_reply = false;
70  std::string reply_buffer;
71 
72  std::string command;
73 
74  std::unique_ptr<Subprocess> subprocess;
75 
76  void ThreadMain();
77  void Step();
78  std::thread the_thread;
79  std::recursive_mutex io_mutex;
80  std::string WaitForReply(std::string);
81  void WaitForPrompt();
82  void SendInstructionRaw(std::string);
83  void PollOutput();
84  // std::atomic does not work with std::list, as it has no default noexcept constructor.
85  std::list<std::string> lines_received;
86  std::list<std::pair<std::string,std::function<void(std::string)>>> instructions_actions;
87  //std::list<std::string> instructions;
88  std::list<std::function<void()>> replies;
89 };
90 
91 } // namespace AlgAudio
92 
93 #endif // SCLANGSUBPROCESS_HPP
void SendInstruction(std::string)
Definition: SCLangSubprocess.hpp:34
Signal on_started
Definition: SCLangSubprocess.hpp:55
Signal< std::string > on_any_line_received
Definition: SCLangSubprocess.hpp:54
Definition: Alertable.hpp:26
SCLangSubprocess(std::string command)