mono_talk 0.0.1 → 0.0.2

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: 22053d020852e17c7014583a5f5da96205cdc1a8
4
- data.tar.gz: b7fd7ba08ab9658196c6dd66ad0397b5fb31c8aa
3
+ metadata.gz: b1b7bf6d190b8fbf1b502b4863292ccaa162ee56
4
+ data.tar.gz: 420a34cf26a72e581bb9996a91e25223b2073d74
5
5
  SHA512:
6
- metadata.gz: 0f8c67bea5558b108482a0cd37f0feb580cd6fb23ce1f94d719a686e45b3cd74c1734873ea6b7cc56ae1c0e89961dada45eeddebc468c0b4b37c2f4784801d90
7
- data.tar.gz: 102f11be19c4fa71f636d6bd4b965b4d278be436d6a2c9f67b135272881a73807d26b92041c3f187fb86bb8eb201e660e2fbcdb8425ab307f52bf809c30a1b07
6
+ metadata.gz: 8bd9987a400e6eda030160c1f013ddeb4399e642c1296230c67bc7bb283d859940ae36b65bc40326a093719960ef14d4e69758f332c603a038ac88dd7821bef9
7
+ data.tar.gz: 9fa7442e14c48e207d1a877579a5efd3a763e8d952a621d3350a0b381f2693c2f261e0132dda6cdf527f0e01c3e0895bb95c0db639f33daa1d31853757066731
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
@@ -0,0 +1,49 @@
1
+ require "module/module"
2
+ require './mono_talk/config.rb'
3
+
4
+ module MonoTalk
5
+ class Client
6
+ @@retry = 5
7
+
8
+ def initialize(servicename)
9
+ @io = STDERR
10
+ servicename = servicename.to_sym
11
+ @host = Mono::check_local(Mono::HOST[servicename])
12
+ @port = Mono::PORT[servicename]
13
+ @status = :close
14
+
15
+ @io.puts "Connect to #{@host}:#{@port}"
16
+
17
+ (@@retry+1).times do |index|
18
+ begin
19
+ open(@host,@port)
20
+ break
21
+ rescue
22
+ if index > @@retry
23
+ raise "Connection failed."
24
+ end
25
+ @io.puts "Connection failed, retrying connect to server... [#{index}]"
26
+ sleep 1
27
+ end
28
+ end
29
+ @io.puts "Connection successful."
30
+ end
31
+
32
+ def open(host=@host,port=@port)
33
+ if @status == :close
34
+ @client = TCPSocket.open(host, port)
35
+ @status = :open
36
+ end
37
+ end
38
+
39
+ def write(command_hash)
40
+ open()
41
+ @client.write(JSON.generate(command_hash)+"\n")
42
+ @io.puts ret = @client.gets.gsub("$n","\n")
43
+ @client.close
44
+ @status = :close
45
+ @io.puts 'Connection closed.'
46
+ return ret
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,12 @@
1
+ module MonoTalk
2
+ class SampleRoutine
3
+ def self.call(params)
4
+ return "CLASS routed: SampleRoutine in class. params => #{params.to_s}"
5
+ end
6
+ end
7
+
8
+ SAMPLE_ROUTINE ={
9
+ "TEST" => proc{|params| "Test succeed."},
10
+ "CLASS" => SampleRoutine,
11
+ }
12
+ end
@@ -0,0 +1,24 @@
1
+
2
+ $LOAD_PATH.unshift File.expand_path('lib')
3
+
4
+ require 'json'
5
+ require "socket"
6
+ require 'module/consts'
7
+
8
+ #Server / Client共通の処理・インターフェイスを定義する
9
+ module Mono
10
+ module Module
11
+ @@local_ip = nil
12
+ def define_local(ip)
13
+ @@local_ip = ip
14
+ end
15
+
16
+ def check_local(ip)
17
+ return ip unless @@local_ip
18
+ return "127.0.0.1" if ip == @@local_ip
19
+ return ip
20
+ end
21
+ end
22
+
23
+ extend Module
24
+ end
@@ -0,0 +1,60 @@
1
+ require 'color_print'
2
+ require './mono_talk/config'
3
+
4
+ module MonoTalk
5
+ class Server
6
+ @routine = Hash.new
7
+
8
+ def initialize(servicename,routine)
9
+ @io = STDERR
10
+ @servicename = servicename.to_sym
11
+ @routine = routine
12
+
13
+ @io.puts Color.cyan "Command List"
14
+ @routine.each do |key,value|
15
+ @io.puts "#{key} -> #{value}"
16
+ end
17
+ end
18
+
19
+ def accept
20
+ @server = TCPServer.open(Mono::PORT[@servicename])
21
+ @io.puts 'Acception start, waiting for data...'
22
+ @sock = @server.accept
23
+ @server.close
24
+ begin
25
+ hash = JSON.parse @sock.gets
26
+ @io.puts hash
27
+ @sock.write routing(hash)
28
+ rescue => e
29
+ @io.puts Color.red 'GIVE UP! SOMETHING BAD HAPPEN!'
30
+ @io.puts Color.yellow hash
31
+ @io.puts Color.magenta e
32
+ ensure
33
+ @sock.close
34
+ @io.puts 'Socket was closed.'
35
+ end
36
+ end
37
+
38
+ def test(hash)
39
+ @io.puts Color.yellow "Starting test..."
40
+ @server = TCPServer.open(Mono::PORT[@servicename])
41
+ STDERR.puts 'Acception start, waiting for data...'
42
+ @server.close
43
+ @io.puts hash
44
+ ret = routing(hash)
45
+ @io.puts ret
46
+ @io.puts 'Test was closed.'
47
+ return ret
48
+ end
49
+
50
+ def routing(hash)
51
+ return "Undefined command, ABORT -> #{hash['command']}" unless @routine[hash['command']]
52
+ return @routine[hash['command']].call(hash['params'])
53
+ end
54
+
55
+ def close
56
+ @server.close
57
+ @io.puts 'Server closed.'
58
+ end
59
+ end
60
+ end
@@ -1,3 +1,3 @@
1
1
  module MonoTalk
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/mono_talk.rb CHANGED
@@ -1,3 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path('lib')
2
+
1
3
  require "mono_talk/version"
