socialjack 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/lib/advertised.rb +69 -0
- data/lib/lib/agent.rb +14 -0
- data/lib/lib/client.rb +8 -0
- data/lib/lib/example.rb +34 -0
- data/lib/lib/multilingual.rb +19 -0
- data/lib/lib/socialite.rb +111 -0
- data/lib/socialjack.rb +1 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 66878a80a2473a398d607e3935a18bcbdf0fa56b
|
4
|
+
data.tar.gz: 669770c24d7a1e656fa849a09ed2a9d09d1d7d37
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 36213a9a3d35e9ed8aec2ec1679cf5c89eed1e9961d1f9e280679c3449f202417f94177d9aba988be258864009f8e5d4562632b79eb03b227385df153b88e205
|
7
|
+
data.tar.gz: 9bd9e789e1cf040786caddfb931e480f09a35bdaab76a2a58b9f00f29c48d31e0dac0b081835a7cb0904883251c72ed3f09a46ca0e41161d383210558522aa7e
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'dnssd'
|
2
|
+
require 'monitor'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
Thread.abort_on_exception = true
|
6
|
+
trap 'INT' do exit end
|
7
|
+
trap 'TERM' do exit end
|
8
|
+
|
9
|
+
module Advertiser
|
10
|
+
|
11
|
+
attr_reader :name
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def service_type
|
16
|
+
"_object._tcp"
|
17
|
+
end
|
18
|
+
|
19
|
+
def domain
|
20
|
+
"local"
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_object_name name
|
24
|
+
"#{name}Object"
|
25
|
+
end
|
26
|
+
|
27
|
+
def advertise port, name=nil
|
28
|
+
@name ||= name
|
29
|
+
puts "AD: #{get_object_name(@name)} :: #{port}"
|
30
|
+
raise "Can not advertise w/o name" if @name.nil?
|
31
|
+
record = DNSSD::TextRecord.new
|
32
|
+
record["test"] = '1'
|
33
|
+
record["success"] = '1'
|
34
|
+
DNSSD.register get_object_name(@name), service_type,
|
35
|
+
domain, port, record
|
36
|
+
end
|
37
|
+
|
38
|
+
def find name
|
39
|
+
# use zeroconf to find zmq endpoint
|
40
|
+
# for the given obj name
|
41
|
+
puts "FIND: #{get_object_name(name)} :: #{service_type}"
|
42
|
+
results = []
|
43
|
+
results.extend MonitorMixin
|
44
|
+
empty_cond = results.new_cond
|
45
|
+
s = DNSSD.resolve(get_object_name(name), service_type, domain) do |reply|
|
46
|
+
puts "FOUND: #{reply.name} :: #{reply.target}:#{reply.port}"
|
47
|
+
results.synchronize do
|
48
|
+
results << [reply.target, reply.port]
|
49
|
+
empty_cond.signal
|
50
|
+
end
|
51
|
+
next if reply.flags.more_coming?
|
52
|
+
Thread.exit
|
53
|
+
end
|
54
|
+
# wait for one of the threaded resolvers to find what we want
|
55
|
+
# or a timeout
|
56
|
+
results.synchronize do
|
57
|
+
Timeout::timeout(2) {
|
58
|
+
empty_cond.wait_while { results.empty? }
|
59
|
+
} rescue Timeout::Error
|
60
|
+
end
|
61
|
+
s.stop unless s.stopped?
|
62
|
+
raise "Could not stop resolver" unless s.stopped?
|
63
|
+
return results[0] unless results.empty?
|
64
|
+
return nil
|
65
|
+
rescue => ex
|
66
|
+
puts "EXCEPTION finding name: #{ex}"
|
67
|
+
raise ex
|
68
|
+
end
|
69
|
+
end
|
data/lib/lib/agent.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'dnssd'
|
2
|
+
domains = []
|
3
|
+
enumerator = DNSSD.enumerate_domains do |reply|
|
4
|
+
domains << reply.domain
|
5
|
+
puts "Found domains:\n#{domains.join "\n"}"
|
6
|
+
next if reply.flags.more_coming?
|
7
|
+
break
|
8
|
+
end
|
9
|
+
DNSSD.register 'agent', '_agent._tcp', nil, 6464
|
10
|
+
puts "registered agent at 6464"
|
11
|
+
loop do
|
12
|
+
sleep 1
|
13
|
+
puts "agent running.."
|
14
|
+
end
|
data/lib/lib/client.rb
ADDED
data/lib/lib/example.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'wiredobject'
|
2
|
+
|
3
|
+
class Testeroo
|
4
|
+
include WiredObject
|
5
|
+
end
|
6
|
+
|
7
|
+
to_send = ARGV.shift
|
8
|
+
puts "TOSEND: #{to_send}"
|
9
|
+
unless to_send
|
10
|
+
tester = Testeroo.new
|
11
|
+
tester.instance_eval do
|
12
|
+
puts 'advertising'
|
13
|
+
puts 'binding'
|
14
|
+
bind '0.0.0.0', 2000
|
15
|
+
advertise 2000, 'TEST'
|
16
|
+
end
|
17
|
+
tester.instance_eval do
|
18
|
+
puts 'poping'
|
19
|
+
loop do
|
20
|
+
r = pop
|
21
|
+
puts "R: #{r}" if r
|
22
|
+
end
|
23
|
+
end
|
24
|
+
else
|
25
|
+
tester2 = Testeroo.new
|
26
|
+
tester2.instance_eval do
|
27
|
+
puts 'pushing'
|
28
|
+
push 'TEST', to_send
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
puts "END OF EXAMPLE"
|
33
|
+
puts "Threads:"
|
34
|
+
Thread.list.each {|t| p t}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'msgpack'
|
2
|
+
|
3
|
+
module Multilingual
|
4
|
+
|
5
|
+
attr_accessor :serializer, :deserializer
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def serialize data
|
10
|
+
serializer ||= proc {|d| MessagePack.pack d}
|
11
|
+
serializer.call data
|
12
|
+
end
|
13
|
+
|
14
|
+
def deserialize data
|
15
|
+
deserializer ||= proc {|d| MessagePack.unpack d}
|
16
|
+
deserializer.call data
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'ffi-rzmq'
|
2
|
+
require 'msgpack'
|
3
|
+
|
4
|
+
require_relative 'advertised'
|
5
|
+
require_relative 'multilingual'
|
6
|
+
|
7
|
+
module Socialite
|
8
|
+
|
9
|
+
include Advertiser
|
10
|
+
include Multilingual
|
11
|
+
|
12
|
+
attr_reader :host, :port
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def connections
|
17
|
+
@connections ||= {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def ctx
|
21
|
+
self.class.instance_eval do
|
22
|
+
return @ctx ||= ZMQ::Context.new(1)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def bind_random host='0.0.0.0'
|
27
|
+
100.times do
|
28
|
+
# catch bind exceptions, just try again
|
29
|
+
port = Random.rand 2000...8000
|
30
|
+
return bind(host, port) rescue "Bad Bind: #{port}"
|
31
|
+
end
|
32
|
+
raise "Could not find port to bind to"
|
33
|
+
end
|
34
|
+
|
35
|
+
def bind host, port
|
36
|
+
@in_connection = ctx.socket ZMQ::PAIR
|
37
|
+
#socket.setsockopt ZMQ::LINGER, 1
|
38
|
+
@in_connection.bind "tcp://#{host}:#{port}"
|
39
|
+
poller.register(@in_connection, ZMQ::POLLIN)
|
40
|
+
@host, @port = host, port
|
41
|
+
return @in_connection
|
42
|
+
end
|
43
|
+
|
44
|
+
def unbind
|
45
|
+
@in_connection.close
|
46
|
+
end
|
47
|
+
|
48
|
+
def connection name
|
49
|
+
return connections[name] if connections.include? name
|
50
|
+
celf = self
|
51
|
+
addr = find name
|
52
|
+
raise "Could not find #{name}" if addr.nil?
|
53
|
+
connection = connect *addr
|
54
|
+
return if connection.nil?
|
55
|
+
shadow = connection.instance_eval {class << self; self; end;}
|
56
|
+
shadow.instance_eval do
|
57
|
+
define_method :cleanup do
|
58
|
+
celf.instance_eval do
|
59
|
+
connections.delete name if connections[name] == self
|
60
|
+
end
|
61
|
+
connection.close rescue "Bad connection close"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
connections[name] = connection
|
65
|
+
return connection
|
66
|
+
end
|
67
|
+
|
68
|
+
def connect host='127.0.0.1', port
|
69
|
+
socket = ctx.socket ZMQ::PAIR
|
70
|
+
#socket.setsockopt ZMQ::LINGER, 1
|
71
|
+
socket.connect "tcp://#{host}:#{port}"
|
72
|
+
poller.register(socket, ZMQ::POLLIN)
|
73
|
+
return socket
|
74
|
+
end
|
75
|
+
|
76
|
+
def push name, data, do_retry=true
|
77
|
+
# send the data to an obj matching the name
|
78
|
+
conn = connection name
|
79
|
+
to_send = serialize data
|
80
|
+
begin
|
81
|
+
conn.send_string to_send, ZMQ::NOBLOCK
|
82
|
+
rescue => ex
|
83
|
+
conn.cleanup
|
84
|
+
if do_retry
|
85
|
+
push name, data, false
|
86
|
+
else
|
87
|
+
raise ex
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def poller
|
93
|
+
@poller ||= ZMQ::Poller.new
|
94
|
+
end
|
95
|
+
|
96
|
+
def pop
|
97
|
+
poller.poll 1000
|
98
|
+
poller.readables.each do |conn|
|
99
|
+
begin
|
100
|
+
message = ""
|
101
|
+
conn.recv_string message
|
102
|
+
return deserialize message
|
103
|
+
rescue => ex
|
104
|
+
conn.cleanup
|
105
|
+
raise ex
|
106
|
+
end
|
107
|
+
end
|
108
|
+
return nil
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
data/lib/socialjack.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'lib/socialite'
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: socialjack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robby Ranshous
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: all work and no chatter makes jack a crouchy boy
|
14
|
+
email:
|
15
|
+
- rranshous@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/lib/advertised.rb
|
21
|
+
- lib/lib/agent.rb
|
22
|
+
- lib/lib/client.rb
|
23
|
+
- lib/lib/example.rb
|
24
|
+
- lib/lib/multilingual.rb
|
25
|
+
- lib/lib/socialite.rb
|
26
|
+
- lib/socialjack.rb
|
27
|
+
homepage: http://oneinchmile.com
|
28
|
+
licenses: []
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.4.8
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: jack is a very social boy
|
50
|
+
test_files: []
|