avro_machine 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c15d2804073f87cb495d9c9792fa92001c919f2
4
+ data.tar.gz: 9f66a12c321ec0acaa3b11fd761617d56d05fb8c
5
+ SHA512:
6
+ metadata.gz: a9aa0579745842988bf885fa9ffc76da76644698e90c76d4eee10e2db54f15a780a4ed6dfd71c3498acbb0a8367c5289ee08da68d136e6d6b172d4fe2f01363e
7
+ data.tar.gz: 8c5daab1d869dc386a4d798d8687d49c291ee34b017645debe46d85041c6e60c7d328639158d52d93f6f134e868a8cba3f6032028948735dff9914fab528e175
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in avro_machine.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jeff Ching
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,61 @@
1
+ # AvroMachine
2
+
3
+ Easily create a server that accepts Avro messages
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'avro_machine'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install avro_machine
20
+
21
+ ## Usage
22
+
23
+ Create a responder class that inherits from `Avro::IPC::Responder`:
24
+
25
+ ```
26
+ class MyResponder < AvroMachine::Responder
27
+ def self.protocol
28
+ # return an Avro::Protocol
29
+ end
30
+
31
+ # assume this protocol defines a message1 that returns a string
32
+ def message1(request)
33
+ "foo"
34
+ end
35
+
36
+ # assume this protocols defines a message2 that returns an array<int>
37
+ def message2(request)
38
+ [1,2,3]
39
+ end
40
+ end
41
+ ```
42
+
43
+ Extend the `AvroMachine::Connection` class and define a `responder` class:
44
+
45
+ ```
46
+ class MyConnection < AvroMachine::Connection
47
+ def self.responder
48
+ MyResponder
49
+ end
50
+ end
51
+ ```
52
+
53
+ See the example in the example folder.
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/[my-github-username]/avro_machine/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -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 'avro_machine/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "avro_machine"
8
+ spec.version = AvroMachine::VERSION
9
+ spec.authors = ["Jeff Ching"]
10
+ spec.email = ["jching@avvo.com"]
11
+ spec.summary = %q{EventMachine server for Avro}
12
+ spec.description = %q{EventMachine server for Avro}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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 'avro', '~> 1.7.7'
22
+ spec.add_dependency 'eventmachine', '~> 1.0.0'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'avro_machine', path: '../'
@@ -0,0 +1,34 @@
1
+ Bundler.require
2
+
3
+ class Client
4
+ PROTOCOL = Avro::Protocol.parse(File.read(File.expand_path("../example.avpr", __FILE__)))
5
+
6
+ attr_reader :server, :port
7
+
8
+ def initialize(server, port)
9
+ @server = server
10
+ @port = port
11
+ end
12
+
13
+ def message1
14
+ requestor.request('message1', {})
15
+ end
16
+
17
+ def hello_world(name)
18
+ requestor.request('hello_world', {
19
+ "name" => name
20
+ })
21
+ end
22
+
23
+ def requestor
24
+ sock = TCPSocket.new(server, port)
25
+ client = Avro::IPC::SocketTransport.new(sock)
26
+ @requestor = Avro::IPC::Requestor.new(PROTOCOL, client)
27
+ end
28
+
29
+ end
30
+
31
+ require 'pp'
32
+ client = Client.new('localhost', 9090)
33
+ pp client.message1
34
+ pp client.hello_world('Jeff')
File without changes
@@ -0,0 +1,8 @@
1
+ @namespace("Example")
2
+
3
+ protocol Example {
4
+
5
+ array<int> message1();
6
+
7
+ string hello_world(string name);
8
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "protocol" : "Example",
3
+ "namespace" : "Example",
4
+ "types" : [ ],
5
+ "messages" : {
6
+ "message1" : {
7
+ "request" : [ ],
8
+ "response" : {
9
+ "type" : "array",
10
+ "items" : "int"
11
+ }
12
+ },
13
+ "hello_world" : {
14
+ "request" : [ {
15
+ "name" : "name",
16
+ "type" : "string"
17
+ } ],
18
+ "response" : "string"
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ Bundler.require
3
+
4
+ class MyResponder < AvroMachine::Responder
5
+ def self.protocol
6
+ Avro::Protocol.parse(File.read(File.expand_path("../example.avpr", __FILE__)))
7
+ end
8
+
9
+ def message1(params)
10
+ [1, 2, 3]
11
+ end
12
+
13
+ def hello_world(params)
14
+ "Hello, #{params['name']}"
15
+ end
16
+ end
17
+
18
+ class MyConnection < AvroMachine::Connection
19
+ def self.responder
20
+ MyResponder
21
+ end
22
+ end
23
+
24
+ port = ENV.fetch('PORT', 9090).to_i
25
+ EventMachine.run do
26
+ EventMachine.start_server('0.0.0.0', port, MyConnection)
27
+ end
@@ -0,0 +1,7 @@
1
+ require "eventmachine"
2
+ require "avro"
3
+
4
+ module AvroMachine
5
+ autoload :Connection, 'avro_machine/connection'
6
+ autoload :Responder, 'avro_machine/responder'
7
+ end
@@ -0,0 +1,39 @@
1
+ require 'eventmachine'
2
+
3
+ module AvroMachine
4
+ class Connection < EventMachine::Connection
5
+
6
+ def self.responder
7
+ raise NotImplemented, "need to implement a reponder class"
8
+ end
9
+
10
+ def initialize(*args)
11
+ @input = ""
12
+ super
13
+ end
14
+
15
+ # buffer data - we can receive incomplete request over the tcp port
16
+ def receive_data(data)
17
+ @input << data
18
+
19
+ # an avro request is terminated by a 0 byte
20
+ return unless @input[-4..-1].unpack('N')[0] == 0
21
+
22
+ handle_avro(@input)
23
+ end
24
+
25
+ def handle_avro(input)
26
+ reader = Avro::IPC::FramedReader.new(StringIO.new(input))
27
+ str = reader.read_framed_message
28
+
29
+ # handle the request
30
+ responder = self.class.responder.new
31
+ resp = responder.respond(str)
32
+
33
+ # format the response
34
+ writer = Avro::IPC::FramedWriter.new(StringIO.new(""))
35
+ writer.write_framed_message(resp)
36
+ send_data(writer.to_s)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ module AvroMachine
2
+ class Responder < Avro::IPC::Responder
3
+ def self.protocol
4
+ raise NotImplemented, "need to implement a protocol method"
5
+ end
6
+
7
+ def initialize
8
+ super(self.class.protocol)
9
+ end
10
+
11
+ def call(message, request)
12
+ send(message.name, request)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module AvroMachine
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avro_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Ching
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: avro
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.7.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.7.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: eventmachine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: EventMachine server for Avro
70
+ email:
71
+ - jching@avvo.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - avro_machine.gemspec
82
+ - example/Gemfile
83
+ - example/client.rb
84
+ - example/config.ru
85
+ - example/example.avdl
86
+ - example/example.avpr
87
+ - example/server
88
+ - lib/avro_machine.rb
89
+ - lib/avro_machine/connection.rb
90
+ - lib/avro_machine/responder.rb
91
+ - lib/avro_machine/version.rb
92
+ homepage: ''
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: EventMachine server for Avro
116
+ test_files: []
117
+ has_rdoc: