Class: Gossiperl::Client::Serialization::Serializer
- Inherits:
-
Object
- Object
- Gossiperl::Client::Serialization::Serializer
- Defined in:
- lib/gossiperl_client/headers.rb,
lib/gossiperl_client/serialization/serializer.rb
Instance Method Summary collapse
- #deserialize(bin_digest) ⇒ Object
- #digest_from_binary(digest_type, bin_digest) ⇒ Object
- #digest_to_binary(digest) ⇒ Object
- #digest_type_class(digest_type) ⇒ Object
- #serialize(digest) ⇒ Object
- #serialize_arbitrary(digest_type, digest_data) ⇒ Object
- #type_to_thrift_type(type) ⇒ Object
Instance Method Details
#deserialize(bin_digest) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gossiperl_client/serialization/serializer.rb', line 45 def deserialize bin_digest envelope_resp = self.digest_from_binary('digestEnvelope', bin_digest) if envelope_resp.has_key?(:ok) = self.digest_type_class( envelope_resp[:ok].payload_type ) if == :forward return { :forward => true, :type => envelope_resp[:ok].payload_type, :envelope => envelope_resp[:ok] } else payload = digest_from_binary( envelope_resp[:ok].payload_type, Base64.strict_decode64( envelope_resp[:ok].bin_payload ) ) if payload.has_key?(:ok) return payload[:ok] else return { :error => :not_thrift } end end end return { :error => :not_thrift } end |
#digest_from_binary(digest_type, bin_digest) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gossiperl_client/serialization/serializer.rb', line 74 def digest_from_binary digest_type, bin_digest begin transport = ::Thrift::MemoryBufferTransport.new( bin_digest ) protocol = ::Thrift::BinaryProtocol.new(transport) digest = self.digest_type_class(digest_type).new digest.read( protocol ) return { :ok => digest } rescue Exception => ex return { :error => ex } end end |
#digest_to_binary(digest) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/gossiperl_client/serialization/serializer.rb', line 67 def digest_to_binary digest transport = ::Thrift::MemoryBufferTransport.new() protocol = ::Thrift::BinaryProtocol.new(transport) digest.write( protocol ) protocol.trans.read( protocol.trans.available ).force_encoding('UTF-8') end |
#digest_type_class(digest_type) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/gossiperl_client/serialization/serializer.rb', line 86 def digest_type_class digest_type types = { 'digestError' => Gossiperl::Client::Thrift::DigestError, 'digestForwardedAck' => Gossiperl::Client::Thrift::DigestForwardedAck, 'digestEnvelope' => Gossiperl::Client::Thrift::DigestEnvelope, 'digest' => Gossiperl::Client::Thrift::Digest, 'digestAck' => Gossiperl::Client::Thrift::DigestAck, 'digestSubscriptions' => Gossiperl::Client::Thrift::DigestSubscriptions, 'digestExit' => Gossiperl::Client::Thrift::DigestExit, 'digestSubscribe' => Gossiperl::Client::Thrift::DigestSubscribe, 'digestSubscribeAck' => Gossiperl::Client::Thrift::DigestSubscribeAck, 'digestUnsubscribe' => Gossiperl::Client::Thrift::DigestUnsubscribe, 'digestUnsubscribeAck' => Gossiperl::Client::Thrift::DigestUnsubscribeAck, 'digestEvent' => Gossiperl::Client::Thrift::DigestEvent } return types[ digest_type ] if types.has_key? digest_type return :forward end |
#serialize(digest) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gossiperl_client/serialization/serializer.rb', line 32 def serialize digest digest_type = digest.class.name.split('::').last digest_type = digest_type[0].downcase + digest_type[1..digest_type.length] if digest_type == 'digestEnvelope' return self.digest_to_binary( envelope ) end envelope = Gossiperl::Client::Thrift::DigestEnvelope.new envelope.payload_type = digest_type envelope.bin_payload = Base64.strict_encode64( self.digest_to_binary( digest ) ) envelope.id = SecureRandom.uuid.to_s self.digest_to_binary( envelope ) end |
#serialize_arbitrary(digest_type, digest_data) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gossiperl_client/serialization/serializer.rb', line 9 def serialize_arbitrary digest_type, digest_data transport = ::Thrift::MemoryBufferTransport.new() protocol = ::Thrift::BinaryProtocol.new(transport) protocol.write_struct_begin(digest_type.to_s) digest_data.each_key{|key| value = digest_data[key][:value] type = self.type_to_thrift_type( digest_data[key][:type] ) unless type.nil? protocol.write_field({ :type => type, :name => key.to_s }, digest_data[key][:field_id], value ) else raise ArgumentError.new("Unsupported serializable type #{digest_data[key][:type]} for field #{key.to_s}.") end } protocol.write_field_stop protocol.write_struct_end envelope = Gossiperl::Client::Thrift::DigestEnvelope.new envelope.payload_type = digest_type.to_s envelope.bin_payload = Base64.strict_encode64( protocol.trans.read( protocol.trans.available ).force_encoding('UTF-8') ) envelope.id = SecureRandom.uuid.to_s self.digest_to_binary( envelope ) end |
#type_to_thrift_type(type) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/gossiperl_client/serialization/serializer.rb', line 105 def type_to_thrift_type type type = type.to_sym serializable_thrift_types = { :bool => ::Thrift::Types::BOOL, :byte => ::Thrift::Types::BYTE, :double => ::Thrift::Types::DOUBLE, :i16 => ::Thrift::Types::I16, :i32 => ::Thrift::Types::I32, :i64 => ::Thrift::Types::I64, :string => ::Thrift::Types::STRING } return (serializable_thrift_types)[ type ] if serializable_thrift_types.has_key? type return nil end |