libbgp  0.6
A C++ BGP Library.
serializable.h
Go to the documentation of this file.
1 
11 #ifndef SERIALIZABLE_H_
12 #define SERIALIZABLE_H_
13 #include <stdint.h>
14 #include <unistd.h>
15 #include <stdarg.h>
16 #include "bgp-log-handler.h"
17 
18 namespace libbgp {
19 
20 class BgpLogHandler;
21 
26 class Serializable {
27 public:
28  Serializable(BgpLogHandler *logger);
29  ~Serializable();
30 
31  // print the Serializable object as human readable string.
32  ssize_t print(uint8_t *to, size_t buf_sz) const;
33 
34  // print the Serializable object as human readable string, pre-indented.
35  ssize_t print(size_t indent, uint8_t *to, size_t buf_sz) const;
36 
47  virtual ssize_t parse(const uint8_t *from, size_t msg_sz) = 0;
48 
59  virtual ssize_t write(uint8_t *to, size_t buf_sz) const = 0;
60 
61  virtual ssize_t length() const;
62 
63  bool hasError() const;
64 
65  // get error code
66  uint8_t getErrorCode() const;
67 
68  // get error subcode
69  uint8_t getErrorSubCode() const;
70 
71  // get error payload (data field of NOTIFICATION message)
72  const uint8_t* getError() const;
73 
74  // get length of error payload
75  size_t getErrorLength() const;
76 
77  // replace logger
78  void setLogger(BgpLogHandler *logger);
79 
80 protected:
81  // print helper. print string with format to pointer to buffer. will change
82  // buffer pointer and buf_left after write. return bytes written
83  static ssize_t _print(size_t indent, uint8_t **to, size_t *buf_left, const char* format, ...);
84 
95  virtual ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const = 0;
96 
97  // utility function to error related values
98  void setError(uint8_t err, uint8_t suberr, const uint8_t *data, size_t data_len);
99 
100  // utility function to forward attribute parse error to here
101  void forwardParseError(const Serializable &other);
102 
103  uint8_t err_code;
104  uint8_t err_subcode;
105  size_t err_len;
106  uint8_t *err_data;
107  BgpLogHandler *logger;
108 };
109 
110 }
111 
112 #endif // SERIALIZABLE_H_
virtual ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const =0
Print implementation.
virtual ssize_t length() const
Get size in bytes required to serialize the object.
static ssize_t _print(size_t indent, uint8_t **to, size_t *buf_left, const char *format,...)
Print helper.
bool hasError() const
Check if error information available.
Definition: serializable.cc:45
~Serializable()
Destroy the Serializable:: Serializable object.
Definition: serializable.cc:35
The serializable base class.
Definition: serializable.h:26
ssize_t print(uint8_t *to, size_t buf_sz) const
Print the Serializable object as human readable string.
virtual ssize_t write(uint8_t *to, size_t buf_sz) const =0
Serialize the object and write to buffer.
void setLogger(BgpLogHandler *logger)
Replace logger for this object.
void setError(uint8_t err, uint8_t suberr, const uint8_t *data, size_t data_len)
Set the error information.
Definition: serializable.cc:61
Definition: bgp-afi.h:14
The BgpLogHandler class.
BGP log handler.
virtual ssize_t parse(const uint8_t *from, size_t msg_sz)=0
Deserialize the object from buffer.
Serializable(BgpLogHandler *logger)
Construct a new Serializable:: Serializable object.
Definition: serializable.cc:23
void forwardParseError(const Serializable &other)
Forward error information from other Serializable object.
Definition: serializable.cc:97