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.



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

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.



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

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. Must be an array or nil. If mseq detects true among the tags, then mseq reflects the message: it sends the message back to the sender (minus tags and blob, and with updated global_tick). This is so that a client can send mseq a message with tags it does not subscribe to and know when it has arrived. The true is removed from the tag list before mseq sends it to subscribers.



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

def tags
  @tags
end

Class Method Details

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



34
35
36
37
# File 'lib/funl/message.rb', line 34

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

.control(op_type, *args) ⇒ Object



39
40
41
# File 'lib/funl/message.rb', line 39

def self.control op_type, *args
  Message.new.tap {|m| m.client_id = [op_type, *args]}
end

.from_msgpack(src) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/funl/message.rb', line 111

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



107
108
109
# File 'lib/funl/message.rb', line 107

def self.from_serialized ary
  new *ary
end

Instance Method Details

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



68
69
70
71
72
73
74
75
76
# File 'lib/funl/message.rb', line 68

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

#control?Boolean

Is this a control packet rather than a data packet?

Returns:

  • (Boolean)


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

def control?
  @client_id.kind_of? Array
end

#control_opObject

Array of [op_type, *args] for the control operation.



49
50
51
# File 'lib/funl/message.rb', line 49

def control_op
  @client_id
end

#hashObject



79
80
81
# File 'lib/funl/message.rb', line 79

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

#inspectObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/funl/message.rb', line 53

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



64
65
66
# File 'lib/funl/message.rb', line 64

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

#to_jsonObject



103
104
105
# File 'lib/funl/message.rb', line 103

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/funl/message.rb', line 86

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