ahoy 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ahoy/broadcast.rb +1 -2
- data/lib/ahoy/chat.rb +34 -7
- data/lib/ahoy/user.rb +1 -1
- data/readme.rdoc +72 -0
- metadata +3 -2
data/lib/ahoy/broadcast.rb
CHANGED
@@ -2,8 +2,7 @@ require File.expand_path("#{File.dirname(__FILE__)}/../ahoy")
|
|
2
2
|
|
3
3
|
module Ahoy
|
4
4
|
class Broadcast
|
5
|
-
def initialize(
|
6
|
-
user = Ahoy::User.new(name, location, domain)
|
5
|
+
def initialize(user)
|
7
6
|
user.sign_in
|
8
7
|
sleep 1
|
9
8
|
@chats = user.contacts.map {|contact| user.chat(contact)}
|
data/lib/ahoy/chat.rb
CHANGED
@@ -4,8 +4,9 @@ require File.expand_path("#{File.dirname(__FILE__)}/xmpp4r_hack")
|
|
4
4
|
|
5
5
|
module Ahoy
|
6
6
|
class Chat
|
7
|
-
attr_reader :user, :contact
|
8
|
-
|
7
|
+
attr_reader :user, :contact
|
8
|
+
attr_accessor :client
|
9
|
+
protected :client, :client=
|
9
10
|
|
10
11
|
def initialize(user, contact)
|
11
12
|
@user = user
|
@@ -28,7 +29,7 @@ module Ahoy
|
|
28
29
|
message = Jabber::Message.new(contact.name, message)
|
29
30
|
message.type = :chat
|
30
31
|
begin
|
31
|
-
|
32
|
+
client.send(message)
|
32
33
|
rescue IOError
|
33
34
|
connect
|
34
35
|
retry
|
@@ -36,19 +37,45 @@ module Ahoy
|
|
36
37
|
message
|
37
38
|
end
|
38
39
|
|
40
|
+
def on_reply(&block)
|
41
|
+
start unless client
|
42
|
+
client.delete_message_callback("on_reply")
|
43
|
+
|
44
|
+
client.add_message_callback(0, "on_reply") do |message|
|
45
|
+
block.call(message.body) if message.type == :chat
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def receive
|
50
|
+
start unless client
|
51
|
+
thread = Thread.current
|
52
|
+
reply = nil
|
53
|
+
|
54
|
+
client.add_message_callback(0, "receive") do |message|
|
55
|
+
if message.type == :chat
|
56
|
+
reply = message.body
|
57
|
+
thread.run
|
58
|
+
end
|
59
|
+
end
|
60
|
+
Thread.stop
|
61
|
+
|
62
|
+
client.delete_message_callback("receive")
|
63
|
+
reply
|
64
|
+
end
|
65
|
+
|
39
66
|
def close
|
40
|
-
|
41
|
-
|
67
|
+
client.close
|
68
|
+
self.client = nil
|
42
69
|
end
|
43
70
|
|
44
71
|
private
|
45
72
|
def connect
|
46
73
|
contact.resolve
|
47
74
|
|
48
|
-
|
75
|
+
self.client = Jabber::Client.new(Jabber::JID.new(user.name))
|
49
76
|
sleep 0.5
|
50
77
|
begin
|
51
|
-
|
78
|
+
client.connect(contact.target, contact.port, user.contact.ip)
|
52
79
|
rescue Errno::ECONNREFUSED
|
53
80
|
raise Ahoy::ContactOfflineError.new("Contact Offline")
|
54
81
|
end
|
data/lib/ahoy/user.rb
CHANGED
data/readme.rdoc
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
=Ahoy
|
2
|
+
Serverless Messaging using Bonjour/DNSDS/mDNS, XMPP, and Ruby
|
3
|
+
|
4
|
+
Wha?
|
5
|
+
|
6
|
+
In simpler terms, this is a library for sending messages to iChat (or Adium, Pidgin, etc) using the Bonjour chat protocol.
|
7
|
+
|
8
|
+
The Bonjour chat protocol is pretty much XMPP with the presence and server parts ripped out and replaced Bonjour (DNS Service Discovery and multicast DNS). Like standard XMPP while mainly used as for instant messaging, there is nothing stopping you for using it for a more generic messaging system, a presence system or interprocess communication.
|
9
|
+
|
10
|
+
Ahoy isn't much more than a wrapper with a nice API around the dnssd and xmpp4r gems.
|
11
|
+
|
12
|
+
The API is based around the idea that this is an instant messaging protocol, there is a user, that user has a list of contacts, you can start a chat with a contact and send messages the contact though that chat session. This may be refined, but is unlikely to change in it's outlook.
|
13
|
+
|
14
|
+
Example:
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
require 'ahoy'
|
18
|
+
|
19
|
+
user = Ahoy::User.new("mat")
|
20
|
+
user.sign_in
|
21
|
+
|
22
|
+
sleep 1 # wait for the contact list to populate
|
23
|
+
|
24
|
+
first_contact = user.contacts.first # find someone to talk to
|
25
|
+
|
26
|
+
chat = user.chat(first_contact) # open a chat with them
|
27
|
+
|
28
|
+
chat.send("We come in peace.") # send a message
|
29
|
+
|
30
|
+
puts chat.receive # blocks until we get a response
|
31
|
+
|
32
|
+
chat.close
|
33
|
+
|
34
|
+
Along with the blocking Chat#receive method to receive replies, there is an on_reply method that takes a callback. This method returns immediately, and the block is only called when there is a reply, and can be used like so
|
35
|
+
|
36
|
+
require 'rubygems'
|
37
|
+
require 'ahoy'
|
38
|
+
|
39
|
+
user = Ahoy::User.new("mat")
|
40
|
+
user.sign_in
|
41
|
+
sleep 1
|
42
|
+
chat = user.chat(user.contacts.first)
|
43
|
+
|
44
|
+
chat.on_reply do |reply|
|
45
|
+
puts reply
|
46
|
+
end
|
47
|
+
|
48
|
+
chat.send("hello")
|
49
|
+
|
50
|
+
# do something while we wait for a reply
|
51
|
+
loop do
|
52
|
+
sleep 1
|
53
|
+
end
|
54
|
+
|
55
|
+
If you are sending messages to a contact on the same machine (likely while testing, sending messages to yourself in iChat), you may need to set the interface on the user to your primary network interface:
|
56
|
+
|
57
|
+
user = Ahoy::User.new("mat")
|
58
|
+
user.interface = "en1" # on Mac OS X en1 is usually Wi-Fi, en0 is ethernet
|
59
|
+
user.sign_in
|
60
|
+
|
61
|
+
...
|
62
|
+
|
63
|
+
The current use case is to send a message to a team of developers when a deploy script is run, there is a simplified interface for this:
|
64
|
+
|
65
|
+
require 'rubygems'
|
66
|
+
require 'ahoy/broadcast'
|
67
|
+
|
68
|
+
user = Ahoy::User.new("Dr. Nick")
|
69
|
+
cast = Ahoy::Broadcast.new(user)
|
70
|
+
|
71
|
+
cast.send("Hi, everybody!")
|
72
|
+
cast.close
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ahoy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mat Sadler
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-03-04 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- lib/ahoy/user.rb
|
50
50
|
- lib/ahoy/xmpp4r_hack.rb
|
51
51
|
- lib/ahoy.rb
|
52
|
+
- readme.rdoc
|
52
53
|
has_rdoc: true
|
53
54
|
homepage: http://sourcetagsandcodes.com
|
54
55
|
licenses: []
|