packet 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +4 -0
- data/Rakefile +16 -8
- data/TODO +8 -0
- data/examples/asteroid.c +296 -0
- data/examples/asteroid.h +5 -0
- data/examples/concurrent_thread.c +53 -0
- data/examples/extconf.h +4 -0
- data/examples/extconf.rb +10 -0
- data/examples/persistent_print.rb +24 -0
- data/examples/sample_server.rb +19 -0
- data/examples/use_stuff.rb +3 -0
- data/lib/bin_parser.rb +16 -8
- data/lib/class_helpers.rb +74 -0
- data/lib/connection.rb +18 -18
- data/lib/core.rb +38 -13
- data/lib/double_keyed_hash.rb +6 -0
- data/lib/meta_pimp.rb +20 -6
- data/lib/nbio.rb +27 -30
- data/lib/packet.rb +6 -6
- data/lib/packet_master.rb +55 -58
- data/lib/pimp.rb +1 -0
- data/lib/thread_pool.rb +54 -0
- data/lib/timer_store.rb +63 -0
- data/lib/worker.rb +4 -28
- data/lib/worker_pool.rb +10 -0
- metadata +66 -55
- data/LICENSE +0 -4
- data/bin/packet_mongrel.rb +0 -215
- data/bin/runner.rb +0 -35
- data/lib/attribute_accessors.rb +0 -48
- data/lib/buftok.rb +0 -127
- data/lib/cpu_worker.rb +0 -19
- data/lib/deferrable.rb +0 -210
- data/lib/io_worker.rb +0 -6
- data/lib/ruby_hacks.rb +0 -125
data/lib/worker_pool.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# implements worker pools
|
2
|
+
# Warning: Work in Progress
|
3
|
+
# FIXME: but often what user needs is ability to have different workers and control their execution seperately.
|
4
|
+
# Above requirment comes from BackgrounDrb experience, and may not be used otherwise.
|
5
|
+
|
6
|
+
module Packet
|
7
|
+
class WorkerPool
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
metadata
CHANGED
@@ -1,75 +1,86 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.4
|
3
|
-
specification_version: 1
|
4
2
|
name: packet
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2007-11-21 00:00:00 +05:30
|
8
|
-
summary: Packet, Events... we got em.
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: foo@bar.com
|
12
|
-
homepage: http://code.google.com/p/packet/
|
13
|
-
rubyforge_project:
|
14
|
-
description: Packet, Events... we got em.
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 1.8.4
|
24
|
-
version:
|
4
|
+
version: 0.1.1
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
|
-
- Hemant
|
7
|
+
- Hemant Kumar
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2007-12-20 00:00:00 +05:30
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Packet, A Pure Ruby library for Event Driven Network Programming.
|
17
|
+
email: mail@gnufied.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- MIT-LICENSE
|
25
|
+
- TODO
|
31
26
|
files:
|
32
|
-
- LICENSE
|
27
|
+
- MIT-LICENSE
|
33
28
|
- README
|
34
29
|
- Rakefile
|
35
30
|
- TODO
|
36
|
-
- bin/packet_mongrel.rb
|
37
|
-
- bin/runner.rb
|
38
|
-
- lib/buftok.rb
|
39
|
-
- lib/io_worker.rb
|
40
31
|
- lib/disconnect_error.rb
|
41
|
-
- lib/
|
42
|
-
- lib/
|
32
|
+
- lib/nbio.rb
|
33
|
+
- lib/worker.rb
|
43
34
|
- lib/meta_pimp.rb
|
44
|
-
- lib/pimp.rb
|
45
|
-
- lib/packet.rb
|
46
|
-
- lib/bin_parser.rb
|
47
35
|
- lib/packet_guid.rb
|
48
|
-
- lib/
|
36
|
+
- lib/double_keyed_hash.rb
|
49
37
|
- lib/periodic_event.rb
|
38
|
+
- lib/callback.rb
|
50
39
|
- lib/connection.rb
|
51
|
-
- lib/
|
52
|
-
- lib/deferrable.rb
|
53
|
-
- lib/nbio.rb
|
54
|
-
- lib/ruby_hacks.rb
|
40
|
+
- lib/bin_parser.rb
|
55
41
|
- lib/packet_master.rb
|
56
|
-
- lib/
|
57
|
-
- lib/
|
58
|
-
- lib/
|
59
|
-
- lib/
|
60
|
-
|
61
|
-
|
42
|
+
- lib/class_helpers.rb
|
43
|
+
- lib/pimp.rb
|
44
|
+
- lib/packet.rb
|
45
|
+
- lib/event.rb
|
46
|
+
- lib/core.rb
|
47
|
+
- lib/thread_pool.rb
|
48
|
+
- lib/packet_mongrel.rb
|
49
|
+
- lib/timer_store.rb
|
50
|
+
- lib/worker_pool.rb
|
51
|
+
- examples/concurrent_thread.c
|
52
|
+
- examples/sample_server.rb
|
53
|
+
- examples/asteroid.h
|
54
|
+
- examples/persistent_print.rb
|
55
|
+
- examples/use_stuff.rb
|
56
|
+
- examples/extconf.h
|
57
|
+
- examples/asteroid.c
|
58
|
+
- examples/extconf.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://code.google.com/p/packet/
|
61
|
+
post_install_message:
|
62
62
|
rdoc_options: []
|
63
63
|
|
64
|
-
|
65
|
-
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 1.8.5
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
72
78
|
requirements: []
|
73
79
|
|
74
|
-
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 0.9.5
|
82
|
+
signing_key:
|
83
|
+
specification_version: 2
|
84
|
+
summary: Packet, A Pure Ruby library for Event Driven Network Programming.
|
85
|
+
test_files: []
|
75
86
|
|
data/LICENSE
DELETED
data/bin/packet_mongrel.rb
DELETED
@@ -1,215 +0,0 @@
|
|
1
|
-
# This module rewrites pieces of the very good Mongrel web server in
|
2
|
-
# order to change it from a threaded application to an event based
|
3
|
-
# application running inside an Packet event loop. It should
|
4
|
-
# be compatible with the existing Mongrel handlers for Rails,
|
5
|
-
# Camping, Nitro, etc....
|
6
|
-
PACKET_APP = File.expand_path(File.join(File.dirname(__FILE__) + "/.."))
|
7
|
-
["bin","config","parser","worker","framework","lib","pimp"].each { |x| $LOAD_PATH.unshift(PACKET_APP + "/#{x}")}
|
8
|
-
require "packet"
|
9
|
-
require 'packet'
|
10
|
-
|
11
|
-
module Mongrel
|
12
|
-
class MongrelProtocol < Packet::Connection
|
13
|
-
def post_init
|
14
|
-
@parser = HttpParser.new
|
15
|
-
@params = HttpParams.new
|
16
|
-
@nparsed = 0
|
17
|
-
@request = nil
|
18
|
-
@request_len = nil
|
19
|
-
@linebuffer = ''
|
20
|
-
end
|
21
|
-
|
22
|
-
def receive_data data
|
23
|
-
@linebuffer << data
|
24
|
-
@nparsed = @parser.execute(@params, @linebuffer, @nparsed) unless @parser.finished?
|
25
|
-
if @parser.finished?
|
26
|
-
if @request_len.nil?
|
27
|
-
@request_len = @params[::Mongrel::Const::CONTENT_LENGTH].to_i
|
28
|
-
script_name, path_info, handlers = ::Mongrel::HttpServer::Instance.classifier.resolve(@params[::Mongrel::Const::REQUEST_PATH])
|
29
|
-
if handlers
|
30
|
-
@params[::Mongrel::Const::PATH_INFO] = path_info
|
31
|
-
@params[::Mongrel::Const::SCRIPT_NAME] = script_name
|
32
|
-
@params[::Mongrel::Const::REMOTE_ADDR] = @params[::Mongrel::Const::HTTP_X_FORWARDED_FOR] #|| ::Socket.unpack_sockaddr_in(get_peername)[1]
|
33
|
-
@notifiers = handlers.select { |h| h.request_notify }
|
34
|
-
end
|
35
|
-
if @request_len > ::Mongrel::Const::MAX_BODY
|
36
|
-
new_buffer = Tempfile.new(::Mongrel::Const::MONGREL_TMP_BASE)
|
37
|
-
new_buffer.binmode
|
38
|
-
new_buffer << @linebuffer[@nparsed..-1]
|
39
|
-
@linebuffer = new_buffer
|
40
|
-
else
|
41
|
-
@linebuffer = StringIO.new(@linebuffer[@nparsed..-1])
|
42
|
-
@linebuffer.pos = @linebuffer.length
|
43
|
-
end
|
44
|
-
end
|
45
|
-
if @linebuffer.length >= @request_len
|
46
|
-
@linebuffer.rewind
|
47
|
-
::Mongrel::HttpServer::Instance.process_http_request(@params,@linebuffer,self)
|
48
|
-
@linebuffer.delete if Tempfile === @linebuffer
|
49
|
-
end
|
50
|
-
elsif @linebuffer.length > ::Mongrel::Const::MAX_HEADER
|
51
|
-
close_connection
|
52
|
-
raise ::Mongrel::HttpParserError.new("HEADER is longer than allowed, aborting client early.")
|
53
|
-
end
|
54
|
-
rescue ::Mongrel::HttpParserError
|
55
|
-
if $mongrel_debug_client
|
56
|
-
STDERR.puts "#{Time.now}: BAD CLIENT (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #$!"
|
57
|
-
STDERR.puts "#{Time.now}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n"
|
58
|
-
end
|
59
|
-
close_connection
|
60
|
-
rescue Exception => e
|
61
|
-
close_connection
|
62
|
-
raise e
|
63
|
-
end
|
64
|
-
|
65
|
-
def write data
|
66
|
-
send_data data
|
67
|
-
end
|
68
|
-
|
69
|
-
def closed?
|
70
|
-
false
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
class HttpServer
|
76
|
-
def initialize(host, port, num_processors=950, x=0, y=nil) # Deal with Mongrel 1.0.1 or earlier, as well as later.
|
77
|
-
@socket = nil
|
78
|
-
@classifier = URIClassifier.new
|
79
|
-
@host = host
|
80
|
-
@port = port
|
81
|
-
@workers = ThreadGroup.new
|
82
|
-
if y
|
83
|
-
@throttle = x
|
84
|
-
@timeout = y || 60
|
85
|
-
else
|
86
|
-
@timeout = x
|
87
|
-
end
|
88
|
-
@num_processors = num_processors #num_processors is pointless for evented....
|
89
|
-
@death_time = 60
|
90
|
-
self.class.const_set(:Instance,self)
|
91
|
-
end
|
92
|
-
|
93
|
-
def run
|
94
|
-
trap('INT') { raise StopServer }
|
95
|
-
trap('TERM') { raise StopServer }
|
96
|
-
@acceptor = Thread.new do
|
97
|
-
Packet::Reactor.run do |t_reactor|
|
98
|
-
begin
|
99
|
-
t_reactor.start_server(@host,@port,MongrelProtocol)
|
100
|
-
rescue StopServer
|
101
|
-
t_reactor.start_server
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
def process_http_request(params,linebuffer,client)
|
108
|
-
if not params[Const::REQUEST_PATH]
|
109
|
-
uri = URI.parse(params[Const::REQUEST_URI])
|
110
|
-
params[Const::REQUEST_PATH] = uri.request_uri
|
111
|
-
end
|
112
|
-
|
113
|
-
raise "No REQUEST PATH" if not params[Const::REQUEST_PATH]
|
114
|
-
|
115
|
-
script_name, path_info, handlers = @classifier.resolve(params[Const::REQUEST_PATH])
|
116
|
-
|
117
|
-
if handlers
|
118
|
-
notifiers = handlers.select { |h| h.request_notify }
|
119
|
-
request = HttpRequest.new(params, linebuffer, notifiers)
|
120
|
-
|
121
|
-
# request is good so far, continue processing the response
|
122
|
-
response = HttpResponse.new(client)
|
123
|
-
|
124
|
-
# Process each handler in registered order until we run out or one finalizes the response.
|
125
|
-
dispatch_to_handlers(handlers,request,response)
|
126
|
-
|
127
|
-
# And finally, if nobody closed the response off, we finalize it.
|
128
|
-
unless response.done
|
129
|
-
response.finished
|
130
|
-
else
|
131
|
-
response.close_connection
|
132
|
-
end
|
133
|
-
else
|
134
|
-
# Didn't find it, return a stock 404 response.
|
135
|
-
client.send_data(Const::ERROR_404_RESPONSE)
|
136
|
-
client.close_connection
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def dispatch_to_handlers(handlers,request,response)
|
141
|
-
handlers.each do |handler|
|
142
|
-
handler.process(request, response)
|
143
|
-
break if response.done
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
class HttpRequest
|
149
|
-
def initialize(params, linebuffer, dispatchers)
|
150
|
-
@params = params
|
151
|
-
@dispatchers = dispatchers
|
152
|
-
@body = linebuffer
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
class HttpResponse
|
157
|
-
def send_file(path, small_file = false)
|
158
|
-
File.open(path, "rb") do |f|
|
159
|
-
while chunk = f.read(Const::CHUNK_SIZE) and chunk.length > 0
|
160
|
-
begin
|
161
|
-
write(chunk)
|
162
|
-
rescue Object => exc
|
163
|
-
break
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
@body_sent = true
|
168
|
-
end
|
169
|
-
|
170
|
-
def write(data)
|
171
|
-
@socket.send_data data
|
172
|
-
end
|
173
|
-
|
174
|
-
def close_connection_after_writing
|
175
|
-
@socket.close_connection
|
176
|
-
end
|
177
|
-
|
178
|
-
def socket_error(details)
|
179
|
-
@socket.close_connection
|
180
|
-
done = true
|
181
|
-
raise details
|
182
|
-
end
|
183
|
-
|
184
|
-
def finished
|
185
|
-
send_status
|
186
|
-
send_header
|
187
|
-
send_body
|
188
|
-
@socket.close_connection
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
class Configurator
|
193
|
-
# This version fixes a bug in the regular Mongrel version by adding
|
194
|
-
# initialization of groups.
|
195
|
-
def change_privilege(user, group)
|
196
|
-
if user and group
|
197
|
-
log "Initializing groups for {#user}:{#group}."
|
198
|
-
Process.initgroups(user,Etc.getgrnam(group).gid)
|
199
|
-
end
|
200
|
-
|
201
|
-
if group
|
202
|
-
log "Changing group to #{group}."
|
203
|
-
Process::GID.change_privilege(Etc.getgrnam(group).gid)
|
204
|
-
end
|
205
|
-
|
206
|
-
if user
|
207
|
-
log "Changing user to #{user}."
|
208
|
-
Process::UID.change_privilege(Etc.getpwnam(user).uid)
|
209
|
-
end
|
210
|
-
rescue Errno::EPERM
|
211
|
-
log "FAILED to change user:group #{user}:#{group}: #$!"
|
212
|
-
exit 1
|
213
|
-
end
|
214
|
-
end
|
215
|
-
end
|
data/bin/runner.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
EVAL_APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__) + "/.."))
|
2
|
-
["bin","worker","lib"].each { |x| $LOAD_PATH.unshift(EVAL_APP_ROOT + "/#{x}")}
|
3
|
-
WORKER_ROOT = EVAL_APP_ROOT + "/worker"
|
4
|
-
|
5
|
-
require "packet"
|
6
|
-
class Foo
|
7
|
-
def receive_data p_data
|
8
|
-
ask_worker(:no_proxy_worker,:data => p_data, :type => :request)
|
9
|
-
end
|
10
|
-
|
11
|
-
def worker_receive p_data
|
12
|
-
send_data "#{p_data[:data]}\n"
|
13
|
-
end
|
14
|
-
|
15
|
-
def show_result p_data
|
16
|
-
send_data("#{p_data[:response]}\n")
|
17
|
-
end
|
18
|
-
|
19
|
-
def connection_completed
|
20
|
-
end
|
21
|
-
|
22
|
-
def post_init
|
23
|
-
end
|
24
|
-
|
25
|
-
def wow
|
26
|
-
puts "Wow"
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
Packet::Reactor.run do |t_reactor|
|
31
|
-
t_reactor.start_server("localhost", 11006,Foo) do |instance|
|
32
|
-
instance.wow
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
data/lib/attribute_accessors.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
# Extends the class object with class and instance accessors for class attributes,
|
2
|
-
# just like the native attr* accessors for instance attributes.
|
3
|
-
class Class # :nodoc:
|
4
|
-
def cattr_reader(*syms)
|
5
|
-
syms.flatten.each do |sym|
|
6
|
-
next if sym.is_a?(Hash)
|
7
|
-
class_eval(<<-EOS, __FILE__, __LINE__)
|
8
|
-
unless defined? @@#{sym}
|
9
|
-
@@#{sym} = nil
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.#{sym}
|
13
|
-
@@#{sym}
|
14
|
-
end
|
15
|
-
|
16
|
-
def #{sym}
|
17
|
-
@@#{sym}
|
18
|
-
end
|
19
|
-
EOS
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def cattr_writer(*syms)
|
24
|
-
options = syms.last.is_a?(Hash) ? syms.pop : {}
|
25
|
-
syms.flatten.each do |sym|
|
26
|
-
class_eval(<<-EOS, __FILE__, __LINE__)
|
27
|
-
unless defined? @@#{sym}
|
28
|
-
@@#{sym} = nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.#{sym}=(obj)
|
32
|
-
@@#{sym} = obj
|
33
|
-
end
|
34
|
-
|
35
|
-
#{"
|
36
|
-
def #{sym}=(obj)
|
37
|
-
@@#{sym} = obj
|
38
|
-
end
|
39
|
-
" unless options[:instance_writer] == false }
|
40
|
-
EOS
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def cattr_accessor(*syms)
|
45
|
-
cattr_reader(*syms)
|
46
|
-
cattr_writer(*syms)
|
47
|
-
end
|
48
|
-
end
|