PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/ruby/binding-ruby/src/main/ruby/transport/packetizer.rb

#
Ruby | 187 lines | 153 code | 34 blank | 0 comment | 36 complexity | ae25d3305be74bad3319e6852692fac6 MD5 | raw file
Possible License(s): Apache-2.0
  1. require 'socket'
  2. require 'etch/bindings/ruby/transport/who.rb'
  3. require 'etch/bindings/ruby/transport/flex_buffer.rb'
  4. require 'etch/bindings/ruby/transport/data_type_values.rb'
  5. class Packetizer
  6. include DataHandler, PacketSource, DataTypeValues
  7. attr :handler, :maxPktSize, :dataSrc
  8. private :handler, :maxPktSize, :dataSrc
  9. @savedBuf = FlexBuffer.new
  10. $DEFAULT_MAX_PKT_SIZE = 10240
  11. $USE_DEFAULT_MAX_PKT_SIZE = -1
  12. $USE_UNLIMITED_MAX_PKT_SIZE = 0
  13. $HEADER_SIZE = 8
  14. $SIG = 0xdeadbeef
  15. def initialize( handler, maxPktSize )
  16. @handler = handler
  17. if ( maxPktSize == $USE_DEFAULT_MAX_PKT_SIZE )
  18. maxPktSize = $DEFAULT_MAX_PKT_SIZE
  19. else if ( maxPktSize == $USE_UNLIMITED_MAX_PKT_SIZE )
  20. maxPktSize = $INTEGER_MAX_VALUE
  21. @maxPktSize = maxPktSize
  22. end
  23. end
  24. def Packetizer( handler )
  25. initialize( handler, $USE_DEFAULT_MAX_PKT_SIZE )
  26. end
  27. def Packetizer( maxPktSize )
  28. initialize( nil, maxPktSize )
  29. end
  30. def Packetizer()
  31. initialize( nil, $DEFAULT_MAX_PKT_SIZE )
  32. end
  33. def setHandler( handler )
  34. @handler = handler
  35. end
  36. def to_s
  37. str.to_str( "Packetizer/%s", dataSrc )
  38. end
  39. def setDataSource( src )
  40. if( src == nil || @dataSrc == nil )
  41. @dataSrc = src
  42. else if( src != @dataSrc )
  43. raise "IllegalArgumentException"
  44. end
  45. end
  46. def data( src, sender, buf = FlexBuffer.new )
  47. setDataSource( src )
  48. while( FlexBuffer.avail() > 0 )
  49. if( @wantHeader )
  50. if( @savedBuf.length() + buf.avail() >= HEADER_SIZE )
  51. if( @savedBuf.length() == 0 )
  52. sig = buf.getint()
  53. if( sig != $SIG )
  54. raise "IOException"
  55. end
  56. len = buf.getInt()
  57. if( len == 0 )
  58. continue
  59. end
  60. if( len < 0 || len > @maxPktSize )
  61. raise "IOException (len < 0 || len > maxPktSize) "
  62. end
  63. @bodyLen = len
  64. @wantHeader = false
  65. else
  66. needFromBuf = $HEADER_SIZE - @savedBuf.length()
  67. @savedBuf.put( buf, needFromBuf )
  68. @savedBuf.setIndex( 0 )
  69. sig = @savedBuf.getInt()
  70. if( sig != $SIG )
  71. raise "IOException bad SIG"
  72. end
  73. len = @savedBuf.length()
  74. @savedBuf.reset()
  75. if( len == 0 )
  76. continue
  77. end
  78. if (len < 0 || len > maxPktSize)
  79. raise " IOException len < 0 || len > maxPktSize"
  80. end
  81. @bodyLen = len
  82. @wantHeader = false
  83. end
  84. else
  85. @savedBuf.setIndex( @savedBuf.length() )
  86. @savedBuf.put( buf )
  87. end
  88. else if( @savedBuf.length() + buf.avail() >= bodyLen)
  89. assert savedBuf.length() < bodyLen
  90. if( @savedBuf.length() == 0 )
  91. length = buf.length();
  92. index = buf.index();
  93. buf.setLength( index + @bodyLen );
  94. @handler.packet( this, sender, buf );
  95. buf.setLength( length );
  96. buf.setIndex( index + @bodyLen );
  97. @wantHeader = true;
  98. else
  99. needFromBuf = @bodyLen - @savedBuf.length()
  100. @savedBuf.put( buf, needFromBuf )
  101. @savedBuf.setIndex( 0 )
  102. @handler.packet( this, sender, @savedBuf )
  103. @savedBuf.reset()
  104. @wantHeader = true
  105. end
  106. else
  107. @savedBuf.put( buf )
  108. end
  109. end
  110. end
  111. end
  112. end
  113. end
  114. def firePacket( sender, buf )
  115. @handler.packet( this, sender, buf )
  116. end
  117. def packet( recipient = who.new, buf = FlexBuffer.new )
  118. len = buf.avail()
  119. if (len < $HEADER_SIZE)
  120. raise "IllegalArgumentException( \"len < HEADER_SIZE\" )"
  121. end
  122. index = buf.index()
  123. buf.putInt( $SIG )
  124. buf.putInt( len - $HEADER_SIZE )
  125. buf.setIndex( index )
  126. @dataSrc.data( recipient, buf )
  127. end
  128. def headerSize()
  129. return $HEADER_SIZE
  130. end
  131. def up( src )
  132. setDataSource( src )
  133. @handler.down( this ) # this?
  134. end
  135. def close( reset )
  136. @dataSrc.close( reset )
  137. end
  138. def localAddress()
  139. return @dataSrc.localAddress()
  140. end
  141. def remoteAddress()
  142. return @dataSrc.remoteAddress()
  143. end
  144. def shutdownInput()
  145. @dataSrc.shutdownInput()
  146. end
  147. def shutdownOutput()
  148. @dataSrc.shutdownOutput()
  149. end
  150. def stop()
  151. @dataSrc.stop()
  152. end
  153. end