bmv2
Designing your own switch target with bmv2
transport.h
1 /* Copyright 2013-present Barefoot Networks, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /*
17  * Antonin Bas (antonin@barefootnetworks.com)
18  *
19  */
20 
21 #ifndef BM_BM_SIM_TRANSPORT_H_
22 #define BM_BM_SIM_TRANSPORT_H_
23 
24 #include <bm/config.h>
25 
26 #include <string>
27 #include <initializer_list>
28 #include <memory>
29 
30 namespace bm {
31 
32 class TransportIface {
33  public:
34  struct MsgBuf {
35  char *buf;
36  unsigned int len;
37  };
38 
39  public:
40  virtual ~TransportIface() { }
41 
42  int open() {
43  if (opened) return 1;
44  opened = true;
45  return open_();
46  }
47 
48  int send(const std::string &msg) const {
49  return send_(msg);
50  }
51 
52  int send(const char *msg, int len) const {
53  return send_(msg, len);
54  }
55 
56  int send_msgs(const std::initializer_list<std::string> &msgs) const {
57  return send_msgs_(msgs);
58  }
59 
60  int send_msgs(const std::initializer_list<MsgBuf> &msgs) const {
61  return send_msgs_(msgs);
62  }
63 
64 #ifdef BM_NANOMSG_ON
65  static std::unique_ptr<TransportIface> make_nanomsg(const std::string &addr);
66 #endif
67  static std::unique_ptr<TransportIface> make_dummy();
68  static std::unique_ptr<TransportIface> make_stdout();
69 
70  private:
71  virtual int open_() = 0;
72 
73  virtual int send_(const std::string &msg) const = 0;
74  virtual int send_(const char *msg, int len) const = 0;
75 
76  virtual int send_msgs_(
77  const std::initializer_list<std::string> &msgs) const = 0;
78  virtual int send_msgs_(const std::initializer_list<MsgBuf> &msgs) const = 0;
79 
80  bool opened{false};
81 };
82 
83 } // namespace bm
84 
85 #endif // BM_BM_SIM_TRANSPORT_H_