OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TTransport.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef _SANDESH_TRANSPORT_TTRANSPORT_H_
21 #define _SANDESH_TRANSPORT_TTRANSPORT_H_ 1
22 
23 #include <stdint.h>
24 #include <string>
25 #include <boost/shared_ptr.hpp>
26 
27 #include <base/logging.h>
28 
29 namespace contrail { namespace sandesh { namespace transport {
30 
34 template <class Transport_>
35 int32_t readAll(Transport_ &trans, uint8_t* buf, uint32_t len) {
36  uint32_t have = 0;
37  uint32_t get = 0;
38 
39  while (have < len) {
40  get = trans.read(buf+have, len-have);
41  if (get <= 0) {
42  LOG(ERROR, __func__ << ": No more data to read");
43  return -1;
44  }
45  have += get;
46  }
47 
48  return have;
49 }
50 
51 
57 class TTransport {
58  public:
62  virtual ~TTransport() {}
63 
67  virtual bool isOpen() {
68  return false;
69  }
70 
79  virtual bool peek() {
80  return isOpen();
81  }
82 
88  virtual void open() {
89  LOG(ERROR, __func__ << ": Cannot open base TTransport.");
90  assert(false);
91  }
92 
96  virtual void close() {
97  LOG(ERROR, __func__ << ": Cannot close base TTransport.");
98  assert(false);
99  }
100 
108  int32_t read(uint8_t* buf, uint32_t len) {
109 
110  return read_virt(buf, len);
111  }
112  virtual int32_t read_virt(uint8_t* /* buf */, uint32_t /* len */) {
113  LOG(ERROR, __func__ << ": Base TTransport cannot read.");
114  assert(false);
115  return -1;
116  }
117 
125  int32_t readAll(uint8_t* buf, uint32_t len) {
126 
127  return readAll_virt(buf, len);
128  }
129  virtual int32_t readAll_virt(uint8_t* buf, uint32_t len) {
130  return contrail::sandesh::transport::readAll(*this, buf, len);
131  }
132 
140  virtual uint32_t readEnd() {
141  // default behaviour is to do nothing
142  return 0;
143  }
144 
157  int write(const uint8_t* buf, uint32_t len) {
158 
159  return write_virt(buf, len);
160  }
161  virtual int write_virt(const uint8_t* /* buf */, uint32_t /* len */) {
162  LOG(ERROR, __func__ << ": Base TTransport cannot write.");
163  assert(false);
164  return -1;
165  }
166 
174  virtual uint32_t writeEnd() {
175  // default behaviour is to do nothing
176  return 0;
177  }
178 
185  virtual void flush() {
186  // default behaviour is to do nothing
187  }
188 
216  const uint8_t* borrow(uint8_t* buf, uint32_t* len) {
217 
218  return borrow_virt(buf, len);
219  }
220  virtual const uint8_t* borrow_virt(uint8_t* /* buf */, uint32_t* /* len */) {
221  return NULL;
222  }
223 
233  void consume(uint32_t len) {
234 
235  consume_virt(len);
236  }
237  virtual void consume_virt(uint32_t /* len */) {
238  LOG(ERROR, __func__ << ": Base TTransport cannot consume.");
239  assert(false);
240  }
241 
242  protected:
247 };
248 
256  public:
258 
259  virtual ~TTransportFactory() {}
260 
264  virtual boost::shared_ptr<TTransport> getTransport(boost::shared_ptr<TTransport> trans) {
265  return trans;
266  }
267 
268 };
269 
270 }}} // contrail::sandesh::transport
271 
272 #endif // #ifndef _SANDESH_TRANSPORT_TTRANSPORT_H_
virtual void consume_virt(uint32_t)
Definition: TTransport.h:237
virtual boost::shared_ptr< TTransport > getTransport(boost::shared_ptr< TTransport > trans)
Definition: TTransport.h:264
const uint8_t * borrow(uint8_t *buf, uint32_t *len)
Definition: TTransport.h:216
virtual int32_t read_virt(uint8_t *, uint32_t)
Definition: TTransport.h:112
virtual const uint8_t * borrow_virt(uint8_t *, uint32_t *)
Definition: TTransport.h:220
int32_t readAll(uint8_t *buf, uint32_t len)
Definition: TTransport.h:125
virtual int32_t readAll_virt(uint8_t *buf, uint32_t len)
Definition: TTransport.h:129
virtual int write_virt(const uint8_t *, uint32_t)
Definition: TTransport.h:161
int32_t read(uint8_t *buf, uint32_t len)
Definition: TTransport.h:108
int write(const uint8_t *buf, uint32_t len)
Definition: TTransport.h:157
#define LOG(_Level, _Msg)
Definition: logging.h:33
int32_t readAll(Transport_ &trans, uint8_t *buf, uint32_t len)
Definition: TTransport.h:35