OpenSDN source code
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  * Modifications Copyright (c) Juniper Networks, Inc.
20  * Copyright Contributors to the OpenSDN project
21  */
22 
23 #ifndef _SANDESH_TRANSPORT_TTRANSPORT_H_
24 #define _SANDESH_TRANSPORT_TTRANSPORT_H_ 1
25 
26 #include <stdint.h>
27 #include <string>
28 #include <boost/shared_ptr.hpp>
29 
30 #include <base/logging.h>
31 
32 namespace contrail { namespace sandesh { namespace transport {
33 
37 template <class Transport_>
38 int32_t readAll(Transport_ &trans, uint8_t* buf, uint32_t len) {
39  uint32_t have = 0;
40  uint32_t get = 0;
41 
42  while (have < len) {
43  get = trans.read(buf+have, len-have);
44  if (get <= 0) {
45  LOG(ERROR, __func__ << ": No more data to read");
46  return -1;
47  }
48  have += get;
49  }
50 
51  return have;
52 }
53 
54 
60 class TTransport {
61  public:
65  virtual ~TTransport() {}
66 
70  virtual bool isOpen() {
71  return false;
72  }
73 
82  virtual bool peek() {
83  return isOpen();
84  }
85 
91  virtual void open() {
92  LOG(ERROR, __func__ << ": Cannot open base TTransport.");
93  assert(false);
94  }
95 
99  virtual void close() {
100  LOG(ERROR, __func__ << ": Cannot close base TTransport.");
101  assert(false);
102  }
103 
111  int32_t read(uint8_t* buf, uint32_t len) {
112 
113  return read_virt(buf, len);
114  }
115  virtual int32_t read_virt(uint8_t* /* buf */, uint32_t /* len */) {
116  LOG(ERROR, __func__ << ": Base TTransport cannot read.");
117  assert(false);
118  return -1;
119  }
120 
128  int32_t readAll(uint8_t* buf, uint32_t len) {
129 
130  return readAll_virt(buf, len);
131  }
132  virtual int32_t readAll_virt(uint8_t* buf, uint32_t len) {
133  return contrail::sandesh::transport::readAll(*this, buf, len);
134  }
135 
143  virtual uint32_t readEnd() {
144  // default behaviour is to do nothing
145  return 0;
146  }
147 
160  int write(const uint8_t* buf, uint32_t len) {
161 
162  return write_virt(buf, len);
163  }
164  virtual int write_virt(const uint8_t* /* buf */, uint32_t /* len */) {
165  LOG(ERROR, __func__ << ": Base TTransport cannot write.");
166  assert(false);
167  return -1;
168  }
169 
177  virtual uint32_t writeEnd() {
178  // default behaviour is to do nothing
179  return 0;
180  }
181 
188  virtual void flush() {
189  // default behaviour is to do nothing
190  }
191 
219  const uint8_t* borrow(uint8_t* buf, uint32_t* len) {
220 
221  return borrow_virt(buf, len);
222  }
223  virtual const uint8_t* borrow_virt(uint8_t* /* buf */, uint32_t* /* len */) {
224  return NULL;
225  }
226 
236  void consume(uint32_t len) {
237 
238  consume_virt(len);
239  }
240  virtual void consume_virt(uint32_t /* len */) {
241  LOG(ERROR, __func__ << ": Base TTransport cannot consume.");
242  assert(false);
243  }
244 
245  protected:
250 };
251 
259  public:
261 
262  virtual ~TTransportFactory() {}
263 
267  virtual boost::shared_ptr<TTransport> getTransport(boost::shared_ptr<TTransport> trans) {
268  return trans;
269  }
270 
271 };
272 
273 }}} // contrail::sandesh::transport
274 
275 #endif // #ifndef _SANDESH_TRANSPORT_TTRANSPORT_H_
virtual boost::shared_ptr< TTransport > getTransport(boost::shared_ptr< TTransport > trans)
Definition: TTransport.h:267
const uint8_t * borrow(uint8_t *buf, uint32_t *len)
Definition: TTransport.h:219
virtual int write_virt(const uint8_t *, uint32_t)
Definition: TTransport.h:164
int32_t readAll(uint8_t *buf, uint32_t len)
Definition: TTransport.h:128
virtual const uint8_t * borrow_virt(uint8_t *, uint32_t *)
Definition: TTransport.h:223
virtual int32_t read_virt(uint8_t *, uint32_t)
Definition: TTransport.h:115
int write(const uint8_t *buf, uint32_t len)
Definition: TTransport.h:160
int32_t read(uint8_t *buf, uint32_t len)
Definition: TTransport.h:111
virtual void consume_virt(uint32_t)
Definition: TTransport.h:240
virtual int32_t readAll_virt(uint8_t *buf, uint32_t len)
Definition: TTransport.h:132
#define LOG(_Level, _Msg)
Definition: logging.h:35
int32_t readAll(Transport_ &trans, uint8_t *buf, uint32_t len)
Definition: TTransport.h:38
Definition: regex.h:11