Class: Ahoy::User

Inherits:
Object
  • Object
show all
Defined in:
lib/ahoy/user.rb

Overview

Ahoy::User represents us, or the current system, and is the entry point for using the Ahoy library.

Send a message to a specific example:

user = Ahoy::User.new("Ford")
user.

chat = user.chat(user.contacts[/Arthur/])
chat.send("Don't panic")

Simple echo server:

user = Ahoy::User.new("echo")
user.

user.on_chat do |chat|
  chat.on_reply do |reply|
    chat.send(reply)
  end
end.join

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(display_name, location = "nowhere", domain = "local.") ⇒ User

:call-seq: User.new(name, location=“nowhere”, domain=“local.”) -> user

Create a new Ahoy::User.

Location should be set to the bonjour/zeroconf hostname.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ahoy/user.rb', line 38

def initialize(display_name, location="nowhere", domain="local.")
  @display_name = display_name
  @short_name = display_name.downcase.gsub(/ /, "-").gsub(/[^a-z0-9-]/, "")
  @location = location.downcase.gsub(/ /, "-").gsub(/[^a-z0-9-]/, "")
  @domain = domain
  
  @contacts = Ahoy::ContactList.new(name)
  
  @port = 5562
  @flags = 0
  @interface = DNSSD::InterfaceAny
end

Instance Attribute Details

#contactsObject (readonly)

Returns the value of attribute contacts.



29
30
31
# File 'lib/ahoy/user.rb', line 29

def contacts
  @contacts
end

#display_nameObject (readonly)

Returns the value of attribute display_name.



29
30
31
# File 'lib/ahoy/user.rb', line 29

def display_name
  @display_name
end

#domainObject (readonly)

Returns the value of attribute domain.



29
30
31
# File 'lib/ahoy/user.rb', line 29

def domain
  @domain
end

#flagsObject

Returns the value of attribute flags.



30
31
32
# File 'lib/ahoy/user.rb', line 30

def flags
  @flags
end

#interfaceObject

Returns the value of attribute interface.



30
31
32
# File 'lib/ahoy/user.rb', line 30

def interface
  @interface
end

#locationObject (readonly)

Returns the value of attribute location.



29
30
31
# File 'lib/ahoy/user.rb', line 29

def location
  @location
end

#portObject

Returns the value of attribute port.



30
31
32
# File 'lib/ahoy/user.rb', line 30

def port
  @port
end

#short_nameObject (readonly)

Returns the value of attribute short_name.



29
30
31
# File 'lib/ahoy/user.rb', line 29

def short_name
  @short_name
end

Instance Method Details

#chat(contact) ⇒ Object

:call-seq: user.chat(contact) -> chat

Initiate a new chat session with contact.



79
80
81
82
# File 'lib/ahoy/user.rb', line 79

def chat(contact)
  chat = Ahoy::Chat.new(contact.name)
  chat.connect(contact.target, contact.port(true))
end

#listenObject

:call-seq: user.listen -> chat

Listen for an incoming chat. This method will block until a chat is recieved.



89
90
91
92
93
# File 'lib/ahoy/user.rb', line 89

def listen
  socket = server.accept
  domain, port, hostname, ip = socket.peeraddr
  Ahoy::Chat.new(contacts.find_by_ip(ip).name).connect(socket)
end

#nameObject

:call-seq: user.name -> string

The user’s name, in name@location format.



55
56
57
# File 'lib/ahoy/user.rb', line 55

def name
  "#{short_name}@#{location}"
end

#on_chat(&block) ⇒ Object

:call-seq: user.on_chat {|chat| block } -> thread

Set up block as a callback for when a chat is initiated by a contact.

This method does not block, but does return a thread, which can be joined if you wish to block.



102
103
104
105
106
107
108
# File 'lib/ahoy/user.rb', line 102

def on_chat(&block)
  Thread.new do
    while chat = listen
      block.call(chat)
    end
  end
end

#sign_in(status = "avail", msg = nil) ⇒ Object

:call-seq: user.sign_in(status=“avail”, msg=nil) -> user

Register user as ‘on-line’ and available to send/receive messages.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ahoy/user.rb', line 63

def (status="avail", msg=nil)
  @registrar = DNSSD.register(
    name,
    Ahoy::SERVICE_TYPE,
    domain,
    port,
    txt_record(status, msg),
    flags.to_i,
    interface)
  self
end