wildsonet-streamer 0.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ gem "wildsonet-netty"
4
+ gem "wildsonet-hazelcast"
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Marek Jelen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,92 @@
1
+ = WildSoNet::Streamer
2
+
3
+ Simple way to stream data from server to client's browser.
4
+
5
+ == Status
6
+
7
+ * No documentation
8
+ * No tests
9
+
10
+ Not production ready ... experimental ... proof of concept.
11
+
12
+ == Browsers
13
+
14
+ === Mac
15
+ * Safari OK
16
+ * Chrome OK
17
+ * Firefox OK
18
+ * Opera Not tested
19
+
20
+ === Windows
21
+ * IE Not tested
22
+ * Chrome Not tested
23
+ * Firefox Not tested
24
+ * Safari Not tested
25
+ * Opera
26
+
27
+ === Linux
28
+ * Firefox Not tested
29
+ * Chrome Not tested
30
+ * Opera
31
+ * Konqueror Not tested
32
+
33
+ === iOS
34
+ * WebKit Not tested
35
+ * Opera Not tested
36
+
37
+ === Android
38
+ * WebKit Not tested
39
+ * Opera Not tested
40
+
41
+ == Usage
42
+
43
+ - Start server at specific address and port
44
+ - Configure front-end server to proxy requests to streamer
45
+ - Request stream from your website
46
+ - Push messages from your application using Hazelcast
47
+
48
+ === Starting Streamer server
49
+
50
+ address = "0.0.0.0"
51
+ port = 3001
52
+ WildSoNet::Streamer::Server.start(port, address)
53
+
54
+ === Configure nginx for proxying
55
+
56
+ location /streamer {
57
+ proxy_pass http://address:port;
58
+ chunked_transfer_encoding on;
59
+ proxy_buffering off;
60
+ }
61
+
62
+ === Embed to your site
63
+
64
+ <script type="text/javascript" src="/streamer.js"></script>
65
+ <script type="text/javascript">
66
+ Streamer.url = "/streamer" //URL to used to access streams, configured in previous step
67
+ Streamer.start(client_id) // Starts stream with client ID
68
+ </script>
69
+
70
+ === Push messages from application
71
+
72
+ When streamer is embedded into application
73
+
74
+ WildSoNet::Streamer::Server.push(client_id, message)
75
+
76
+ or when messages are distributed
77
+
78
+ WildSoNet::Hazelcast.queue("streamer").put(Marshal.dump([client_id, message]))
79
+
80
+ == Contributing to wildsonet-streamer
81
+
82
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
83
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
84
+ * Fork the project
85
+ * Start a feature/bugfix branch
86
+ * Commit and push until you are happy with your contribution
87
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
88
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
89
+
90
+ == Copyright
91
+
92
+ Copyright (c) 2010 Marek Jelen. See LICENSE.txt for further details.
data/lib/handler.rb ADDED
@@ -0,0 +1,77 @@
1
+ module WildSoNet
2
+ module Streamer
3
+
4
+ java_import "org.jboss.netty.channel.SimpleChannelUpstreamHandler"
5
+ java_import "org.jboss.netty.channel.ChannelFutureListener"
6
+
7
+ class Handler < SimpleChannelUpstreamHandler
8
+
9
+ attr_accessor :encoder
10
+
11
+ def messageReceived context, event
12
+
13
+ message = event.getMessage
14
+
15
+ if result = /^GET \/streamer\/([a-z]+)\/(.+) HTTP\/1\..$/.match(message)
16
+ connection = Connection.new(context, @encoder)
17
+ connection.type = result[1]
18
+ connection.id = result[2]
19
+ context.attachment = connection
20
+ end
21
+
22
+ if message == ""
23
+ context.channel.write("HTTP/1.1 200 OK\r\n")
24
+ context.channel.write("Content-Type: text/html; charset=UTF-8\r\n")
25
+ context.channel.write("\r\n")
26
+ WildSoNet::Streamer::Server.register(context.attachment.id, context.attachment)
27
+ end
28
+
29
+ end
30
+
31
+ def channelClosed context, event
32
+ WildSoNet::Streamer::Server.unregister(context.attachment.id, context.attachment)
33
+ end
34
+
35
+ end
36
+
37
+ class Connection
38
+
39
+ include ChannelFutureListener
40
+
41
+ attr_accessor :id
42
+ attr_accessor :type
43
+ attr_reader :context
44
+
45
+ def initialize context, encoder
46
+ @context = context
47
+ @encoder = encoder
48
+ end
49
+
50
+ def write data
51
+ case @type
52
+ when "message"
53
+ data = @encoder.message(data)
54
+ @context.channel.write(data).addListener(self)
55
+ when "script"
56
+ data = @encoder.script(data)
57
+ @context.channel.write(data)
58
+ end
59
+ end
60
+
61
+ def operationComplete future
62
+ future.channel.close if future.isDone()
63
+ end
64
+
65
+ end
66
+
67
+ class Encoder
68
+ def self.script data
69
+ "<script type=\"text/javascript\">parent.Streamer.handle(\"#{data}\");</script>"
70
+ end
71
+ def self.message data
72
+ data
73
+ end
74
+ end
75
+
76
+ end
77
+ end
data/lib/server.rb ADDED
@@ -0,0 +1,95 @@
1
+ module WildSoNet
2
+ module Streamer
3
+
4
+ java_import "org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory"
5
+ java_import "org.jboss.netty.bootstrap.ServerBootstrap"
6
+
7
+ java_import "org.jboss.netty.channel.Channels"
8
+ java_import "org.jboss.netty.channel.ChannelPipelineFactory"
9
+ java_import "org.jboss.netty.handler.codec.string.StringDecoder"
10
+ java_import "org.jboss.netty.handler.codec.string.StringEncoder"
11
+ java_import "org.jboss.netty.handler.codec.frame.Delimiters"
12
+ java_import "org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder"
13
+
14
+ java_import "org.jboss.netty.channel.SimpleChannelUpstreamHandler"
15
+
16
+ java_import "java.util.concurrent.Executors"
17
+ java_import "java.net.InetSocketAddress"
18
+
19
+ class Server
20
+
21
+ def initialize port, address, encoder
22
+ @factory = NioServerSocketChannelFactory.new(Executors.newCachedThreadPool(), Executors.newCachedThreadPool())
23
+ @bootstrap = ServerBootstrap.new(@factory)
24
+ @bootstrap.setPipelineFactory(PipelineFactory.new(encoder))
25
+ @bootstrap.bind(InetSocketAddress.new(address, port))
26
+ end
27
+
28
+ def self.start(port, address = "0.0.0.0", encoder=WildSoNet::Streamer::Encoder)
29
+ @@servers ||= {}
30
+ @@servers["#{address}:#{port}"] = Server.new(port, address, encoder)
31
+ @@connections = {}
32
+ @@receiver = Receiver.new
33
+ end
34
+
35
+ def self.push(cid, message)
36
+
37
+ @@connections[cid.to_s].each do |con|
38
+ con.write(message)
39
+ end if @@connections[cid.to_s]
40
+
41
+ end
42
+
43
+ def self.register(cid, connection)
44
+ @@connections[cid.to_s] ||= []
45
+ @@connections[cid.to_s] << connection
46
+ end
47
+
48
+ def self.unregister(cid, connection)
49
+ return if not @@connections[cid.to_s]
50
+ @@connections[cid.to_s].delete(connection)
51
+ end
52
+
53
+ end
54
+
55
+ class Receiver
56
+ def initialize
57
+ @thread = Thread.new do
58
+ while true
59
+ msg = WildSoNet::Hazelcast.queue("streamer").take()
60
+ msg = Marshal.load(msg)
61
+ WildSoNet::Streamer::Server.push(msg[0], msg[1])
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ class PipelineFactory
68
+
69
+ include ChannelPipelineFactory
70
+
71
+ def initialize encoder
72
+ @encoder = encoder
73
+ end
74
+
75
+ def getPipeline
76
+
77
+ pip = Channels.pipeline
78
+
79
+ pip.addLast("framer", DelimiterBasedFrameDecoder.new(8192, Delimiters.lineDelimiter()))
80
+
81
+ pip.addLast("decoder", StringDecoder.new)
82
+ pip.addLast("encoder", StringEncoder.new)
83
+
84
+ handler = Handler.new
85
+ handler.encoder = @encoder
86
+
87
+ pip.addLast("handler", handler)
88
+
89
+ pip
90
+
91
+ end
92
+
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ module WildSoNet
2
+ module Streamer
3
+ VERSION = "0.0.1" unless defined?(::WildSoNet::Streamer::VERSION)
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require "java"
2
+
3
+ require "wildsonet-streamer-version"
4
+
5
+ require "handler"
6
+ require "server"
@@ -0,0 +1,44 @@
1
+ lib = File.join(File.dirname(__FILE__), "lib")
2
+ $: << lib unless $:.include?(lib)
3
+
4
+ require "wildsonet-streamer-version"
5
+
6
+ Gem::Specification.new do |s|
7
+
8
+ s.name = "wildsonet-streamer"
9
+ s.version = WildSoNet::Streamer::VERSION
10
+ s.authors = ["Marek Jelen"]
11
+ s.summary = "Stream data to client's browsers"
12
+ s.description = "Simple way to stream data to client's browsers"
13
+ s.email = "marek@jelen.biz"
14
+ s.homepage = "http://github.com/marekjelen/wildsonet-streamer"
15
+ s.licenses = ["MIT"]
16
+
17
+ s.platform = "java"
18
+ s.required_rubygems_version = ">= 1.3.6"
19
+
20
+ s.extra_rdoc_files = [
21
+ "LICENSE.txt",
22
+ "README.rdoc"
23
+ ]
24
+
25
+ s.files = [
26
+ "Gemfile",
27
+ "LICENSE.txt",
28
+ "README.rdoc",
29
+ "lib/wildsonet-streamer.rb",
30
+ "lib/wildsonet-streamer-version.rb",
31
+ "lib/handler.rb",
32
+ "lib/server.rb",
33
+ "wildsonet-streamer.gemspec"
34
+ ]
35
+
36
+ s.require_paths = ["lib"]
37
+
38
+ s.test_files = [
39
+ ]
40
+
41
+ s.add_runtime_dependency("wildsonet-netty", ["> 0.0"])
42
+ s.add_runtime_dependency("wildsonet-hazelcast", ["> 0.0"])
43
+ end
44
+
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wildsonet-streamer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: java
11
+ authors:
12
+ - Marek Jelen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-03 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: wildsonet-netty
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">"
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 0
31
+ version: "0.0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: wildsonet-hazelcast
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">"
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ - 0
45
+ version: "0.0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Simple way to stream data to client's browsers
49
+ email: marek@jelen.biz
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - LICENSE.txt
56
+ - README.rdoc
57
+ files:
58
+ - Gemfile
59
+ - LICENSE.txt
60
+ - README.rdoc
61
+ - lib/wildsonet-streamer.rb
62
+ - lib/wildsonet-streamer-version.rb
63
+ - lib/handler.rb
64
+ - lib/server.rb
65
+ - wildsonet-streamer.gemspec
66
+ has_rdoc: true
67
+ homepage: http://github.com/marekjelen/wildsonet-streamer
68
+ licenses:
69
+ - MIT
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 1
90
+ - 3
91
+ - 6
92
+ version: 1.3.6
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Stream data to client's browsers
100
+ test_files: []
101
+