LCOV - code coverage report
Current view: top level - vnsw/agent/oper - instance_task.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 16 35 45.7 %
Date: 2026-06-11 01:56:02 Functions: 6 19 31.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Juniper Networks, Inc. All right reserved.
       3             :  */
       4             : #ifndef AGENT_OPER_INSTANCE_TASK_H__
       5             : #define AGENT_OPER_INSTANCE_TASK_H__
       6             : 
       7             : #include <queue>
       8             : #include <boost/asio.hpp>
       9             : #include <boost/function.hpp>
      10             : #include "base/timer.h"
      11             : #include "base/queue_task.h"
      12             : #include "cmn/agent_signal.h"
      13             : #include "cmn/agent_cmn.h"
      14             : #include "db/db_entry.h"
      15             : 
      16             : class EventManager;
      17             : 
      18             : class InstanceTask {
      19             :  public:
      20             :     typedef boost::function<void(InstanceTask *task, const std::string &msg)>
      21             :         OnDataCallback;
      22             :     typedef boost::function<void(InstanceTask *task,
      23             :             const boost::system::error_code &ec)>OnExitCallback;
      24             : 
      25             :     InstanceTask();
      26           0 :     virtual ~InstanceTask() {}
      27             : 
      28             :     virtual bool Run() = 0;
      29             :     virtual void Stop() = 0;
      30             :     virtual void Terminate() = 0;
      31             :     virtual bool IsSetup() = 0;
      32             : 
      33             :     // TODO reimplement instance_manager.cc to remove these two?
      34             :     virtual pid_t pid() const = 0;
      35             :     virtual const std::string &cmd() const = 0;
      36             : 
      37             :     virtual int cmd_type() const = 0;
      38             : 
      39           0 :     bool is_running() const {
      40           0 :         return is_running_;
      41             :     }
      42             : 
      43           0 :     time_t start_time() const {
      44           0 :         return start_time_;
      45             :     }
      46             : 
      47           1 :     void set_on_data_cb(OnDataCallback cb) {
      48           1 :         on_data_cb_ = cb;
      49           1 :     }
      50             : 
      51           1 :     void set_on_exit_cb(OnExitCallback cb) {
      52           1 :         on_exit_cb_ = cb;
      53           1 :     }
      54             : 
      55           0 :     int incr_reattempts() {
      56           0 :         return ++reattempts_;
      57             :     }
      58             : 
      59           0 :     int reattempts() {
      60           0 :         return reattempts_;
      61             :     }
      62             : 
      63             :  protected:
      64             :     bool is_running_;
      65             :     time_t start_time_;
      66             :     int reattempts_;
      67             :     OnDataCallback on_data_cb_;
      68             :     OnExitCallback on_exit_cb_;
      69             : };
      70             : 
      71             : class InstanceTaskExecvp : public InstanceTask {
      72             :  public:
      73             :     static const size_t kBufLen = 4096;
      74             : 
      75             :     InstanceTaskExecvp(const std::string &name, const std::string &cmd,
      76             :                        int cmd_type, EventManager *evm);
      77             : 
      78             :     bool Run();
      79             :     void Stop();
      80             :     void Terminate();
      81             :     bool IsSetup();
      82             : 
      83           0 :     pid_t pid() const {
      84           0 :         return pid_;
      85             :     }
      86             : 
      87          32 :     void set_cmd(std::string cmd) {
      88          32 :         cmd_ = cmd;
      89          32 :     }
      90             : 
      91           0 :     const std::string &cmd() const {
      92           0 :         return cmd_;
      93             :     }
      94             : 
      95           0 :     int cmd_type() const {
      96           0 :         return cmd_type_;
      97             :     }
      98             : 
      99           1 :     void set_pipe_stdout(bool pipe) {
     100           1 :         pipe_stdout_ = pipe;
     101           1 :     }
     102             : 
     103             : 
     104             :  private:
     105             :     void ReadData(const boost::system::error_code &ec, size_t read_bytes);
     106             : 
     107             :     const std::string name_;
     108             :     std::string cmd_;
     109             :     boost::asio::posix::stream_descriptor input_;
     110             :     bool setup_done_; // indicates whether errors_ has a valid descriptor or not
     111             :     char rx_buff_[kBufLen];
     112             : 
     113             :     pid_t pid_;
     114             :     int cmd_type_;
     115             :     bool pipe_stdout_;
     116             : };
     117             : 
     118             : class InstanceTaskMethod : public InstanceTask {
     119             :  public:
     120             :     pid_t pid() const {
     121             :         return 0;
     122             :     }
     123             : };
     124             : 
     125             : class InstanceTaskQueue {
     126             : public:
     127             :     typedef boost::function<void(InstanceTaskQueue *task_queue)> OnTimeoutCallback;
     128             :     InstanceTaskQueue(EventManager *evm);
     129             :     ~InstanceTaskQueue();
     130             : 
     131             :     bool OnTimerTimeout();
     132             :     void TimerErrorHandler(const std::string &name, std::string error);
     133             : 
     134           0 :     InstanceTask *Front() { return task_queue_.front(); }
     135           0 :     void Pop() { task_queue_.pop(); }
     136           0 :     bool Empty() { return task_queue_.empty(); }
     137           0 :     void Push(InstanceTask *task) { task_queue_.push(task); }
     138           6 :     int Size() { return task_queue_.size(); }
     139             :     void StartTimer(int time);
     140             :     void StopTimer();
     141             :     void Clear();
     142             : 
     143           2 :     void set_on_timeout_cb(OnTimeoutCallback cb) {
     144           2 :         on_timeout_cb_ = cb;
     145           2 :     }
     146             : 
     147             : private:
     148             :     EventManager *evm_;
     149             :     Timer *timeout_timer_;
     150             :     std::queue<InstanceTask *> task_queue_;
     151             :     OnTimeoutCallback on_timeout_cb_;
     152             : };
     153             : 
     154             : #endif  // AGENT_OPER_INSTANCE_TASK_H__

Generated by: LCOV version 1.14