0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Protocol.cc
Go to the documentation of this file.
1 
22 #include <Common/Compat.h>
23 
24 #include "CommBuf.h"
25 #include "Event.h"
26 #include "Protocol.h"
27 
28 #include <Common/Error.h>
29 #include <Common/Logger.h>
30 #include <Common/Serialization.h>
31 
32 #include <cassert>
33 #include <iostream>
34 
35 using namespace Hypertable;
36 using namespace Serialization;
37 using namespace std;
38 
39 int32_t Protocol::response_code(const Event *event) {
40  if (event->type == Event::ERROR)
41  return event->error;
42 
43  const uint8_t *msg = event->payload;
44  size_t remaining = event->payload_len;
45 
46  try { return decode_i32(&msg, &remaining); }
47  catch (Exception &e) { return e.code(); }
48 }
49 
50 
52  int error = Error::OK;
53 
54  if (event == 0)
55  return String("NULL event");
56 
57  const uint8_t *msg = event->payload;
58  size_t remaining = event->payload_len;
59 
60  if (event->type != Event::MESSAGE)
61  return event->to_str();
62 
63  try {
64  error = decode_i32(&msg, &remaining);
65 
66  if (error == Error::OK)
67  return Error::get_text(error);
68 
69  uint16_t len;
70  const char *str = decode_str16(&msg, &remaining, &len);
71 
72  return String(str, len > 150 ? 150 : len);
73  }
74  catch (Exception &e) {
75  return format("%s - %s", e.what(), Error::get_text(e.code()));
76  }
77 }
78 
79 
81 Protocol::create_error_message(CommHeader &header, int error, const char *msg){
82  CommBufPtr cbuf = make_shared<CommBuf>(header, 4 + encoded_length_str16(msg));
83  cbuf->append_i32(error);
84  cbuf->append_str16(msg);
85  return cbuf;
86 }
87 
88 
Network communication event.
Definition: Event.h:54
static int32_t response_code(const Event *event)
Returns the response code from an event event generated in response to a request message.
Definition: Protocol.cc:39
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
static String string_format_message(const Event *event)
Returns error message decoded standard error MESSAGE generated in response to a request message...
Definition: Protocol.cc:51
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
STL namespace.
static CommBufPtr create_error_message(CommHeader &header, int error, const char *msg)
Creates a standard error message response.
Definition: Protocol.cc:81
Type type
Type of event.
Definition: Event.h:165
Error event
Definition: Event.h:64
Declarations for Event.
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
size_t encoded_length_str16(const char *str)
Computes the encoded length of a string16 encoding.
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
std::shared_ptr< CommBuf > CommBufPtr
Smart pointer to CommBuf.
Definition: CommBuf.h:305
Logging routines and macros.
Compatibility Macros for C/C++.
Functions to serialize/deserialize primitives to/from a memory buffer.
const char * decode_str16(const uint8_t **bufp, size_t *remainp)
Decodes a c-style string from the given buffer.
Hypertable definitions
Header for messages transmitted via AsyncComm.
Definition: CommHeader.h:40
Declarations for CommBuf.
Declarations for Protocol.
Request/response message event.
Definition: Event.h:63
This is a generic exception class for Hypertable.
Definition: Error.h:314
Error codes, Exception handling, error logging.
int code() const
Returns the error code.
Definition: Error.h:391