spockets 0.0.5 → 0.0.6
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.
- data/CHANGELOG +3 -0
- data/README.rdoc +86 -0
- data/lib/spockets/Watcher.rb +5 -1
- metadata +9 -21
- data/CHANGELOG~ +0 -0
- data/LICENSE +0 -165
- data/README +0 -81
- data/README~ +0 -73
- data/lib/spockets/Spockets.rb~ +0 -95
- data/lib/spockets/Watcher.rb~ +0 -89
- data/spockets-0.0.2.gem +0 -0
- data/spockets-0.0.3.gem +0 -0
- data/spockets-0.0.4.gem +0 -0
- data/spockets.gemspec +0 -24
- data/spockets.gemspec~ +0 -24
data/CHANGELOG
CHANGED
data/README.rdoc
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
== Spockets ==
|
2
|
+
|
3
|
+
Spockets is a simple library for dealing with multiple sockets.
|
4
|
+
You supply a socket, and one or more blocks to
|
5
|
+
execute, and Spockets will make sure those blocks get
|
6
|
+
executed when something comes in over the wire. It's just
|
7
|
+
that simple.
|
8
|
+
|
9
|
+
There is one requirement for spockets and that is the ActionPool.
|
10
|
+
This just allows spockets to use a thread pool for executing
|
11
|
+
blocks so you don't end up having to wait on slow blocks. You
|
12
|
+
can even provide your own pool for spockets to use, so all the
|
13
|
+
action can stay in one local pool.
|
14
|
+
|
15
|
+
install (stable):
|
16
|
+
|
17
|
+
gem install spockets
|
18
|
+
|
19
|
+
install (unstable):
|
20
|
+
|
21
|
+
gem sources -a http://gems.github.com
|
22
|
+
gem install spox-spockets
|
23
|
+
|
24
|
+
or
|
25
|
+
|
26
|
+
git clone http://github.com/spox/spockets.git
|
27
|
+
cd spockets
|
28
|
+
gem build spockets.gemspec
|
29
|
+
gem install ./
|
30
|
+
|
31
|
+
It has RDocs. They are short, but will be helpful and you
|
32
|
+
should really consider giving them a look. If you want to
|
33
|
+
view them online, you can see them here:
|
34
|
+
|
35
|
+
http://dev.modspox.com/~sine/spockets
|
36
|
+
|
37
|
+
There is also a trac site. It has examples as well as
|
38
|
+
a bug tracker. It's located at:
|
39
|
+
|
40
|
+
http://dev.modspox.com/trac/spockets
|
41
|
+
|
42
|
+
Examples are usually helpful, so here we go:
|
43
|
+
|
44
|
+
Code:
|
45
|
+
|
46
|
+
require 'socket'
|
47
|
+
require 'spockets'
|
48
|
+
spockets = Spockets::Spockets.new
|
49
|
+
|
50
|
+
se = TCPServer.new(3000)
|
51
|
+
loop do
|
52
|
+
s = se.accept
|
53
|
+
puts "Socket: #{s}"
|
54
|
+
spockets.add(s){|string| puts "#{s}: #{string}" }
|
55
|
+
end
|
56
|
+
sleep
|
57
|
+
|
58
|
+
|
59
|
+
Connecting:
|
60
|
+
|
61
|
+
> telnet 192.168.0.95 3000
|
62
|
+
Trying 192.168.0.95...
|
63
|
+
Connected to 192.168.0.95.
|
64
|
+
Escape character is '^]'.
|
65
|
+
goodbyeworld
|
66
|
+
^]
|
67
|
+
telnet> quit
|
68
|
+
Connection closed.
|
69
|
+
|
70
|
+
> telnet 192.168.0.95 3000
|
71
|
+
Trying 192.168.0.95...
|
72
|
+
Connected to 192.168.0.95.
|
73
|
+
Escape character is '^]'.
|
74
|
+
foobar
|
75
|
+
complete
|
76
|
+
^]
|
77
|
+
telnet> quit
|
78
|
+
Connection closed.
|
79
|
+
|
80
|
+
Output:
|
81
|
+
|
82
|
+
Socket: #<TCPSocket:0x98ec5ac>
|
83
|
+
Socket: #<TCPSocket:0x98ec37c>
|
84
|
+
#<TCPSocket:0x98ec37c>: foobar
|
85
|
+
#<TCPSocket:0x98ec5ac>: goodbyeworld
|
86
|
+
#<TCPSocket:0x98ec37c>: complete
|
data/lib/spockets/Watcher.rb
CHANGED
@@ -64,7 +64,7 @@ module Spockets
|
|
64
64
|
@sockets.delete(sock)
|
65
65
|
else
|
66
66
|
string = clean? ? do_clean(string) : string
|
67
|
-
|
67
|
+
process(string.dup, sock)
|
68
68
|
end
|
69
69
|
end
|
70
70
|
rescue Resync
|
@@ -74,6 +74,10 @@ module Spockets
|
|
74
74
|
@runner = nil
|
75
75
|
end
|
76
76
|
|
77
|
+
def process(string, sock)
|
78
|
+
@sockets[sock][:procs].each{|b| @pool.process{ b.call(string)}}
|
79
|
+
end
|
80
|
+
|
77
81
|
def do_clean(string)
|
78
82
|
unless(@ic.nil?)
|
79
83
|
return untaint(string)
|
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
|
+
version: 0.0.6
|
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-
|
12
|
+
date: 2009-06-27 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,33 +22,21 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: "0"
|
24
24
|
version:
|
25
|
-
description:
|
25
|
+
description: Socket helper library
|
26
26
|
email: spox@modspox.com
|
27
27
|
executables: []
|
28
28
|
|
29
29
|
extensions: []
|
30
30
|
|
31
31
|
extra_rdoc_files:
|
32
|
-
- README
|
32
|
+
- README.rdoc
|
33
33
|
files:
|
34
|
-
- README
|
35
|
-
-
|
36
|
-
- spockets.
|
37
|
-
- spockets-0.0.3.gem
|
38
|
-
- spockets.gemspec
|
39
|
-
- CHANGELOG~
|
40
|
-
- lib
|
41
|
-
- lib/spockets
|
42
|
-
- lib/spockets/Spockets.rb~
|
43
|
-
- lib/spockets/Watcher.rb~
|
34
|
+
- README.rdoc
|
35
|
+
- CHANGELOG
|
36
|
+
- lib/spockets.rb
|
44
37
|
- lib/spockets/Exceptions.rb
|
45
|
-
- lib/spockets/Watcher.rb
|
46
38
|
- lib/spockets/Spockets.rb
|
47
|
-
- lib/spockets.rb
|
48
|
-
- CHANGELOG
|
49
|
-
- LICENSE
|
50
|
-
- spockets-0.0.4.gem
|
51
|
-
- spockets-0.0.2.gem
|
39
|
+
- lib/spockets/Watcher.rb
|
52
40
|
has_rdoc: true
|
53
41
|
homepage: http://dev.modspox.com/trac/spockets
|
54
42
|
post_install_message:
|
@@ -56,7 +44,7 @@ rdoc_options:
|
|
56
44
|
- --title
|
57
45
|
- Spockets
|
58
46
|
- --main
|
59
|
-
- README
|
47
|
+
- README.rdoc
|
60
48
|
- --line-numbers
|
61
49
|
require_paths:
|
62
50
|
- lib
|
data/CHANGELOG~
DELETED
File without changes
|
data/LICENSE
DELETED
@@ -1,165 +0,0 @@
|
|
1
|
-
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
-
Version 3, 29 June 2007
|
3
|
-
|
4
|
-
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
-
Everyone is permitted to copy and distribute verbatim copies
|
6
|
-
of this license document, but changing it is not allowed.
|
7
|
-
|
8
|
-
|
9
|
-
This version of the GNU Lesser General Public License incorporates
|
10
|
-
the terms and conditions of version 3 of the GNU General Public
|
11
|
-
License, supplemented by the additional permissions listed below.
|
12
|
-
|
13
|
-
0. Additional Definitions.
|
14
|
-
|
15
|
-
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
-
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
-
General Public License.
|
18
|
-
|
19
|
-
"The Library" refers to a covered work governed by this License,
|
20
|
-
other than an Application or a Combined Work as defined below.
|
21
|
-
|
22
|
-
An "Application" is any work that makes use of an interface provided
|
23
|
-
by the Library, but which is not otherwise based on the Library.
|
24
|
-
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
-
of using an interface provided by the Library.
|
26
|
-
|
27
|
-
A "Combined Work" is a work produced by combining or linking an
|
28
|
-
Application with the Library. The particular version of the Library
|
29
|
-
with which the Combined Work was made is also called the "Linked
|
30
|
-
Version".
|
31
|
-
|
32
|
-
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
-
Corresponding Source for the Combined Work, excluding any source code
|
34
|
-
for portions of the Combined Work that, considered in isolation, are
|
35
|
-
based on the Application, and not on the Linked Version.
|
36
|
-
|
37
|
-
The "Corresponding Application Code" for a Combined Work means the
|
38
|
-
object code and/or source code for the Application, including any data
|
39
|
-
and utility programs needed for reproducing the Combined Work from the
|
40
|
-
Application, but excluding the System Libraries of the Combined Work.
|
41
|
-
|
42
|
-
1. Exception to Section 3 of the GNU GPL.
|
43
|
-
|
44
|
-
You may convey a covered work under sections 3 and 4 of this License
|
45
|
-
without being bound by section 3 of the GNU GPL.
|
46
|
-
|
47
|
-
2. Conveying Modified Versions.
|
48
|
-
|
49
|
-
If you modify a copy of the Library, and, in your modifications, a
|
50
|
-
facility refers to a function or data to be supplied by an Application
|
51
|
-
that uses the facility (other than as an argument passed when the
|
52
|
-
facility is invoked), then you may convey a copy of the modified
|
53
|
-
version:
|
54
|
-
|
55
|
-
a) under this License, provided that you make a good faith effort to
|
56
|
-
ensure that, in the event an Application does not supply the
|
57
|
-
function or data, the facility still operates, and performs
|
58
|
-
whatever part of its purpose remains meaningful, or
|
59
|
-
|
60
|
-
b) under the GNU GPL, with none of the additional permissions of
|
61
|
-
this License applicable to that copy.
|
62
|
-
|
63
|
-
3. Object Code Incorporating Material from Library Header Files.
|
64
|
-
|
65
|
-
The object code form of an Application may incorporate material from
|
66
|
-
a header file that is part of the Library. You may convey such object
|
67
|
-
code under terms of your choice, provided that, if the incorporated
|
68
|
-
material is not limited to numerical parameters, data structure
|
69
|
-
layouts and accessors, or small macros, inline functions and templates
|
70
|
-
(ten or fewer lines in length), you do both of the following:
|
71
|
-
|
72
|
-
a) Give prominent notice with each copy of the object code that the
|
73
|
-
Library is used in it and that the Library and its use are
|
74
|
-
covered by this License.
|
75
|
-
|
76
|
-
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
-
document.
|
78
|
-
|
79
|
-
4. Combined Works.
|
80
|
-
|
81
|
-
You may convey a Combined Work under terms of your choice that,
|
82
|
-
taken together, effectively do not restrict modification of the
|
83
|
-
portions of the Library contained in the Combined Work and reverse
|
84
|
-
engineering for debugging such modifications, if you also do each of
|
85
|
-
the following:
|
86
|
-
|
87
|
-
a) Give prominent notice with each copy of the Combined Work that
|
88
|
-
the Library is used in it and that the Library and its use are
|
89
|
-
covered by this License.
|
90
|
-
|
91
|
-
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
-
document.
|
93
|
-
|
94
|
-
c) For a Combined Work that displays copyright notices during
|
95
|
-
execution, include the copyright notice for the Library among
|
96
|
-
these notices, as well as a reference directing the user to the
|
97
|
-
copies of the GNU GPL and this license document.
|
98
|
-
|
99
|
-
d) Do one of the following:
|
100
|
-
|
101
|
-
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
-
License, and the Corresponding Application Code in a form
|
103
|
-
suitable for, and under terms that permit, the user to
|
104
|
-
recombine or relink the Application with a modified version of
|
105
|
-
the Linked Version to produce a modified Combined Work, in the
|
106
|
-
manner specified by section 6 of the GNU GPL for conveying
|
107
|
-
Corresponding Source.
|
108
|
-
|
109
|
-
1) Use a suitable shared library mechanism for linking with the
|
110
|
-
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
-
a copy of the Library already present on the user's computer
|
112
|
-
system, and (b) will operate properly with a modified version
|
113
|
-
of the Library that is interface-compatible with the Linked
|
114
|
-
Version.
|
115
|
-
|
116
|
-
e) Provide Installation Information, but only if you would otherwise
|
117
|
-
be required to provide such information under section 6 of the
|
118
|
-
GNU GPL, and only to the extent that such information is
|
119
|
-
necessary to install and execute a modified version of the
|
120
|
-
Combined Work produced by recombining or relinking the
|
121
|
-
Application with a modified version of the Linked Version. (If
|
122
|
-
you use option 4d0, the Installation Information must accompany
|
123
|
-
the Minimal Corresponding Source and Corresponding Application
|
124
|
-
Code. If you use option 4d1, you must provide the Installation
|
125
|
-
Information in the manner specified by section 6 of the GNU GPL
|
126
|
-
for conveying Corresponding Source.)
|
127
|
-
|
128
|
-
5. Combined Libraries.
|
129
|
-
|
130
|
-
You may place library facilities that are a work based on the
|
131
|
-
Library side by side in a single library together with other library
|
132
|
-
facilities that are not Applications and are not covered by this
|
133
|
-
License, and convey such a combined library under terms of your
|
134
|
-
choice, if you do both of the following:
|
135
|
-
|
136
|
-
a) Accompany the combined library with a copy of the same work based
|
137
|
-
on the Library, uncombined with any other library facilities,
|
138
|
-
conveyed under the terms of this License.
|
139
|
-
|
140
|
-
b) Give prominent notice with the combined library that part of it
|
141
|
-
is a work based on the Library, and explaining where to find the
|
142
|
-
accompanying uncombined form of the same work.
|
143
|
-
|
144
|
-
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
-
|
146
|
-
The Free Software Foundation may publish revised and/or new versions
|
147
|
-
of the GNU Lesser General Public License from time to time. Such new
|
148
|
-
versions will be similar in spirit to the present version, but may
|
149
|
-
differ in detail to address new problems or concerns.
|
150
|
-
|
151
|
-
Each version is given a distinguishing version number. If the
|
152
|
-
Library as you received it specifies that a certain numbered version
|
153
|
-
of the GNU Lesser General Public License "or any later version"
|
154
|
-
applies to it, you have the option of following the terms and
|
155
|
-
conditions either of that published version or of any later version
|
156
|
-
published by the Free Software Foundation. If the Library as you
|
157
|
-
received it does not specify a version number of the GNU Lesser
|
158
|
-
General Public License, you may choose any version of the GNU Lesser
|
159
|
-
General Public License ever published by the Free Software Foundation.
|
160
|
-
|
161
|
-
If the Library as you received it specifies that a proxy can decide
|
162
|
-
whether future versions of the GNU Lesser General Public License shall
|
163
|
-
apply, that proxy's public statement of acceptance of any version is
|
164
|
-
permanent authorization for you to choose that version for the
|
165
|
-
Library.
|
data/README
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
== Spockets ==
|
2
|
-
|
3
|
-
Spockets is a simple library for dealing with multiple sockets.
|
4
|
-
You supply a socket, and one or more blocks to
|
5
|
-
execute, and Spockets will make sure those blocks get
|
6
|
-
executed when something comes in over the wire. It's just
|
7
|
-
that simple.
|
8
|
-
|
9
|
-
There is one requirement for spockets and that is the ActionPool.
|
10
|
-
This just allows spockets to use a thread pool for executing
|
11
|
-
blocks so you don't end up having to wait on slow blocks. You
|
12
|
-
can even provide your own pool for spockets to use, so all the
|
13
|
-
action can stay in one local pool.
|
14
|
-
|
15
|
-
install (easy):
|
16
|
-
|
17
|
-
gem install spockets
|
18
|
-
|
19
|
-
install (less easy):
|
20
|
-
|
21
|
-
git clone http://github.com/spox/spockets.git
|
22
|
-
cd spockets
|
23
|
-
gem build spockets.gemspec
|
24
|
-
gem install spockets-x.x.x.gem
|
25
|
-
|
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
|
36
|
-
|
37
|
-
Examples are usually helpful, so here we go:
|
38
|
-
|
39
|
-
Code:
|
40
|
-
|
41
|
-
require 'socket'
|
42
|
-
require 'spockets'
|
43
|
-
spockets = Spockets::Spockets.new
|
44
|
-
|
45
|
-
se = TCPServer.new(3000)
|
46
|
-
loop do
|
47
|
-
s = se.accept
|
48
|
-
puts "Socket: #{s}"
|
49
|
-
spockets.add(s){|string| puts "#{s}: #{string}" }
|
50
|
-
end
|
51
|
-
sleep
|
52
|
-
|
53
|
-
|
54
|
-
Connecting:
|
55
|
-
|
56
|
-
> telnet 192.168.0.95 3000
|
57
|
-
Trying 192.168.0.95...
|
58
|
-
Connected to 192.168.0.95.
|
59
|
-
Escape character is '^]'.
|
60
|
-
goodbyeworld
|
61
|
-
^]
|
62
|
-
telnet> quit
|
63
|
-
Connection closed.
|
64
|
-
|
65
|
-
> telnet 192.168.0.95 3000
|
66
|
-
Trying 192.168.0.95...
|
67
|
-
Connected to 192.168.0.95.
|
68
|
-
Escape character is '^]'.
|
69
|
-
foobar
|
70
|
-
complete
|
71
|
-
^]
|
72
|
-
telnet> quit
|
73
|
-
Connection closed.
|
74
|
-
|
75
|
-
Output:
|
76
|
-
|
77
|
-
Socket: #<TCPSocket:0x98ec5ac>
|
78
|
-
Socket: #<TCPSocket:0x98ec37c>
|
79
|
-
#<TCPSocket:0x98ec37c>: foobar
|
80
|
-
#<TCPSocket:0x98ec5ac>: goodbyeworld
|
81
|
-
#<TCPSocket:0x98ec37c>: complete
|
data/README~
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
== Spockets ==
|
2
|
-
|
3
|
-
Spockets is a simple library for dealing with multiple sockets.
|
4
|
-
You supply a socket, and one or more blocks to
|
5
|
-
execute, and Spockets will make sure those blocks get
|
6
|
-
executed when something comes in over the wire. It's just
|
7
|
-
that simple.
|
8
|
-
|
9
|
-
There is one requirement for spockets and that is the ActionPool.
|
10
|
-
This just allows spockets to use a thread pool for executing
|
11
|
-
blocks so you don't end up having to wait on slow blocks. You
|
12
|
-
can even provide your own pool for spockets to use, so all the
|
13
|
-
action can stay in one local pool.
|
14
|
-
|
15
|
-
install (easy):
|
16
|
-
|
17
|
-
gem install spockets
|
18
|
-
|
19
|
-
install (less easy):
|
20
|
-
|
21
|
-
git clone http://github.com/spox/spockets.git
|
22
|
-
cd spockets
|
23
|
-
gem build spockets.gemspec
|
24
|
-
gem install spockets-x.x.x.gem
|
25
|
-
|
26
|
-
It has RDocs. They are where ever your rubygems installed them.
|
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
|
data/lib/spockets/Spockets.rb~
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
require 'spockets/Exceptions'
|
2
|
-
require 'spockets/Watcher'
|
3
|
-
|
4
|
-
module Spockets
|
5
|
-
|
6
|
-
class Spockets
|
7
|
-
|
8
|
-
# :pool:: ActionPool if you would like to consolidate
|
9
|
-
# :clean:: Clean string. Set to true for default or
|
10
|
-
# provide a block to clean strings
|
11
|
-
# creates a new holder
|
12
|
-
def initialize(args={})
|
13
|
-
@sockets = {}
|
14
|
-
@watcher = Watcher.new(:sockets => @sockets, :clean => args[:clean], :pool => args[:pool])
|
15
|
-
end
|
16
|
-
|
17
|
-
# socket:: socket to listen to
|
18
|
-
# block:: block to execute when activity is received
|
19
|
-
# Adds a socket to the list to listen to. When a string
|
20
|
-
# is received on the socket, it will send it to the block
|
21
|
-
# for processing
|
22
|
-
def add(socket, &block)
|
23
|
-
raise DuplicateSocket.new(socket) if @sockets.has_key?(socket)
|
24
|
-
@sockets[socket][:procs] = [block]
|
25
|
-
begin
|
26
|
-
@watcher.sync
|
27
|
-
rescue NotRunning
|
28
|
-
start
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# socket:: socket in list
|
33
|
-
# block:: additional block to execute
|
34
|
-
# This will add additional blocks to the associated
|
35
|
-
# socket to be executed when a new string is received
|
36
|
-
def extra(socket, &block)
|
37
|
-
raise UnknownSocket.new(socket) unless @sockets.has_key?(socket)
|
38
|
-
@sockets[socket][:procs] << block
|
39
|
-
end
|
40
|
-
|
41
|
-
# socket:: socket to remove
|
42
|
-
# Removes socket from list
|
43
|
-
def remove(socket)
|
44
|
-
raise UnknownSocket.new(socket) unless @sockets.has_key?(socket)
|
45
|
-
@sockets.delete(socket)
|
46
|
-
begin
|
47
|
-
@watcher.sync
|
48
|
-
rescue NotRunning
|
49
|
-
start
|
50
|
-
end
|
51
|
-
end
|
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
|
-
|
62
|
-
# remove all sockets
|
63
|
-
def clear
|
64
|
-
@sockets.clear
|
65
|
-
stop
|
66
|
-
end
|
67
|
-
|
68
|
-
# start spockets
|
69
|
-
def start
|
70
|
-
raise AlreadyRunning.new if @watcher.running?
|
71
|
-
@watcher.start
|
72
|
-
end
|
73
|
-
|
74
|
-
# stop spockets
|
75
|
-
def stop
|
76
|
-
raise NotRunning.new unless @watcher.running?
|
77
|
-
@watcher.stop
|
78
|
-
end
|
79
|
-
|
80
|
-
# currently watching sockets
|
81
|
-
def running?
|
82
|
-
!@watcher.nil? && @watcher.running?
|
83
|
-
end
|
84
|
-
|
85
|
-
# socket:: a socket
|
86
|
-
# check if the given socket is being watched
|
87
|
-
def include?(socket)
|
88
|
-
@sockets.has_key?(socket)
|
89
|
-
end
|
90
|
-
|
91
|
-
alias :delete :remove
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
data/lib/spockets/Watcher.rb~
DELETED
@@ -1,89 +0,0 @@
|
|
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
|
data/spockets-0.0.2.gem
DELETED
Binary file
|
data/spockets-0.0.3.gem
DELETED
Binary file
|
data/spockets-0.0.4.gem
DELETED
Binary file
|
data/spockets.gemspec
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
spec = Gem::Specification.new do |s|
|
2
|
-
s.name = 'spockets'
|
3
|
-
s.author = %q(spox)
|
4
|
-
s.email = %q(spox@modspox.com)
|
5
|
-
s.version = '0.0.5'
|
6
|
-
s.summary = %q(Socket Helper)
|
7
|
-
s.platform = Gem::Platform::RUBY
|
8
|
-
s.has_rdoc = true
|
9
|
-
s.rdoc_options = %w(--title Spockets --main README --line-numbers)
|
10
|
-
s.extra_rdoc_files = %w(README)
|
11
|
-
s.files = Dir['**/*']
|
12
|
-
s.require_paths = %w(lib)
|
13
|
-
s.add_dependency 'ActionPool'
|
14
|
-
s.homepage = %q(http://dev.modspox.com/trac/spockets)
|
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(" ")
|
24
|
-
end
|
data/spockets.gemspec~
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
spec = Gem::Specification.new do |s|
|
2
|
-
s.name = 'spockets'
|
3
|
-
s.author = %q(spox)
|
4
|
-
s.email = %q(spox@modspox.com)
|
5
|
-
s.version = '0.0.4'
|
6
|
-
s.summary = %q(Socket Helper)
|
7
|
-
s.platform = Gem::Platform::RUBY
|
8
|
-
s.has_rdoc = true
|
9
|
-
s.rdoc_options = %w(--title Spockets --main README --line-numbers)
|
10
|
-
s.extra_rdoc_files = %w(README)
|
11
|
-
s.files = Dir['**/*']
|
12
|
-
s.require_paths = %w(lib)
|
13
|
-
s.add_dependency 'ActionPool'
|
14
|
-
s.homepage = %q(http://dev.modspox.com/trac/spockets)
|
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(" ")
|
24
|
-
end
|