2
4
  require 'module/server'
3
5
  require 'module/client'
@@ -0,0 +1,9 @@
1
+ module Mono
2
+ PORT = {
3
+ test: 8000
4
+ }
5
+
6
+ HOST = {
7
+ test: "127.0.0.1"
8
+ }
9
+ end
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift File.expand_path('./lib')
2
+ require 'module/server'
3
+ require 'module/client'
4
+ require 'module/consts'
5
+ require 'mono_talk/version'
6
+
7
+ server = MonoTalk::Server.new(:test,MonoTalk::SAMPLE_ROUTINE)
8
+
9
+ describe MonoTalk do
10
+ it 'has a version number' do
11
+ expect(MonoTalk::VERSION).not_to be nil
12
+ end
13
+
14
+ it 'should be "Test succeed."' do
15
+ expect(server.test({"command" => "TEST"}) == "Test succeed.").not_to be false
16
+ end
17
+
18
+ it 'should be run expectedly.' do
19
+ client = nil
20
+ t = Thread.new do
21
+ client = MonoTalk::Client.new(:test)
22
+ client.write({"command"=>"TEST"})
23
+ end
24
+ server.accept
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mono_talk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tatumaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-29 00:00:00.000000000 Z
11
+ date: 2014-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -46,13 +46,21 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
49
51
  - Gemfile
50
52
  - LICENSE.txt
51
53
  - README.md
52
54
  - Rakefile
55
+ - lib/module/client.rb
56
+ - lib/module/consts.rb
57
+ - lib/module/module.rb
58
+ - lib/module/server.rb
53
59
  - lib/mono_talk.rb
54
60
  - lib/mono_talk/version.rb
55
61
  - mono_talk.gemspec
62
+ - mono_talk/config.rb
63
+ - spec/mono_talk_spec.rb
56
64
  homepage: ''
57
65
  licenses:
58
66
  - MIT
@@ -77,4 +85,5 @@ rubygems_version: 2.3.0
77
85
  signing_key:
78
86
  specification_version: 4
79
87
  summary: Mono talk the simplest TCP conversation tool.
80
- test_files: []
88
+ test_files:
89
+ - spec/mono_talk_spec.rb