ruby/trema/features-request.c in trema-0.2.2.1 vs ruby/trema/features-request.c in trema-0.2.3

- old
+ new

@@ -1,8 +1,6 @@ /* - * Author: Yasuhito Takamiya <yasuhito@gmail.com> - * * Copyright (C) 2008-2012 NEC Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 2, as * published by the Free Software Foundation. @@ -16,12 +14,13 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "trema.h" #include "ruby.h" +#include "trema-ruby-utils.h" +#include "trema.h" extern VALUE mTrema; VALUE cFeaturesRequest; @@ -31,74 +30,101 @@ buffer *features_request = create_features_request( 0 ); return Data_Wrap_Struct( klass, NULL, free_buffer, features_request ); } -/* - * A features request message is sent upon TLS session establishment to obtain - * switch's supported capabilities. Creates an object that encapsulates the - * +OFPT_FEATURES_REQUEST+ OpenFlow message. +/* + * Creates a FeaturesRequest OpenFlow message. * - * @overload initialize(options={}) + * @overload initialize() * @example - * FeaturesRequeset.new - * FeaturesRequest.new( :transaction_id => 123 ) + * FeaturesRequest.new * + * @overload initialize(transaction_id) + * @example + * FeaturesRequest.new( 123 ) + * @param [Integer] transaction_id + * An unsigned 32bit integer number associated with this message. + * + * @overload initialize(options) + * @example + * FeaturesRequest.new( :xid => 123 ) + * FeaturesRequest.new( :transaction_id => 123 ) * @param [Hash] options * the options to create a message with. + * @option options [Number] :xid + * @option options [Number] :transaction_id + * An unsigned 32bit integer number associated with this message. + * If not specified, an auto-generated value is set. * - * @option options [Symbol] :transaction_id - * any positive number, same value should be attached to the +OFPT_FEATURES_REPLY+ - * message. If not specified is auto-generated. - * - * @raise [ArgumentError] if transaction id is not an unsigned 32-bit integer. - * @raise [TypeError] if options is not a hash. - * - * @return [FeaturesRequest] - * an object that encapsulates the +OFPT_FEATURES_REQUEST+ OpenFlow message. + * @raise [ArgumentError] if transaction ID is not an unsigned 32-bit integer. + * @raise [TypeError] if argument is not a Integer or a Hash. + * @return [FeaturesRequest] */ static VALUE features_request_init( int argc, VALUE *argv, VALUE self ) { - buffer *features_request; + buffer *features_request = NULL; Data_Get_Struct( self, buffer, features_request ); - uint32_t xid = get_transaction_id(); - VALUE options; + VALUE options = Qnil; - if ( rb_scan_args( argc, argv, "01", &options ) == 1 ) { - Check_Type( options, T_HASH ); - VALUE xid_ruby; - if ( ( xid_ruby = rb_hash_aref( options, ID2SYM( rb_intern( "transaction_id" ) ) ) ) != Qnil ) { - if ( rb_funcall( xid_ruby, rb_intern( "unsigned_32bit?" ), 0 ) == Qfalse ) { - rb_raise( rb_eArgError, "Transaction ID must be an unsigned 32-bit integer" ); + if ( rb_scan_args( argc, argv, "01", &options ) == 0 ) { + set_xid( features_request, get_transaction_id() ); + } + else { + if ( options == Qnil ) { + set_xid( features_request, get_transaction_id() ); + } + else if ( rb_obj_is_kind_of( options, rb_cInteger ) == Qtrue ) { + validate_xid( options ); + set_xid( features_request, ( uint32_t ) NUM2UINT( options ) ); + } + else { + Check_Type( options, T_HASH ); + VALUE tmp = Qnil; + VALUE xid = Qnil; + + tmp = rb_hash_aref( options, ID2SYM( rb_intern( "transaction_id" ) ) ); + if ( tmp != Qnil ) { + xid = tmp; } - xid = ( uint32_t ) NUM2UINT( xid_ruby ); + tmp = rb_hash_aref( options, ID2SYM( rb_intern( "xid" ) ) ); + if ( tmp != Qnil ) { + xid = tmp; + } + + if ( xid != Qnil ) { + validate_xid( xid ); + set_xid( features_request, ( uint32_t ) NUM2UINT( xid ) ); + } + else { + set_xid( features_request, get_transaction_id() ); + } } } - ( ( struct ofp_header * ) ( features_request->data ) )->xid = htonl( xid ); + return self; } /* - * Transaction ids, message sequence numbers matching requests to replies. + * Transaction ids, message sequence numbers matching requests to + * replies. * - * @return [Number] the value of transaction id. + * @return [Number] the value of transaction ID. */ static VALUE features_request_transaction_id( VALUE self ) { - buffer *features_request; - Data_Get_Struct( self, buffer, features_request ); - uint32_t xid = ntohl( ( ( struct ofp_header * ) ( features_request->data ) )->xid ); - return UINT2NUM( xid ); + return get_xid( self ); } void Init_features_request() { cFeaturesRequest = rb_define_class_under( mTrema, "FeaturesRequest", rb_cObject ); rb_define_alloc_func( cFeaturesRequest, features_request_alloc ); rb_define_method( cFeaturesRequest, "initialize", features_request_init, -1 ); rb_define_method( cFeaturesRequest, "transaction_id", features_request_transaction_id, 0 ); + rb_alias( cFeaturesRequest, rb_intern( "xid" ), rb_intern( "transaction_id" ) ); } /* * Local variables: