spockets 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,8 @@
1
+ 0.0.5
2
+
3
+ * Added a delete alias
4
+ * Added an on_close method to set an action
5
+ to run when a closed socket is detected
6
+ * Added an include? method to check if a socket
7
+ is currently being watched
8
+ * Added clear method to remove all sockets
data/CHANGELOG~ ADDED
File without changes
data/README CHANGED
@@ -23,8 +23,16 @@ cd spockets
23
23
  gem build spockets.gemspec
24
24
  gem install spockets-x.x.x.gem
25
25
 
26
- It has RDocs. They are where ever your rubygems installed them.
27
- They will probably be helpful to look over.
26
+ It has RDocs. They are short, but will be helpful and you
27
+ should really consider giving them a look. If you want to
28
+ view them online, you can see them here:
29
+
30
+ http://dev.modspox.com/~sine/spockets
31
+
32
+ There is also a trac site. It has examples as well as
33
+ a bug tracker. It's located at:
34
+
35
+ http://dev.modspox.com/trac/spockets
28
36
 
29
37
  Examples are usually helpful, so here we go:
30
38
 
data/README~ CHANGED
@@ -14,7 +14,7 @@ action can stay in one local pool.
14
14
 
15
15
  install (easy):
16
16
 
17
- gem install --source gems.github.com spockets
17
+ gem install spockets
18
18
 
19
19
  install (less easy):
20
20
 
@@ -24,4 +24,50 @@ gem build spockets.gemspec
24
24
  gem install spockets-x.x.x.gem
25
25
 
26
26
  It has RDocs. They are where ever your rubygems installed them.
27
- They will probably be helpful to look over.
27
+ They will probably be helpful to look over.
28
+
29
+ Examples are usually helpful, so here we go:
30
+
31
+ Code:
32
+
33
+ require 'socket'
34
+ require 'spockets'
35
+ spockets = Spockets::Spockets.new
36
+
37
+ se = TCPServer.new(3000)
38
+ loop do
39
+ s = se.accept
40
+ puts "Socket: #{s}"
41
+ spockets.add(s){|string| puts "#{s}: #{string}" }
42
+ end
43
+ sleep
44
+
45
+
46
+ Connecting:
47
+
48
+ > telnet 192.168.0.95 3000
49
+ Trying 192.168.0.95...
50
+ Connected to 192.168.0.95.
51
+ Escape character is '^]'.
52
+ goodbyeworld
53
+ ^]
54
+ telnet> quit
55
+ Connection closed.
56
+
57
+ > telnet 192.168.0.95 3000
58
+ Trying 192.168.0.95...
59
+ Connected to 192.168.0.95.
60
+ Escape character is '^]'.
61
+ foobar
62
+ complete
63
+ ^]
64
+ telnet> quit
65
+ Connection closed.
66
+
67
+ Output:
68
+
69
+ Socket: #<TCPSocket:0x98ec5ac>
70
+ Socket: #<TCPSocket:0x98ec37c>
71
+ #<TCPSocket:0x98ec37c>: foobar
72
+ #<TCPSocket:0x98ec5ac>: goodbyeworld
73
+ #<TCPSocket:0x98ec37c>: complete
@@ -21,7 +21,8 @@ module Spockets
21
21
  # for processing
22
22
  def add(socket, &block)
23
23
  raise DuplicateSocket.new(socket) if @sockets.has_key?(socket)
24
- @sockets[socket] = [block]
24
+ @sockets[socket] = {}
25
+ @sockets[socket][:procs] = [block]
25
26
  begin
26
27
  @watcher.sync
27
28
  rescue NotRunning
@@ -35,7 +36,7 @@ module Spockets
35
36
  # socket to be executed when a new string is received
36
37
  def extra(socket, &block)
37
38
  raise UnknownSocket.new(socket) unless @sockets.has_key?(socket)
38
- @sockets[socket] << block
39
+ @sockets[socket][:procs] << block
39
40
  end
40
41
 
41
42
  # socket:: socket to remove
@@ -50,6 +51,15 @@ module Spockets
50
51
  end
51
52
  end
52
53
 
54
+ # socket:: socket to add close action
55
+ # block:: action to perform on socket close
56
+ # Executes block when socket has been closed. Ideal
57
+ # for reconnection needs
58
+ def on_close(socket, &block)
59
+ raise UnknownSocket.new(socket) unless @sockets.has_key?(socket)
60
+ @sockets[socket][:closed] = block
61
+ end
62
+
53
63
  # remove all sockets
54
64
  def clear
55
65
  @sockets.clear
