Class: Trema::Error

Inherits:
Object
  • Object
show all
Defined in:
ruby/trema/error.c

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Error

Examples:

Error.new(
  :type => OFPET_BAD_REQUEST,
  :code => OFPBRC_BAD_TYPE,
)
Error.new(
  :type => OFPET_BAD_REQUEST,
  :code => OFPBRC_BAD_TYPE,
  :transcation_id => 123
)
Error.new(
  :type => OFPET_BAD_REQUEST,
  :code => OFPBRC_BAD_TYPE,
  :transcation_id => 123
  :data => "Error!!"
)

Parameters:

  • options (Hash)

    the options to create a message with.

Options Hash (options):

  • :type (Number)

    a command or action that failed.

  • :code (Number)

    the reason of the failed type error.

  • :data (String)

    a more user friendly explanation of the error. Defaults to nil if not specified.

  • :xid (Number)
  • :transaction_id (Number)

    An unsigned 32bit integer number associated with this message. If not specified, an auto-generated value is set.

Raises:

  • (ArgumentError)

    if transaction ID is not an unsigned 32bit integer.

  • (ArgumentError)

    if type and code are not supplied.

  • (ArgumentError)

    if user data is not a string.

  • (TypeError)

    if options is not a hash.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ruby/trema/error.c', line 72

static VALUE
error_init( int argc, VALUE *argv, VALUE self ) {
  buffer *error = NULL;
  Data_Get_Struct( self, buffer, error );
  VALUE options;

  if ( rb_scan_args( argc, argv, "01", &options ) == 1 ) {
    Check_Type( options, T_HASH );
    VALUE tmp = Qnil;

    tmp = rb_hash_aref( options, ID2SYM( rb_intern( "type" ) ) );
    if ( tmp != Qnil ) {
      ( ( struct ofp_error_msg * ) error->data )->type = htons( ( uint16_t ) NUM2UINT( tmp ) );
    }
    else {
      rb_raise( rb_eArgError, "Type is a mandatory option" );
    }

    tmp = rb_hash_aref( options, ID2SYM( rb_intern( "code" ) ) );
    if ( tmp != Qnil ) {
      ( ( struct ofp_error_msg * ) error->data )->code = htons( ( uint16_t ) NUM2UINT( tmp ) );
    }
    else {
      rb_raise( rb_eArgError, "Code is a mandatory option" );
    }

    VALUE xid = Qnil;
    tmp = rb_hash_aref( options, ID2SYM( rb_intern( "transaction_id" ) ) );
    if ( tmp != Qnil ) {
      xid = tmp;
    }
    tmp = rb_hash_aref( options, ID2SYM( rb_intern( "xid" ) ) );
    if ( tmp != Qnil ) {
      xid = tmp;
    }
    if ( xid != Qnil ) {
      validate_xid( xid );
      set_xid( error, ( uint32_t ) NUM2UINT( xid ) );
    }
    else {
      set_xid( error, get_transaction_id() );
    }

    VALUE data = rb_hash_aref( options, ID2SYM( rb_intern( "data" ) ) );
    if ( data != Qnil ) {
      Check_Type( data, T_STRING );
      uint16_t length = ( uint16_t ) RSTRING_LEN( data );
      append_back_buffer( error, length );
      ( ( struct ofp_header * ) ( error->data ) )->length = htons( ( uint16_t ) ( offsetof( struct ofp_error_msg, data ) + length ) );
      memcpy( ( char * ) error->data + offsetof( struct ofp_error_msg, data ), RSTRING_PTR( data ), length );
    }
  }
  else {
    rb_raise( rb_eArgError, "Type and code are mandatory options" );
  }

  return self;
}

Instance Method Details

#codeNumber

Reason of the failed type error.

Returns:

  • (Number)

    the value of error code.



189
190
191
192
193
# File 'ruby/trema/error.c', line 189

static VALUE
error_code( VALUE self ) {
  struct ofp_error_msg *error = get_error( self );
  return UINT2NUM( ntohs( error->code ) );
}

#dataString?

An optional user data payload field, possibly detailed explanation of the error.

Returns:

  • (String)

    user data payload is set.

  • (nil)

    user data payload is not set.



159
160
161
162
163
164
165
166
167
168
169
# File 'ruby/trema/error.c', line 159

static VALUE
error_data( VALUE self ) {
  struct ofp_error_msg *error = get_error( self );
  long length = ( long ) ( ntohs( error->header.length ) - sizeof( struct ofp_error_msg ) );
  if ( length > 0 ) {
    return rb_str_new( ( char * ) error->data, length );
  }
  else {
    return Qnil;
  }
}

#error_typeNumber

Indicates the command or action that failed.

Returns:

  • (Number)

    the value of error type.



177
178
179
180
181
# File 'ruby/trema/error.c', line 177

static VALUE
error_type( VALUE self ) {
  struct ofp_error_msg *error = get_error( self );
  return UINT2NUM( ntohs( error->type ) );
}

#transaction_idNumber

Transaction ids, message sequence numbers matching requests to replies.

Returns:

  • (Number)

    the value of transaction id.



145
146
147
148
149
150
# File 'ruby/trema/error.c', line 145

static VALUE
error_transaction_id( VALUE self ) {
  struct ofp_error_msg *error = get_error( self );
  uint32_t xid = ntohl( error->header.xid );
  return UINT2NUM( xid );
}