cmdserver 0.9.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: 3c4084d52fe2ef4f97a79630ee88c52ca99c2b1b
4
+ data.tar.gz: d389b6d35fc6c9670f395c4001a41167276a3081
5
+ SHA512:
6
+ metadata.gz: 1cdf8084c4412313c54a5d2c13b49b06ec1ce3b824d9cb30edbeded8e79f07755e7e5e2a7352829235b9334e0355c6f530814098cc9cc287e0b34d4bf216d35c
7
+ data.tar.gz: 117b41f1ca968f3d8bd76725ea2f0589d3862045a23f238bdc6634c8cdbca7cdef5561654c2f8eb7c7efa8c4fcf577798cacda2b983ab1d3a620df75aa4b3e73
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.3
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 ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ernest Deák
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/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Cmdserver
2
+
3
+ Cmdserver gives you the ability to design a very simple command server.
4
+ Simply create a .rb module under `~/.cmdserver/modules/` and override the module `Cmdserver::CmdProtocol`.
5
+ Fire up your server and you are done! It is ready to respond to the commands you have defined.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cmdserver'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cmdserver
22
+
23
+ ## Usage
24
+
25
+ By default, Cmdserver looks into `~/.cmdserver/modules` for any .rb files present. It then `require`s them into the program.
26
+ In these .rb files, you override the module `Cmdserver::CmdProtocol` as demonstrated bellow
27
+
28
+ ```ruby
29
+ module Cmdserver::CmdProtocol
30
+ def self.extend_protocol
31
+ @protocol_hash["CustomCommand"] = -> client_socket, arguments { client_socket.puts "You sent: #{arguments}" }
32
+ end
33
+
34
+ def self.default_action(client_socket, arguments)
35
+ client_socket.puts "#{arguments} - Command not recognized"
36
+ end
37
+ end
38
+ ```
39
+
40
+ Here we defined `CustomCommand` to be the string that will trigger the execution of the function we presented.
41
+ The first argument passed to the function is the client socket, the second argument `arguments` is a __string__ of whatever
42
+ is left, when we removed `CustomCommand` from the string we initially recieved from the client. This might change in the future.
43
+ For now, argument parsing is left up to the individual functions.
44
+
45
+ Also note that overriding the default behaviour can be done only once. The last loaded module that redefines `self.default_action` is what is going to happen, when the command is not recognized. By default, it echoes back whatever it recieves.
46
+
47
+ The `@protocol_hash` can be destroied in any module. The hash gets copied into the core on a per-module basis. Note that this can introduce
48
+ conflicts when many modules define the same keys for commands.
49
+
50
+
51
+ Then, in your program
52
+ ```ruby
53
+ require "cmdserver"
54
+ server = Cmdserver::TCPCommandServer.new(1234)
55
+ server.start()
56
+ ```
57
+
58
+ That starts the main loop which will then start accepting connections.
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ 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).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/majorendian/cmdserver.
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
74
+
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 "cmdserver"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/cmdserver.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cmdserver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cmdserver"
8
+ spec.version = Cmdserver::VERSION
9
+ spec.authors = ["Ernest Deák"]
10
+ spec.email = ["gordon.zar@gmail.com"]
11
+
12
+ spec.summary = %q{Simple, module based command execution server over tcp.}
13
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/majorendian/Modular-Tcp-Command-Server"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "release"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ end
data/lib/cmdserver.rb ADDED
@@ -0,0 +1,136 @@
1
+ require "socket"
2
+ require "pathname"
3
+ require "cmdserver/version"
4
+
5
+ module Cmdserver
6
+
7
+ module CmdProtocol
8
+ # Protocol stuff goes here.
9
+ # Should be loaded from ~/.cmdserver/modules/*.rb
10
+ @protocol_hash = {}
11
+ def self.extend_protocol()
12
+ @protocol_hash = {
13
+ "dummy" => -> cs, args { cs.puts "Dummy reply"}
14
+ }
15
+ end
16
+
17
+ def self.default_action(cs, args)
18
+ cs.puts args
19
+ end
20
+
21
+ module_function
22
+ def getProtocolHash()
23
+ self.extend_protocol()
24
+ return @protocol_hash
25
+ end
26
+
27
+ # Default behaviour when querry was not found
28
+ module_function
29
+ def default(cs, args)
30
+ # NOTE: args is a String
31
+ self.default_action(cs, args)
32
+ end
33
+ end
34
+
35
+ class Settings
36
+
37
+ def initialize(config_dir="~/.cmdserver/")
38
+ @workdir = Pathname.new(File.expand_path(config_dir))
39
+ @config_rc = @workdir + "configrc"
40
+ @module_dir = @workdir + "modules"
41
+ if not @workdir.exist?
42
+ Dir.mkdir @workdir
43
+ if not @config_rc.exist?
44
+ File.new @config_rc, "w"
45
+ end
46
+ end
47
+ if not @module_dir.exist?
48
+ Dir.mkdir @module_dir
49
+ end
50
+ # Load modules contained within the module
51
+ # directories
52
+ load_modules()
53
+ end
54
+
55
+ def load_modules
56
+ Dir.glob("#{@module_dir}/*.rb").each do |mod|
57
+ CmdProtocol.extend_protocol()
58
+ puts "Loading module: #{mod}"
59
+ require mod
60
+ end
61
+ end
62
+ end
63
+
64
+ class TCPCommandServer
65
+
66
+ attr_accessor :socket
67
+
68
+ def initialize(port, settings=nil, hash={}, debug=false)
69
+ if settings.nil?
70
+ @settings = Settings.new()
71
+ end
72
+ @socket = TCPServer.new(port)
73
+ @cmd_hash = hash # hash of commands
74
+ @debug = debug
75
+ load_cmd_proto()
76
+ end
77
+
78
+ def load_cmd_proto()
79
+ phash = CmdProtocol.getProtocolHash()
80
+ phash.each_key do |key|
81
+ @cmd_hash[key] = phash[key]
82
+ end
83
+ end
84
+
85
+ def registerCallback(string, aproc)
86
+ @cmd_hash[string] = aproc
87
+ end
88
+
89
+ def start()
90
+ loop do
91
+ client = @socket.accept
92
+ Thread.new{ process_client(client) }
93
+ end
94
+ end
95
+
96
+ def process_client(client)
97
+ #NOTE: This should remain as an isolated thread process
98
+ loop do
99
+ request = client.gets
100
+ if not request.nil?
101
+ request.chomp!
102
+ puts "Got '#{request}'" if @debug
103
+ real_key = nil
104
+ @cmd_hash.each_key do |key|
105
+ if request.include? key
106
+ real_key = key
107
+ end
108
+ end
109
+ puts "real_key:#{real_key}" if @debug
110
+ if not real_key.nil?
111
+ begin
112
+ request.sub! real_key, ""
113
+ request.chomp!
114
+ if @cmd_hash.key? real_key
115
+ puts "Request after processing: #{request}" if @debug
116
+ @cmd_hash[real_key].call(client, request)
117
+ end
118
+ rescue Exception
119
+ puts "ERROR: #{$!}"
120
+ raise $!
121
+ end
122
+ else
123
+ Cmdserver::CmdProtocol.default(client, request)
124
+ end
125
+ else
126
+ client.close()
127
+ break
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+
135
+ #server = TCPCommandServer.new(2121, {}, false)
136
+ #server.start()
@@ -0,0 +1,3 @@
1
+ module Cmdserver
2
+ VERSION = "0.9.1"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmdserver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Ernest Deák
8
+ autorequire:
9
+ bindir: release
10
+ cert_chain: []
11
+ date: 2015-10-27 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - gordon.zar@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - cmdserver.gemspec
72
+ - lib/cmdserver.rb
73
+ - lib/cmdserver/version.rb
74
+ homepage: https://github.com/majorendian/Modular-Tcp-Command-Server
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
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.5.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Simple, module based command execution server over tcp.
99
+ test_files: []