@@ -5,8 +5,6 @@ module Spockets
5
5
 
6
6
  class Spockets
7
7
 
8
- alias :delete :remove
9
-
10
8
  # :pool:: ActionPool if you would like to consolidate
11
9
  # :clean:: Clean string. Set to true for default or
12
10
  # provide a block to clean strings
@@ -23,7 +21,7 @@ module Spockets
23
21
  # for processing
24
22
  def add(socket, &block)
25
23
  raise DuplicateSocket.new(socket) if @sockets.has_key?(socket)
26
- @sockets[socket] = [block]
24
+ @sockets[socket][:procs] = [block]
27
25
  begin
28
26
  @watcher.sync
29
27
  rescue NotRunning
@@ -37,7 +35,7 @@ module Spockets
37
35
  # socket to be executed when a new string is received
38
36
  def extra(socket, &block)
39
37
  raise UnknownSocket.new(socket) unless @sockets.has_key?(socket)
40
- @sockets[socket] << block
38
+ @sockets[socket][:procs] << block
41
39
  end
42
40
 
43
41
  # socket:: socket to remove
@@ -52,6 +50,15 @@ module Spockets
52
50
  end
53
51
  end
54
52
 
53
+ # socket:: socket to add close action
54
+ # block:: action to perform on socket close
55
+ # Executes block when socket has been closed. Ideal
56
+ # for reconnection needs
57
+ def on_close(socket, &block)
58
+ raise UnknownSocket.new(socket) unless @sockets.has_key?(socket)
59
+ @sockets[socket][:closed] = block
60
+ end
61
+
55
62
  # remove all sockets
56
63
  def clear
57
64
  @sockets.clear
@@ -81,6 +88,8 @@ module Spockets
81
88
  @sockets.has_key?(socket)
82
89
  end
83
90
 
91
+ alias :delete :remove
92
+
84
93
  end
85
94
 
86
95
  end
@@ -31,7 +31,7 @@ module Spockets
31
31
  @runner.join(0.1)
32
32
  @runner.raise Resync.new
33
33
  @runner.join(0.1)
34
- @runner.kill unless @runner.alive?
34
+ @runner.kill unless @runner.nil? || @runner.alive?
35
35
  @runner = nil
36
36
  end
37
37
 
@@ -56,15 +56,15 @@ module Spockets
56
56
  def watch
57
57
  until(@stop)
58
58
  begin
59
- puts 'we are watching'
60
59
  resultset = Kernel.select(@sockets.keys, nil, nil, nil)
61
60
  for sock in resultset[0]
62
61
  string = sock.gets
63
62
  if(sock.closed? || string.nil?)
63
+ @sockets[sock][:closed].call(sock) if @sockets[sock].has_key?(:closed)
64
64
  @sockets.delete(sock)
65
65
  else
66
66
  string = clean? ? do_clean(string) : string
67
- @sockets[sock].each{|b| @pool.process{ b.call(string)}}
67
+ @sockets[sock][:procs].each{|b| @pool.process{ b.call(string)}}
68
68
  end
69
69
  end
70
70
  rescue Resync
