hokaido 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13b934e4a7e01d32d6366179ef660e484fe5ddeb
4
- data.tar.gz: a7b298c2e37776c1ee1a58a50ff705d33851b6fe
3
+ metadata.gz: ba9d4544676c2a17748b534f434b318b223331c6
4
+ data.tar.gz: a1602cea4d9d03375373f6eacab8f82bb96d0cb3
5
5
  SHA512:
6
- metadata.gz: 5e2cd0b75c3665e2a3e430a4c6f8a80a90285dddf79a93e5e7a91caf6460f8cc41e2826a286915569f5f839b8ca8e4ccac7eb6761969fafc3037335a0ec69d2f
7
- data.tar.gz: 64a50a2095150a07fcc29e025fe06a8d2d19931e673ce323e13a55e081d832916cce5a7facdde73bdeae2ba503aa418e13ac6647842af8e67b608616a9018d27
6
+ metadata.gz: 713597c50d91a3ec475322f6fadfc27a166f0f1cc1841ad6e00c52492585789bdc57ed33f1d48bfc20a6b0b72e3bc277da054ff51633e24c558de483bc6c051f
7
+ data.tar.gz: 9baffd608a5d42bc64b2a546f22f986d437bcf9903324f01427811c555b688da329e03ba411bef6aa7e6002f9171ad113359ef10429b5db8ddaad0382bb0b73e
data/Rakefile CHANGED
@@ -1,2 +1 @@
1
- require "bundler/gem_tasks"
2
-
1
+ require 'bundler/gem_tasks'
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'hokaido/cli'
3
+ require 'hokaido'
4
4
 
5
5
  Hokaido::CLI.start
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'hokaido/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "hokaido"
7
+ spec.name = 'hokaido'
8
8
  spec.version = Hokaido::VERSION
9
9
  spec.authors = %w(ursm hibariya)
10
10
  spec.email = %w(ursm@ursm.jp celluloid.key@gmail.com)
@@ -17,8 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ['lib']
19
19
 
20
- spec.add_runtime_dependency 'thor'
20
+ spec.add_runtime_dependency 'celluloid'
21
21
  spec.add_runtime_dependency 'ruby-terminfo'
22
+ spec.add_runtime_dependency 'thor'
22
23
 
23
24
  spec.add_development_dependency 'bundler', '~> 1.7'
24
25
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -1,4 +1,8 @@
1
1
  require 'hokaido/version'
2
2
 
3
3
  module Hokaido
4
+ autoload :Broadcast, 'hokaido/broadcast'
5
+ autoload :CLI, 'hokaido/cli'
6
+ autoload :Server, 'hokaido/server'
7
+ autoload :Watcher, 'hokaido/watcher'
4
8
  end
@@ -0,0 +1,80 @@
1
+ require 'celluloid'
2
+ require 'io/console'
3
+ require 'pty'
4
+ require 'socket'
5
+ require 'terminfo'
6
+
7
+ module Hokaido
8
+ class Broadcast
9
+ class Connection
10
+ include Celluloid
11
+
12
+ def initialize(host, port)
13
+ @socket = TCPSocket.new(host, port)
14
+
15
+ @socket.puts 'broadcast'
16
+ end
17
+
18
+ def send(data)
19
+ @socket.write data
20
+ end
21
+ end
22
+
23
+ class OutputHandler
24
+ include Celluloid
25
+
26
+ def initialize(ptyout, connection)
27
+ @ptyout, @connection = ptyout, connection
28
+
29
+ async.run
30
+ end
31
+
32
+ def run
33
+ while chunk = @ptyout.readpartial(4096)
34
+ $stdout.write chunk
35
+ @connection.async.send chunk
36
+ end
37
+
38
+ terminate
39
+ end
40
+ end
41
+
42
+ class InputHandler
43
+ include Celluloid
44
+
45
+ def initialize(ptyin)
46
+ @ptyin = ptyin
47
+
48
+ async.run
49
+ end
50
+
51
+ def run
52
+ while char = $stdin.getch
53
+ @ptyin.putc char
54
+ end
55
+
56
+ terminate
57
+ end
58
+ end
59
+
60
+ class Command
61
+ include Celluloid
62
+
63
+ def initialize(command, host, port)
64
+ ptyout, ptyin, pid = PTY.getpty(command)
65
+ connection = Connection.new_link(host, port)
66
+
67
+ OutputHandler.new_link ptyout, connection
68
+ InputHandler.new_link ptyin
69
+
70
+ async.wait_for_exit pid
71
+ end
72
+
73
+ def wait_for_exit(pid)
74
+ Process.waitpid pid
75
+
76
+ terminate
77
+ end
78
+ end
79
+ end
80
+ end
@@ -1,9 +1,7 @@
1
- require 'io/console'
2
- require 'pty'
1
+ require 'celluloid/autostart'
3
2
  require 'socket'
4
3
  require 'terminfo'
5
4
  require 'thor'
6
- require 'thread'
7
5
 
8
6
  module Hokaido
9
7
  class CLI < Thor
@@ -12,79 +10,22 @@ module Hokaido
12
10
 
13
11
  desc :broadcast, 'Broadcast a session'
14
12
  def broadcast(command = ENV['SHELL'])
