channel.h 5.5 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#ifndef BAIDU_PADDLE_SERVING_PREDICTOR_CHANNEL_H
#define BAIDU_PADDLE_SERVING_PREDICTOR_CHANNEL_H

#include "common/inner_common.h"

namespace baidu {
namespace paddle_serving {
namespace predictor {

class Channel;

class Bus {
public:
    Bus() {
        clear();
    }

    int regist(const std::string& op, Channel* channel) {
        std::pair<boost::unordered_map<std::string, Channel*>::iterator, bool> r
            = _op_channels.insert(std::make_pair(op, channel));
        if (!r.second) {
            LOG(ERROR) << "Failed insert op&channel into bus:" << op;
            return -1;
        }
        return 0;
    }

    Channel* channel_by_name(const std::string& op_name) {
        typename boost::unordered_map<std::string, Channel*>::iterator it 
            = _op_channels.find(op_name);
        if (it == _op_channels.end()) {
            LOG(WARNING) 
                << "Not found channel in bus, op_name:" 
                << op_name << ".";
            return NULL;
        }
        
        return it->second;
    }

    void clear() {
        _op_channels.clear();
    }

    size_t size() const {
        return _op_channels.size();
    }

private:
    boost::unordered_map<std::string, Channel*> _op_channels;
};

class Channel {
public:
    Channel() {}

    void init(uint32_t id, const char* op) {
        _id = id;
        _op = std::string(op);
        clear_data();
    }

    void deinit() {
        clear_data();
    }

    uint32_t id() const {
        return _id; 
    }

    const std::string& op() {
        return _op;
    }

    int share_to_bus(Bus* bus) {
        if (bus->regist(_op, this) != 0) {
77
            LOG(ERROR) 
W
wangguibao 已提交
78
                << "Failed regist channel[" << _op
W
wangguibao 已提交
79
                << "] to bus!";
W
wangguibao 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
            return -1;
        }

        return 0;
    }

    virtual void clear_data() = 0;

    virtual void* param() = 0;
    virtual const void* param() const = 0;

    virtual google::protobuf::Message* message() = 0;
    virtual const google::protobuf::Message* message() const = 0;

    virtual Channel& operator=(const Channel& channel) = 0;

    virtual std::string debug_string() const = 0;

private:
    uint32_t _id;
    std::string _op;
};

template<typename T>
class OpChannel : public Channel {
public:
    OpChannel() {
    }

    void clear_data() {
        _data.Clear();
    }

    void* param() {
        return &_data;
    }

    const void* param() const {
        return &_data;
    }

    google::protobuf::Message* message() {
        return message_impl(derived_from_message<
                TIsDerivedFromB<T, google::protobuf::Message>::RESULT>());
    }

    google::protobuf::Message* message_impl(derived_from_message<true>) {
        return dynamic_cast<const google::protobuf::Message*>(&_data);
    }

    google::protobuf::Message* message_impl(derived_from_message<false>) {
131
        LOG(ERROR) << "Current type: " << typeid(T).name()
W
wangguibao 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145
            << " is not derived from protobuf.";
        return NULL; 
    }

    const google::protobuf::Message* message() const {
        return message_impl(derived_from_message<
                TIsDerivedFromB<T, google::protobuf::Message>::RESULT>());
    }

    const google::protobuf::Message* message_impl(derived_from_message<true>) const {
        return dynamic_cast<const google::protobuf::Message*>(&_data);
    }

    const google::protobuf::Message* message_impl(derived_from_message<false>) const {
146
        LOG(ERROR) << "Current type: " << typeid(T).name()
W
wangguibao 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
            << " is not derived from protobuf.";
        return NULL; 
    }
    
    Channel& operator=(const Channel& channel) {
        _data = *(dynamic_cast<const OpChannel<T>&>(channel)).data();
        return *this;
    }

    std::string debug_string() const {
        return _data.ShortDebugString();
    }
    
    // functions of derived class

    T* data() {
        return &_data;
    }

    const T* data() const {
        return &_data;
    }

    Channel& operator=(const T& obj) {
        _data = obj;
        return *this;
    }

private:
    T _data;
};

template<>
class OpChannel<google::protobuf::Message> : public Channel {
public:
    OpChannel<google::protobuf::Message>() : _data(NULL) {
    }

    virtual ~OpChannel<google::protobuf::Message>() {
        _data = NULL;
    }

    void clear_data() {
        _data = NULL;
    }

    void* param() {
        return const_cast<void*>((const void*)_data);
    }

    const void* param() const {
        return _data;
    }

    google::protobuf::Message* message() {
        return const_cast<google::protobuf::Message*>(_data);
    }

    const google::protobuf::Message* message() const {
        return _data;
    }

    Channel& operator=(const Channel& channel) {
        _data = channel.message();
        return *this;
    }

    std::string debug_string() const {
        if (_data) {
            return _data->ShortDebugString();
        } else {
            return "{\"Error\": \"Null Message Ptr\"}";
        }
    }

    // derived function imiplements
    google::protobuf::Message* data() {
        return const_cast<google::protobuf::Message*>(_data);
    }

    const google::protobuf::Message* data() const {
        return _data;
    }

    OpChannel<google::protobuf::Message>& operator=(
            google::protobuf::Message* message) {
        _data = message;
        return *this;
    }

    OpChannel<google::protobuf::Message>& operator=(
            const google::protobuf::Message* message) {
        _data = message;
        return *this;
    }

private:
    const google::protobuf::Message* _data;
};

} // predictor
} // paddle_serving
} // baidu

#endif