Class: Xap::XapDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/xap/xap_dev.rb

Overview

Classes that want to receive filtered xAP events from the main event loop should inherit from this class and override receive_message.

Direct Known Subclasses

Schema::XapBscDevice

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, uid, interval) ⇒ XapDevice

Initializes this superclass with the given address and base UID, which will be used as the source address and UID of messages generated by this device, and the given heartbeat interval.

address - a non-wildcarded XapAddress that doesn’t contain ‘:’. uid - eight hexadecimal digits, the first two of which must be FF, the last two 00, and the middle two neither FF nor 00. interval - the number of seconds between xAP heartbeats sent by this device model when added to an XapHandler event loop.



19
20
21
22
23
24
# File 'lib/xap/xap_dev.rb', line 19

def initialize address, uid, interval
	raise 'Heartbeat interval must be a Fixnum or nil' if interval && !interval.is_a?(Fixnum)
	set_address address
	self.uid = uid
	@interval = interval
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



8
9
10
# File 'lib/xap/xap_dev.rb', line 8

def address
  @address
end

#intervalObject

Returns the value of attribute interval.



8
9
10
# File 'lib/xap/xap_dev.rb', line 8

def interval
  @interval
end

#uidObject

Returns the value of attribute uid.



8
9
10
# File 'lib/xap/xap_dev.rb', line 8

def uid
  @uid
end

Instance Method Details

#handler=(handler) ⇒ Object

Sets the XapHandler that manages messages to and from this device. This should typically be called by XapHandler itself. Subclasses may override this method in order to send messages that are supposed to be sent on initialization, so long as they call this base implementation first. XapHandler will call this method with nil if the device is removed from the handler or the xAP socket is closed.



32
33
34
35
# File 'lib/xap/xap_dev.rb', line 32

def handler= handler
	raise 'handler must be a XapHandler' unless handler.nil? || handler.is_a?(XapHandler)
	@handler = handler
end

#receive_message(msg) ⇒ Object

Called whenever a matching message is received by the associated handler.



39
40
41
# File 'lib/xap/xap_dev.rb', line 39

def receive_message msg
	puts "XXX: You forgot to override receive_message in #{self}: #{msg.inspect.lines.to_a.join("\t")}"
end

#to_sObject

Returns a string description of this device.



44
45
46
# File 'lib/xap/xap_dev.rb', line 44

def to_s
	"<#{self.class.name}: #{@address} #{@uid}>"
end