15
- pty_out, pty_in, pid = *PTY.getpty(command)
16
- nonbloq = Queue.new
17
-
18
- trap :SIGWINCH do
19
- TermInfo.tiocswinsz pty_in, *TermInfo.screen_size
20
- end
21
-
22
- Thread.abort_on_exception = true
23
-
24
- Thread.start do
25
- server = TCPSocket.open(*options.values_at(:host, :port))
26
- server.puts 'write'
27
-
28
- while chunk = nonbloq.deq
29
- server.write chunk
30
- end
31
- end
32
-
33
- Thread.start do
34
- while chunk = pty_out.readpartial(4096)
35
- $stdout.write chunk
36
- nonbloq.enq chunk
37
- end
38
- end
39
-
40
- Thread.start do
41
- TermInfo.tiocswinsz pty_in, *TermInfo.screen_size
42
-
43
- while char = $stdin.getch
44
- pty_in.putc char
45
- end
46
- end
47
-
48
- Process.waitpid pid
13
+ Hokaido::Broadcast::Command.run command, *options.values_at(:host, :port)
49
14
  end
50
15
 
51
16
  desc :server, 'Start server'
52
17
  def server
53
- queue = Queue.new
54
- server = TCPServer.open(*options.values_at(:host, :port))
55
-
56
- loop do
57
- Thread.start server.accept do |client|
58
- case client.gets.chomp
59
- when 'write'
60
- client.puts ':)'
61
-
62
- while chunk = client.readpartial(4096)
63
- queue.enq chunk
64
- end
65
- when 'read'
66
- client.puts '=)'
67
-
68
- while chunk = queue.deq
69
- client.write chunk
70
- end
71
- else
72
- client.puts ':('
73
- end
74
-
75
- client.close
76
- end
77
- end
18
+ server = Server.run(*options.values_at(:host, :port))
78
19
  rescue Interrupt
79
- server.close
80
-
81
20
  exit
21
+ ensure
22
+ server.terminate if server
82
23
  end
83
24
 
84
25
  desc :watch, 'Watch a session'
85
26
  def watch
86
27
  server = TCPSocket.open(*options.values_at(:host, :port))
87
- server.puts 'read'
28
+ server.puts 'watch'
88
29
 
89
30
  while chunk = server.readpartial(4096)
90
31
  $stdout.write chunk
@@ -0,0 +1,69 @@
1
+ require 'celluloid'
2
+ require 'celluloid/notifications'
3
+ require 'socket'
4
+
5
+ module Hokaido
6
+ class ConnectionHandler
7
+ include Celluloid
8
+ include Celluloid::Notifications
9
+
10
+ def initialize(connection)
11
+ @connection = connection
12
+
13
+ async.run
14
+ end
15
+
16
+ def run
17
+ _, port, host = @connection.peeraddr
18
+
19
+ puts "#{host}:#{port} connected"
20
+
21
+ case @connection.gets.chomp
22
+ when 'broadcast'
23
+ @connection.puts ':)'
24
+
25
+ loop do
26
+ publish 'broadcast', @connection.readpartial(4096)
27
+ end
28
+ when 'watch'
29
+ @connection.puts '=)'
30
+
31
+ watcher = Watcher.new_link(@connection)
32
+
33
+ loop do
34
+ @connection.readpartial(4096) # XXX wait for connection closed
35
+ end
36
+ else
37
+ @connection.puts ':('
38
+ end
39
+ rescue Errno::ECONNRESET
40
+ # do nothing, connetion reset by peer
41
+ ensure
42
+ puts "#{host}:#{port} disconnected"
43
+
44
+ @connection.close
45
+ end
46
+ end
47
+
48
+ class Server
49
+ include Celluloid
50
+
51
+ finalizer :shutdown
52
+
53
+ def initialize(host, port)
54
+ @server = TCPServer.new(host, port)
55
+
56
+ async.run
57
+ end
58
+
59
+ def shutdown
60
+ @server.close if @server
61
+ end
62
+
63
+ def run
64
+ loop do
65
+ ConnectionHandler.new_link @server.accept
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,3 +1,3 @@
1
1
  module Hokaido
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -0,0 +1,19 @@
1
+ require 'celluloid'
2
+ require 'celluloid/notifications'
3
+
4
+ module Hokaido
5
+ class Watcher
6
+ include Celluloid
7
+ include Celluloid::Notifications
8
+
9
+ def initialize(socket)
10
+ @socket = socket
11
+
12
+ subscribe 'broadcast', :received
13
+ end
14
+
15
+ def received(topic, chunk)
16
+ @socket.write chunk
17
+ end
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hokaido
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - ursm
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-10-22 00:00:00.000000000 Z
12
+ date: 2014-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: thor
15
+ name: celluloid
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - ">="
@@ -39,6 +39,20 @@ dependencies:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: thor
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
42
56
  - !ruby/object:Gem::Dependency
43
57
  name: bundler
44
58
  requirement: !ruby/object:Gem::Requirement
@@ -84,8 +98,11 @@ files:
84
98
  - bin/hokaido
85
99
  - hokaido.gemspec
86
100
  - lib/hokaido.rb
101
+ - lib/hokaido/broadcast.rb
87
102
  - lib/hokaido/cli.rb
103
+ - lib/hokaido/server.rb
88
104
  - lib/hokaido/version.rb
105
+ - lib/hokaido/watcher.rb
89
106
  homepage: https://github.com/ursmhbry/hokaido
90
107
  licenses:
91
108
  - MIT