Class: Funl::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/funl/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Message

Returns a new instance of Message.



24
25
26
# File 'lib/funl/message.rb', line 24

def initialize(*args)
  @client_id, @local_tick, @global_tick, @delta, @tags, @blob = *args
end

Instance Attribute Details

#blobObject

Application-defined payload data. See blobber.rb.



22
23
24
# File 'lib/funl/message.rb', line 22

def blob
  @blob
end

#client_idObject

Unique (per funl instance) sequential id of client who sent message.



4
5
6
# File 'lib/funl/message.rb', line 4

def client_id
  @client_id
end

#deltaObject

In client request, how far ahead of ack this message is. When messages are pipelined, delta > 1.



16
17
18
# File 'lib/funl/message.rb', line 16

def delta
  @delta
end

#global_tickObject

Global sequential id of message, unique in scope of funl instance. In client request to funl, this means last ack-ed global tick. Assummed to be 64 bits, to avoid rollover errors.



12
13
14
# File 'lib/funl/message.rb', line 12

def global_tick
  @global_tick
end

#local_tickObject

Client’s sequential id of message, unique only in client scope.



7
8
9
# File 'lib/funl/message.rb', line 7

def local_tick
  @local_tick
end

#tagsObject

Application-defined metadata. May be used for filtering etc.



19
20
21
# File 'lib/funl/message.rb', line 19

def tags
  @tags
end

Class Method Details

.[](client: nil, local: nil, global: nil, delta: nil, tags: nil, blob: nil) ⇒ Object



28
29
30
31
# File 'lib/funl/message.rb', line 28

def self.[](
  client: nil, local: nil, global: nil, delta: nil, tags: nil, blob: nil)
  new client, local, global, delta, tags, blob
end

.from_msgpack(src) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/funl/message.rb', line 91

def self.from_msgpack(src)
  case src
  when MessagePack::Unpacker
    new(*src.read)
  
  when IO, StringIO
    from_msgpack(MessagePack::Unpacker.new(src))
  
  else # String
    from_msgpack(MessagePack::Unpacker.new.feed(src))
  end
end

.from_serialized(ary) ⇒ Object



87
88
89
# File 'lib/funl/message.rb', line 87

def self.from_serialized ary
  new *ary
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



48
49
50
51
52
53
54
55
56
# File 'lib/funl/message.rb', line 48

def == other
  other.kind_of? Message and
    @client_id = other.client_id and
    @local_tick = other.local_tick and
    @global_tick = other.global_tick and
    @delta = other.delta and
    @tags = other.tags and
    @blob = other.blob
end

#hashObject



59
60
61
# File 'lib/funl/message.rb', line 59

def hash
  @client_id.hash ^ @local_tick.hash ^ @global_tick.hash
end

#inspectObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/funl/message.rb', line 33

def inspect
  d = delta ? "+#{delta}" : nil
  t = tags ? " #{tags}" : nil
  s = [
    "client #{client_id}",
    "local #{local_tick}",
    "global #{global_tick}#{d}"
  ].join(", ")
  "<Message: #{s}#{t}>"
end

#to_aObject



44
45
46
# File 'lib/funl/message.rb', line 44

def to_a
  [@client_id, @local_tick, @global_tick, @delta, @tags, @blob]
end

#to_jsonObject



83
84
85
# File 'lib/funl/message.rb', line 83

def to_json
  to_a.to_json
end

#to_msgpack(pk = nil) ⇒ Object

Call with Packer, nil, or IO. If pk is nil, returns string. If pk is a Packer, returns the Packer, which will need to be flushed. If pk is IO, returns nil.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/funl/message.rb', line 66

def to_msgpack(pk = nil)
  case pk
  when MessagePack::Packer
    pk.write_array_header(6)
    pk.write @client_id
    pk.write @local_tick
    pk.write @global_tick
    pk.write @delta
    pk.write @tags
    pk.write @blob
    return pk
  
  else # nil or IO
    MessagePack.pack(self, pk)
  end
end