command_server 0.0.1

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
+ SHA1:
3
+ metadata.gz: 07bc31df9b1d027e1cc338301f9df3cfed7a57ea
4
+ data.tar.gz: b5461a7b88697823a48c8dbcab46fcb503bc4dd1
5
+ SHA512:
6
+ metadata.gz: dea05dae8a6443048fda762bbc078ed8a81493f1e0688329d69c1ae7145cc6ec062a3db63af1f85b6542822407bc834810bf72385ce43aa8ea58909836a2d1f7
7
+ data.tar.gz: 674e57e2ecac6fbbba44bfad518121a05ab42147a0ae70f9f1519e4dbd6a234fd8d8a300d46e5cc083c254dd96b1c7d7480c15654f92219a33ee41d424e510e3
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cmdserver.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 clicube
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.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # CommandServer #
2
+
3
+ Library to put commands into running ruby process via socket.
4
+
5
+ You can check status of the process and execute methods.
6
+
7
+
8
+ ## Install ##
9
+
10
+ gem install command_server
11
+
12
+ ## Usage ##
13
+
14
+ require 'command_server'
15
+
16
+ class SomeWorkClass
17
+
18
+ def initialize
19
+ @cmdserver = CommandServer.new
20
+ @cmdserver.enable_builtin_command("eval");
21
+ @cmdserver.enable_builtin_command("help");
22
+ @cmdserver.register_command("show_threads", proc{ puts Thread.list })
23
+ @cmdserver.register_command("show_pid", proc{ puts $$ })
24
+ @cmdserver.start("/tmp/some_work_socket")
25
+ end
26
+
27
+ end
28
+
29
+ SomeWorkClass.new
30
+ sleep
31
+
32
+ And then, put a command from 'cmdshell'
33
+
34
+ $ cmdshell /tmp/some_work_socket show_threads
35
+ #<Thread:0x007f88728cb7c8>
36
+ #<Thread:0x007f88729e1c48>
37
+ #<Thread:0x007f88729e10b8>
38
+
39
+ Or put commands interactively
40
+
41
+ $ cmdshell /tmp/some_work_socket
42
+ (/tmp/some_work_socket)> help
43
+ Available commands:
44
+ eval
45
+ help
46
+ show_pid
47
+ show_threads
48
+ (/tmp/some_work_socket)> show_pid
49
+ 2971
50
+ (/tmp/some_work_socket)> show_threads
51
+ #<Thread:0x007f88728cb7c8>
52
+ #<Thread:0x007f88729e1c48>
53
+ #<Thread:0x007f88729e10b8>
54
+ (/tmp/some_work_socket)> eval puts self
55
+ main
56
+
57
+
58
+ ## License ##
59
+
60
+ This software is released under the MIT License, see LICENSE.txt.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/cmdshell ADDED
@@ -0,0 +1,3 @@
1
+ #!env ruby
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/command_server/command_shell')
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'command_server/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "command_server"
8
+ spec.version = CommandServer::VERSION
9
+ spec.authors = ["clicube"]
10
+ spec.email = ["clicube@gmail.com"]
11
+ spec.summary = %q{Library to put command into running ruby process via socket.}
12
+ spec.description = %q{Library to put command into running ruby process via socket. You can check status of the process and execute methods.}
13
+ spec.homepage = "https://github.com/clicube/command_server"
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_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,2 @@
1
+ require "command_server/version"
2
+ require "command_server/command_server"
@@ -0,0 +1,112 @@
1
+ require 'socket'
2
+
3
+ class CommandServer
4
+
5
+ def initialize
6
+ @cmds = {}
7
+ end
8
+
9
+ def start addr
10
+ @addr = addr
11
+ @server = UNIXServer.new(@addr)
12
+ $stderr.puts "[CommandServer] Server start: #{@addr}"
13
+
14
+ at_exit do
15
+ stop
16
+ end
17
+
18
+ @accept_thread = Thread.new do
19
+ @accept_thread.abort_on_exception = true
20
+ loop do
21
+ accept_and_process
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ def stop
28
+ File.unlink @addr
29
+ @accept_thread.exit
30
+ end
31
+
32
+ def register_command name, proc_obj, description=""
33
+ @cmds[name] = {proc: proc_obj, desc: description}
34
+ end
35
+
36
+ def enable_builtin_command name
37
+ proc_name = "builtin_command_#{name}"
38
+ if self.respond_to?(proc_name, true)
39
+ register_command name, method(proc_name.to_sym)
40
+ else
41
+ raise NameError.new("Undefined builtin command: #{name}")
42
+ end
43
+ end
44
+
45
+ private
46
+ def accept_and_process
47
+
48
+ sock = @server.accept
49
+ cmdline = sock.gets.chomp.strip
50
+ parts = cmdline.split(" ", 2)
51
+ cmd = parts[0]
52
+ arg = parts[1]
53
+ $stderr.puts "[CommandServer] Command received: #{cmdline}"
54
+ if @cmds.include?(cmd)
55
+ cmd_method = @cmds[cmd][:proc]
56
+ cmd_t = Thread.new do
57
+ cmd_t.abort_on_exception = true
58
+ stdout = $stdout
59
+ begin
60
+ $stdout = sock
61
+ cmd_method.call(arg)
62
+ rescue StandardError, ScriptError => e
63
+ msg = "#{e.backtrace.shift}: #{e.message} (#{e.class.to_s})\r\n"
64
+ while(str = e.backtrace.shift)
65
+ msg << "\t#{str}\r\n"
66
+ end
67
+ $stderr.puts msg
68
+ sock.puts msg
69
+ ensure
70
+ $stdout = stdout
71
+ sock.close
72
+ end
73
+ end
74
+ else
75
+ $stderr.puts "[CommandServer] Invalid command: #{cmd}"
76
+ sock.puts "Invalid command: #{cmd}"
77
+ sock.close
78
+ end
79
+ end
80
+
81
+ def builtin_command_eval(arg)
82
+ eval(arg, Object::TOPLEVEL_BINDING)
83
+ end
84
+
85
+ def builtin_command_help(arg)
86
+ puts "Available commands:"
87
+ @cmds.sort.each do |key, item|
88
+ puts " #{key}"
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ if $0 == __FILE__
95
+
96
+ class SomeWorkClass
97
+
98
+ def initialize
99
+ @cmdserver = CommandServer.new
100
+ @cmdserver.enable_builtin_command("eval");
101
+ @cmdserver.enable_builtin_command("help");
102
+ @cmdserver.register_command("show_threads", proc{ puts Thread.list })
103
+ @cmdserver.register_command("show_pid", proc{ puts $$ })
104
+ @cmdserver.start("/tmp/some_work_socket")
105
+ end
106
+
107
+ end
108
+
109
+ SomeWorkClass.new
110
+ sleep
111
+
112
+ end
@@ -0,0 +1,47 @@
1
+ require 'socket'
2
+
3
+ addr = ARGV[0]
4
+ cmd = ARGV[1] || ""
5
+
6
+ def proc_command(addr, cmd)
7
+
8
+ UNIXSocket.open(addr) do |socket|
9
+ socket.puts cmd
10
+ while(line = socket.gets) do
11
+ $stdout.puts line
12
+ end
13
+ end
14
+
15
+ rescue Errno::ENOENT
16
+
17
+ raise IOError.new("[CommandShell] Error: Unavailable socket: #{addr}")
18
+
19
+ end
20
+
21
+
22
+ if !addr || addr.length == 0
23
+ $stdout.puts "Usage: #{$0} <path/to/socket> [command]"
24
+ raise ArgumentError.new("Socket path is required")
25
+ end
26
+
27
+ if cmd.length > 0
28
+ cmdline = ARGV[1..ARGV.length].join(" ")
29
+ proc_command(addr, cmdline)
30
+ else
31
+ while(true) do
32
+ print "(#{addr})> "
33
+ cmd = $stdin.gets
34
+ if cmd == nil
35
+ break
36
+ end
37
+ cmd = cmd.chomp.strip
38
+ if(cmd.length > 0)
39
+ begin
40
+ proc_command(addr, cmd)
41
+ rescue IOError => e
42
+ $stdout.puts e.message
43
+ end
44
+ end
45
+ end
46
+ $stdout.puts
47
+ end
@@ -0,0 +1,3 @@
1
+ class CommandServer
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - clicube
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-21 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ description: Library to put command into running ruby process via socket. You can
42
+ check status of the process and execute methods.
43
+ email:
44
+ - clicube@gmail.com
45
+ executables:
46
+ - cmdshell
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/cmdshell
56
+ - command_server.gemspec
57
+ - lib/command_server.rb
58
+ - lib/command_server/command_server.rb
59
+ - lib/command_server/command_shell.rb
60
+ - lib/command_server/version.rb
61
+ homepage: https://github.com/clicube/command_server
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Library to put command into running ruby process via socket.
85
+ test_files: []