thegrid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thegrid.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Petr Janda
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Thegrid
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'thegrid'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install thegrid
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ require "thegrid/version"
2
+
3
+ module Grid
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+
3
+ module Grid
4
+ class Command
5
+ attr_accessor :name, :params
6
+
7
+ def initialize(name, params)
8
+ @name, @params = name, params
9
+ end
10
+
11
+ def self.parse(body)
12
+ return if body == ''
13
+
14
+ data = JSON.parse(body)
15
+
16
+ cmd = Command.new(
17
+ data['name'].to_sym,
18
+ data['params']
19
+ )
20
+
21
+ cmd
22
+ end
23
+
24
+ def encode
25
+ JSON.unparse(
26
+ :name => @name,
27
+ :params => @params
28
+ )
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ module Grid
2
+ class CommandHandler
3
+ def register_command(name, &block)
4
+ @handlers ||= {}
5
+ @handlers[name] = block
6
+ end
7
+
8
+ def on_command(command)
9
+ @handlers[command.name].call(command.params)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Grid
2
+ class FakeStdin
3
+ def listen(address, &block)
4
+ @block = block
5
+ end
6
+
7
+ def send(name, params)
8
+ command = Command.new(
9
+ name, params
10
+ )
11
+
12
+ @block.call(command)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require 'grid/command_handler'
2
+ require 'grid/fake_stdin'
3
+ require 'grid/zmq/stdin'
4
+
5
+ module Grid
6
+ class Node
7
+ def initialize
8
+ @handler = CommandHandler.new
9
+ end
10
+
11
+ def listen(stdin, address)
12
+ stdin.listen(address) do |command|
13
+ @handler.on_command(command)
14
+ end
15
+ end
16
+
17
+ def command(name, &block)
18
+ @handler.register_command(name, &block)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Thegrid
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'json'
2
+ require 'grid/zmq/socket'
3
+
4
+ module Grid
5
+ module ZMQ
6
+ class Client < Socket
7
+ def initialize(address)
8
+ super(::ZMQ::PUB)
9
+
10
+ send(:bind, address)
11
+ end
12
+
13
+ def call(name, params)
14
+ send(:send_string, Command.new(name, params).encode)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ module Grid
2
+ module ZMQ
3
+ class Socket
4
+ def initialize(type)
5
+ ctx = ::ZMQ::Context.create
6
+ @socket = ctx.socket(type)
7
+ end
8
+
9
+ def send(method, *attrs)
10
+ error_check(@socket.send(method, *attrs))
11
+ end
12
+
13
+ private
14
+
15
+ def error_check(code)
16
+ if ::ZMQ::Util.resultcode_ok?(code)
17
+ false
18
+ else
19
+ STDERR.puts "Operation failed, errno [#{::ZMQ::Util.errno}] description [#{::ZMQ::Util.error_string}]"
20
+ caller(1).each { |callstack| STDERR.puts(callstack) }
21
+ true
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'ffi-rzmq'
2
+ require 'grid/zmq/socket'
3
+ require 'grid/command'
4
+
5
+ module Grid
6
+ module ZMQ
7
+ class Stdin < Socket
8
+ def initialize
9
+ super(::ZMQ::SUB)
10
+
11
+ send(:setsockopt, ::ZMQ::SUBSCRIBE, '')
12
+ end
13
+
14
+ def listen(address)
15
+ send(:connect, address)
16
+
17
+ loop do
18
+ send(:recv_string, body = '')
19
+ yield Command.parse(body)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ require 'grid/node'
3
+ require 'grid/zmq/client'
4
+
5
+ module Grid
6
+ describe Node do
7
+ it 'can register and get the command invoked on fake stdin' do
8
+ executed = 'old'
9
+
10
+ node = Node.new
11
+ node.command :foo do |params|
12
+ executed = params['value']
13
+ end
14
+
15
+ stdin = FakeStdin.new
16
+
17
+ node.listen(stdin, nil)
18
+
19
+ stdin.send(:foo, 'value' => 'new')
20
+
21
+ executed.should == 'new'
22
+ end
23
+
24
+ it 'can register and get the command invoked on zeromq stdin' do
25
+ address = 'tcp://127.0.0.1:2222'
26
+ executed = 'old'
27
+
28
+ node = Node.new
29
+ node.command :foo do |params|
30
+ executed = params['value']
31
+
32
+ Thread.current.kill
33
+ end
34
+
35
+ client = Grid::ZMQ::Client.new(address)
36
+ thread = Thread.new {
37
+ stdin = ZMQ::Stdin.new
38
+ node.listen(stdin, address)
39
+ }
40
+
41
+ sleep 0.1
42
+
43
+ client.call(:foo, :value => 'new')
44
+
45
+ thread.join
46
+
47
+ executed.should == 'new'
48
+ end
49
+ end
50
+ end
File without changes
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "thegrid"
8
+ spec.version = Thegrid::VERSION
9
+ spec.authors = ["Petr Janda"]
10
+ spec.email = ["petrjanda@me.com"]
11
+ spec.description = 'Grid'
12
+ spec.summary = 'Distributed system over ZeroMQ'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'ffi-rzmq', '1.0.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thegrid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Petr Janda
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ffi-rzmq
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Grid
79
+ email:
80
+ - petrjanda@me.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - lib/grid.rb
92
+ - lib/grid/command.rb
93
+ - lib/grid/command_handler.rb
94
+ - lib/grid/fake_stdin.rb
95
+ - lib/grid/node.rb
96
+ - lib/grid/version.rb
97
+ - lib/grid/zmq/client.rb
98
+ - lib/grid/zmq/socket.rb
99
+ - lib/grid/zmq/stdin.rb
100
+ - spec/lib/thegrid/node_spec.rb
101
+ - spec/spec_helper.rb
102
+ - thegrid.gemspec
103
+ homepage: ''
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.24
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Distributed system over ZeroMQ
128
+ test_files:
129
+ - spec/lib/thegrid/node_spec.rb
130
+ - spec/spec_helper.rb