bridge_socket 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0f4f70b2e49119f6eca9a57179fe05d2fb9db1a3
4
+ data.tar.gz: 500d0b3a6a847ccacf72f70cf30e001b1d1f7ca8
5
+ SHA512:
6
+ metadata.gz: fedad05ec8d4ecca7d3b358620910d5ae0f2fa28bbc46aec6b33050062acfaa4357c31043c265504804f855b6584764db983ce7e0d825a7e3e01d964348ecf01
7
+ data.tar.gz: 24c1182df792898ff8f1a75dbe93eb265ec33086ccd19e0b76c5f5e52ee857a140218f1e1244b818287e00f4c13de42af20a2bfdaf55e4ad72e573792c7369e9
data/.gitignore ADDED
@@ -0,0 +1,37 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ /log/
13
+
14
+ ## Specific to RubyMotion:
15
+ .dat*
16
+ .repl_history
17
+ build/
18
+
19
+ ## Documentation cache and generated files:
20
+ /.yardoc/
21
+ /_yardoc/
22
+ /doc/
23
+ /rdoc/
24
+
25
+ ## Environment normalisation:
26
+ /.bundle/
27
+ /vendor/bundle
28
+ /lib/bundler/man/
29
+
30
+ # for a library or gem, you might want to ignore these files since the code is
31
+ # intended to run in multiple environments; otherwise, check them in:
32
+ # Gemfile.lock
33
+ # .ruby-version
34
+ # .ruby-gemset
35
+
36
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
37
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bridge_socket.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Toshiyuki Suzumura
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Toshiyuki Suzumura
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,58 @@
1
+ # BridgeSocket
2
+
3
+ シリアルデバイスやメインソケットをブリッジします.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'bridge_socket'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install bridge_socket
21
+
22
+
23
+ ## Usage
24
+
25
+ ブリッジを開始します.
26
+
27
+ # /dev/cu.usbmodem1421 ... シリアルデバイス
28
+ # /tmp/vmware.windows.com1 ... ドメインソケット
29
+ $ bridge_socket start /dev/cu.usbmodem1421 /tmp/vmware.windows.com1
30
+
31
+ ブリッジを停止します.
32
+
33
+ $ bridge_socket stop /dev/cu.usbmodem1421
34
+
35
+ その他.
36
+
37
+ # status表示
38
+ $ bridge_socket status /dev/cu.usbmodem1421
39
+
40
+ # リスタート
41
+ $ bridge_socket srestart /dev/cu.usbmodem1421
42
+
43
+ # ログ,pidディレクトリを指定.
44
+ # --logdir のみを指定した場合は --piddir もlogdirになります.
45
+ $ bridge_socket start /dev/cu.usbmodem1421 /tmp/vmware.windows.com1 --logdir=/var/log --piddir=/var/run
46
+
47
+ # socketが切れたとき 2sec毎にリトライ (デフォルト 5sec).
48
+ $ bridge_socket start /dev/cu.usbmodem1421 --retry=2
49
+
50
+
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/suzumura-ss/bridge_socket/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/bridge_socket ADDED
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/bridge_socket'
4
+ require 'digest/sha1'
5
+ require 'fileutils'
6
+ require 'daemon_spawn'
7
+
8
+
9
+ class BridgeSocketServer < DaemonSpawn::Base
10
+ def start(args)
11
+ puts "#{Time.now}: Starting for #{args.join(' ')}"
12
+ Kernel.at_exit{
13
+ puts "#{Time.now}: Stopped for #{args.join(' ')}"
14
+ }
15
+ @this = BridgeSocket.new
16
+ if args.find{|a| a=~/^(--retry=(.+))$/}
17
+ args.delete($1)
18
+ @this.retry_interval = $2.to_i
19
+ end
20
+ @this.start(args[0], args[1]){|msg, e|
21
+ puts "#{e} #{msg}"
22
+ }
23
+ end
24
+
25
+ def stop
26
+ @this.stop
27
+ end
28
+ end
29
+
30
+
31
+
32
+ begin
33
+ raise "command required." unless ARGV[0]
34
+ if ARGV[0]=='stop'
35
+ BridgeSocket.test([ARGV[1]])
36
+ else
37
+ BridgeSocket.test([ARGV[1], ARGV[2]])
38
+ end
39
+ rescue => e
40
+ unless e.is_a? Errno::ENOENT and ARGV.find{|v| v=~/^--retry=/}
41
+ puts e
42
+ puts <<__USAGE__
43
+
44
+ Usage:
45
+ Start bridge:
46
+ $ bridge_socket start <character device path> <socket path> [options]
47
+
48
+ Stop bridge:
49
+ $ bridge_socket stop <character device path> [options]
50
+
51
+ Restart bridge:
52
+ $ bridge_socket restart <character device path> [options]
53
+
54
+ Bridge satus:
55
+ $ bridge_socket status <character device path> [options]
56
+
57
+ ptions:
58
+ --logdir=<dir> : log-file dir.
59
+ --piddir=<dir> : pid-file dir.
60
+ --retry=<sec> : retry interval in sec.
61
+
62
+ __USAGE__
63
+ exit 1
64
+ end
65
+ end
66
+
67
+ key = Digest::SHA1.digest(ARGV[1]).unpack("H*")[0]
68
+ wdir = File.expand_path('..', File.dirname(__FILE__))
69
+ logdir = if ARGV.find{|a| a=~/^(--logdir=(.+))$/}
70
+ ARGV.delete($1)
71
+ $2
72
+ else
73
+ "#{wdir}/log"
74
+ end
75
+ piddir = if ARGV.find{|a| a=~/^(--piddir=(.+))$/}
76
+ ARGV.delete($1)
77
+ $2
78
+ else
79
+ logdir
80
+ end
81
+ FileUtils.mkdir_p(logdir)
82
+ FileUtils.mkdir_p(piddir)
83
+ BridgeSocketServer.spawn!(log_file:File.join(logdir, "bridge_socket-#{key}.log"),
84
+ pid_file:File.join(piddir, "bridge_socket-#{key}.pid"),
85
+ sync_log:true,
86
+ singleton:true,
87
+ working_dir:wdir)
@@ -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 'bridge_socket/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bridge_socket"
8
+ spec.version = BridgeSocket::VERSION
9
+ spec.authors = ["Toshiyuki Suzumura"]
10
+ spec.email = ["suz.labo@amail.plala.or.jp"]
11
+
12
+ spec.summary = %q{Bridge character device or socket.}
13
+ spec.description = %q{Bridge character device or socket.
14
+ ex: physical serial device and VMWareFusion's virtual serial port.}
15
+ spec.homepage = "https://github.com/suzumura-ss/bridge_socket"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "bin"
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.9"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_runtime_dependency "daemon-spawn", "~> 0.4"
26
+ end
@@ -0,0 +1,61 @@
1
+ require_relative 'bridge_socket/version'
2
+ require 'socket'
3
+
4
+
5
+ class BridgeSocket
6
+ private
7
+ def do_pipe(fr, fw)
8
+ Thread.start{
9
+ begin
10
+ while v = fr.read(1024)
11
+ fw.write(v)
12
+ end
13
+ rescue
14
+ end
15
+ }
16
+ end
17
+
18
+ public
19
+ def self.test(args)
20
+ args.each{|arg|
21
+ unless arg and (File.stat(arg).chardev? or File.stat(arg).socket?)
22
+ raise RuntimeError, "character device or socket is required - #{arg}"
23
+ end
24
+ }
25
+ end
26
+
27
+ attr_accessor :retry_interval
28
+
29
+ def initialize
30
+ @retry_interval = 5
31
+ end
32
+
33
+ def start(file1, file2, &block)
34
+ files = [file1, file2]
35
+ @retry = true
36
+ while @retry
37
+ begin
38
+ @socks = files.map{|s| File.open(s, 'w+') rescue UNIXSocket.open(s) }
39
+ rescue => e
40
+ return unless @retry
41
+ yield("retry after #{@retry_interval} sec.", e) if block_given?
42
+ sleep @retry_interval
43
+ retry
44
+ end
45
+ yield("Starting #{files.join(" <=> ")}.", nil) if block_given?
46
+ @thread = do_pipe(*@socks)
47
+ do_pipe(*@socks.reverse)
48
+ @thread.join
49
+ @socks.each{|s| s.close rescue nil }
50
+ yield("disconnected.", nil) if block_given?
51
+ end
52
+ end
53
+
54
+ def stop
55
+ @retry = false
56
+ @socks.each{|s| s.close rescue nil }
57
+ @thread.join rescue nil
58
+ @socks = []
59
+ @thread = nil
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ class BridgeSocket
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bridge_socket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Toshiyuki Suzumura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-20 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: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: daemon-spawn
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ description: |-
56
+ Bridge character device or socket.
57
+ ex: physical serial device and VMWareFusion's virtual serial port.
58
+ email:
59
+ - suz.labo@amail.plala.or.jp
60
+ executables:
61
+ - bridge_socket
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - Gemfile
67
+ - LICENSE
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/bridge_socket
72
+ - bridge_socket.gemspec
73
+ - lib/bridge_socket.rb
74
+ - lib/bridge_socket/version.rb
75
+ homepage: https://github.com/suzumura-ss/bridge_socket
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Bridge character device or socket.
99
+ test_files: []