packserv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0cffb8a18775bc97caa1f140d8d235067a251ee93c4de27d26e80cc285b075da
4
+ data.tar.gz: b435f4d13ae3b241be06b12aef669bd027fb4c38ab86393327aee54f182a71ee
5
+ SHA512:
6
+ metadata.gz: 0d0844a8d74cc9777a1138226ee820fe3cddfe9a29ad0f9d4ed378d02732f16a81c8a6edbfb68821921e0e3419728799ee8810278a9e79d4ff7985ba56a53f9d
7
+ data.tar.gz: 673337d9f7a334faf5e2e953699d085586e995074189f0e130f1e76b9a6d55efe7d34407b92c52d8794b75eab188730112ab5bab90e924c08c8bed5816054768
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Stone Tickle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Packserv
2
+ [![Build Status](https://travis-ci.org/annacrombie/packserv.svg?branch=master)](https://travis-ci.org/annacrombie/packserv)
3
+ [![Coverage Status](https://coveralls.io/repos/github/annacrombie/packserv/badge.svg?branch=master)](https://coveralls.io/github/annacrombie/packserv?branch=master)
4
+
5
+ A simple server/client
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'packserv'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install packserv
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/packserv.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'packserv'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/packserv.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'socket'
2
+ require 'msgpack'
3
+
4
+ require 'packserv/client'
5
+ require 'packserv/default_protocol'
6
+ require 'packserv/exceptions'
7
+ require 'packserv/io_packer'
8
+ require 'packserv/io_unpacker'
9
+ require 'packserv/server'
10
+ require 'packserv/version'
11
+
12
+ module PackServ
13
+ class << self
14
+ def connect(host, port)
15
+ Client.new.connect(host, port)
16
+ end
17
+
18
+ def serve(port)
19
+ Server.new.serve(port)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,62 @@
1
+ module PackServ
2
+ class Client
3
+ attr_accessor :handler
4
+
5
+ def initialize(proto = nil)
6
+ @proto = proto || DefaultProtocol
7
+
8
+ @handler = ->(_) {}
9
+ @event_queue = Queue.new
10
+ @response_queue = Queue.new
11
+ @outgoing_queue = Queue.new
12
+
13
+ @threads = ThreadGroup.new
14
+ end
15
+
16
+ def connect(host, port)
17
+ @server = TCPSocket.new(host, port)
18
+
19
+ @threads.add(Thread.new { _connect(@server) })
20
+ @threads.add(Thread.new { dispatch_events })
21
+
22
+ self
23
+ end
24
+
25
+ def disconnect
26
+ @threads.list.map(&:exit)
27
+ @server.close
28
+ end
29
+
30
+ def send(obj)
31
+ @outgoing_queue.push(obj)
32
+
33
+ @response_queue.pop
34
+ end
35
+
36
+ private
37
+
38
+ def dispatch_events
39
+ loop { handler.(@event_queue.pop) }
40
+ end
41
+
42
+ def setup_mailbox(server)
43
+ packer = IOPacker.new(server, @proto)
44
+ @threads.add(Thread.new do
45
+ loop { packer.pack(@proto.create(@outgoing_queue.pop)) }
46
+ end)
47
+ end
48
+
49
+ def _connect(server)
50
+ setup_mailbox(server)
51
+
52
+ IOUnpacker.each_from(server, @proto) do |msg|
53
+ case msg['type']
54
+ when 'event'
55
+ @event_queue
56
+ else
57
+ @response_queue
58
+ end.push(@proto.extract(msg))
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,21 @@
1
+ module PackServ
2
+ module DefaultProtocol
3
+ HEADER_LENGTH = 8
4
+ HEADER_FORMAT = '%08x'.freeze
5
+
6
+ module_function
7
+
8
+ def create(obj, type = '', id = nil)
9
+ {
10
+ 'ver' => PackServ::VERSION,
11
+ 'id' => id || obj.object_id,
12
+ 'type' => type,
13
+ 'payload' => obj
14
+ }
15
+ end
16
+
17
+ def extract(msg)
18
+ msg['payload']
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module PackServ
2
+ module Exceptions
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ module PackServ
2
+ class IOPacker
3
+ def initialize(io, proto)
4
+ @proto = proto
5
+ @io = io
6
+ end
7
+
8
+ def pack(obj)
9
+ write(MessagePack.pack(obj))
10
+ end
11
+
12
+ private
13
+
14
+ def frame_length(packed)
15
+ sprintf(@proto::HEADER_FORMAT, packed.bytesize)
16
+ end
17
+
18
+ def write(packed)
19
+ @io.write(frame_length(packed) + packed)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ module PackServ
2
+ class IOUnpacker
3
+ class << self
4
+ def each_from(io, proto)
5
+ iou = IOUnpacker.new(io, proto)
6
+
7
+ loop do
8
+ frame = iou.get_frame
9
+
10
+ break if frame.empty?
11
+
12
+ yield(MessagePack.unpack(frame))
13
+ end
14
+ end
15
+ end
16
+
17
+ def initialize(io, proto)
18
+ @proto = proto
19
+ @io = io
20
+ end
21
+
22
+ def get_frame
23
+ @io.read(get_frame_length)
24
+ end
25
+
26
+ private
27
+
28
+ def get_frame_length
29
+ @io.read(@proto::HEADER_LENGTH).then { |g| g.nil? ? '' : g }.to_i(16)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,69 @@
1
+ module PackServ
2
+ class Server
3
+ attr_accessor :handler
4
+
5
+ def initialize(proto = nil)
6
+ @proto = proto || DefaultProtocol
7
+
8
+ @handler = ->(_) {}
9
+ @packers = {}
10
+ @event_queue = Queue.new
11
+ @threads = ThreadGroup.new
12
+ end
13
+
14
+ def event(data)
15
+ @event_queue.push(data)
16
+ end
17
+
18
+ def serve(port)
19
+ server = TCPServer.new(port)
20
+
21
+ @threads.add(Thread.new { _serve(server) })
22
+ @threads.add(Thread.new { deliver_events })
23
+
24
+ self
25
+ end
26
+
27
+ def stop
28
+ @threads.list.map(&:exit)
29
+ end
30
+
31
+ private
32
+
33
+ def setup_mailbox(client)
34
+ packer = IOPacker.new(client, @proto)
35
+ outgoing = Queue.new
36
+
37
+ @threads.add(Thread.new do
38
+ loop { packer.pack(@proto.create(outgoing.pop)) }
39
+ end)
40
+
41
+ @packers[client.object_id] = packer
42
+
43
+ outgoing
44
+ end
45
+
46
+ def handle(client)
47
+ outgoing = setup_mailbox(client)
48
+
49
+ IOUnpacker.each_from(client, @proto) do |msg|
50
+ response = handler.(@proto.extract(msg))
51
+
52
+ outgoing.push(response)
53
+ end
54
+ end
55
+
56
+ def deliver_events
57
+ loop do
58
+ data = @event_queue.pop
59
+ @packers.each { |_, k| k.pack(@proto.create(data, 'event')) }
60
+ end
61
+ end
62
+
63
+ def _serve(server)
64
+ loop do
65
+ @threads.add(Thread.new(server.accept) { |client| handle(client) })
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module PackServ
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/packserv.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'packserv/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'packserv'
7
+ spec.version = PackServ::VERSION
8
+ spec.authors = ['Stone Tickle']
9
+ spec.email = ['lattis@mochiro.moe']
10
+
11
+ spec.summary = 'A simple TCP server/client'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
15
+ `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ end
19
+
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 2'
25
+ spec.add_development_dependency 'rake', '~> 12'
26
+ spec.add_development_dependency 'rspec', '~> 3'
27
+ spec.add_development_dependency 'simplecov'
28
+ spec.add_development_dependency 'coveralls'
29
+
30
+
31
+ spec.add_runtime_dependency 'msgpack', '~> 1.2'
32
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: packserv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stone Tickle
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: msgpack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ description:
98
+ email:
99
+ - lattis@mochiro.moe
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/packserv.rb
114
+ - lib/packserv/client.rb
115
+ - lib/packserv/default_protocol.rb
116
+ - lib/packserv/exceptions.rb
117
+ - lib/packserv/io_packer.rb
118
+ - lib/packserv/io_unpacker.rb
119
+ - lib/packserv/server.rb
120
+ - lib/packserv/version.rb
121
+ - packserv.gemspec
122
+ homepage:
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 3.0.1
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A simple TCP server/client
145
+ test_files: []