@@ -0,0 +1,89 @@
1
+ require 'actionpool'
2
+ require 'actionpool/Exceptions'
3
+ require 'iconv'
4
+
5
+ module Spockets
6
+ class Watcher
7
+
8
+ # :sockets:: socket list
9
+ # :clean:: clean UTF8 strings or provide block to run on every read string
10
+ # :pool:: ActionPool to use
11
+ # Creates a new watcher for sockets
12
+ def initialize(args)
13
+ raise MissingArgument.new(:sockets) unless args[:sockets]
14
+ @sockets = args[:sockets]
15
+ @runner = nil
16
+ @clean = args[:clean] && (args[:clean].is_a?(Proc) || args[:clean].is_a?(TrueClass)) ? args[:clean] : nil
17
+ @pool = args[:pool] && args[:pool].is_a?(ActionPool::Pool) ? args[:pool] : ActionPool::Pool.new
18
+ @ic = @clean && @clean.is_a?(TrueClass) ? Iconv.new('UTF-8//IGNORE', 'UTF-8') : nil
19
+ @stop = true
20
+ end
21
+
22
+ # start the watcher
23
+ def start
24
+ @stop = false
25
+ @runner = Thread.new{watch} if @runner.nil? && @sockets.size > 0
26
+ end
27
+
28
+ # stop the watcher
29
+ def stop
30
+ @stop = true
31
+ @runner.join(0.1)
32
+ @runner.raise Resync.new
33
+ @runner.join(0.1)
34
+ @runner.kill unless @runner.alive?
35
+ @runner = nil
36
+ end
37
+
38
+ # is the watcher running?
39
+ def running?
40
+ !@stop
41
+ end
42
+
43
+ # clean incoming strings
44
+ def clean?
45
+ @clean
46
+ end
47
+
48
+ # Ensure all sockets are being listened to
49
+ def sync
50
+ raise NotRunning.new if @runner.nil?
51
+ @runner.raise Resync.new
52
+ end
53
+
54
+ private
55
+
56
+ def watch
57
+ until(@stop)
58
+ begin
59
+ resultset = Kernel.select(@sockets.keys, nil, nil, nil)
60
+ for sock in resultset[0]
61
+ string = sock.gets
62
+ if(sock.closed? || string.nil?)
63
+ @sockets[sock][:closed].call(sock) if @sockets[sock].has_key?(:closed)
64
+ @sockets.delete(sock)
65
+ else
66
+ string = clean? ? do_clean(string) : string
67
+ @sockets[sock][:procs].each{|b| @pool.process{ b.call(string)}}
68
+ end
69
+ end
70
+ rescue Resync
71
+ # break select and relisten #
72
+ end
73
+ end
74
+ @runner = nil
75
+ end
76
+
77
+ def do_clean(string)
78
+ unless(@ic.nil?)
79
+ return untaint(string)
80
+ else
81
+ return @clean.call(string)
82
+ end
83
+ end
84
+
85
+ def untaint(s)
86
+ @ic.iconv(s + ' ')[0..-2]
87
+ end
88
+ end
89
+ end
Binary file
data/spockets.gemspec CHANGED
@@ -2,7 +2,7 @@ spec = Gem::Specification.new do |s|
2
2
  s.name = 'spockets'
3
3
  s.author = %q(spox)
4
4
  s.email = %q(spox@modspox.com)
5
- s.version = '0.0.4'
5
+ s.version = '0.0.5'
6
6
  s.summary = %q(Socket Helper)
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.has_rdoc = true
@@ -13,4 +13,12 @@ spec = Gem::Specification.new do |s|
13
13
  s.add_dependency 'ActionPool'
14
14
  s.homepage = %q(http://dev.modspox.com/trac/spockets)
15
15
  description = []
16
+ File.open("README") do |file|
17
+ file.each do |line|
18
+ line.chomp!
19
+ break if line.empty?
20
+ description << "#{line.gsub(/\[\d\]/, '')}"
21
+ end
22
+ end
23
+ s.description = description[1..-1].join(" ")
16
24
  end
data/spockets.gemspec~ CHANGED
@@ -2,7 +2,7 @@ spec = Gem::Specification.new do |s|
2
2
  s.name = 'spockets'
3
3
  s.author = %q(spox)
4
4
  s.email = %q(spox@modspox.com)
5
- s.version = '0.0.3'
5
+ s.version = '0.0.4'
6
6
  s.summary = %q(Socket Helper)
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.has_rdoc = true
@@ -13,4 +13,12 @@ spec = Gem::Specification.new do |s|
13
13
  s.add_dependency 'ActionPool'
14
14
  s.homepage = %q(http://dev.modspox.com/trac/spockets)
15
15
  description = []
16
+ File.open("README") do |file|
17
+ file.each do |line|
18
+ line.chomp!
19
+ break if line.empty?
20
+ description << "#{line.gsub(/\[\d\]/, '')}"
21
+ end
22
+ end
23
+ s.description = description[1..-1].join(" ")
16
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spockets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - spox
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-24 00:00:00 -07:00
12
+ date: 2009-04-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,7 +22,7 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
- description:
25
+ description: ""
26
26
  email: spox@modspox.com
27
27
  executables: []
28
28
 
@@ -36,14 +36,18 @@ files:
36
36
  - spockets.gemspec~
37
37
  - spockets-0.0.3.gem
38
38
  - spockets.gemspec
39
+ - CHANGELOG~
39
40
  - lib
40
41
  - lib/spockets
41
42
  - lib/spockets/Spockets.rb~
43
+ - lib/spockets/Watcher.rb~
42
44
  - lib/spockets/Exceptions.rb
43
45
  - lib/spockets/Watcher.rb
44
46
  - lib/spockets/Spockets.rb
45
47
  - lib/spockets.rb
48
+ - CHANGELOG
46
49
  - LICENSE
50
+ - spockets-0.0.4.gem
47
51
  - spockets-0.0.2.gem
48
52
  has_rdoc: true
49
53
  homepage: http://dev.modspox.com/trac/spockets