distributor 0.3.0 → 0.4.0

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.
@@ -55,15 +55,20 @@ class Distributor::Client
55
55
  @multiplexer.output ch, data
56
56
  end
57
57
 
58
+ def command(command, data={})
59
+ data["id"] ||= generate_id
60
+ data["command"] = command
61
+ @multiplexer.output 0, Distributor::OkJson.encode(data)
62
+ data["id"]
63
+ end
64
+
58
65
  def run(command, &handler)
59
- id = generate_id
60
- @multiplexer.output 0, Distributor::OkJson.encode({ "id" => id, "command" => "run", "args" => command })
66
+ id = command("run", "args" => command)
61
67
  @handlers[id] = handler
62
68
  end
63
69
 
64
70
  def tunnel(port, &handler)
65
- id = generate_id
66
- @multiplexer.output 0, Distributor::OkJson.encode({ "id" => id, "command" => "tunnel", "port" => port })
71
+ id = command("tunnel", "port" => port)
67
72
  @handlers[id] = handler
68
73
  end
69
74
 
@@ -77,7 +82,7 @@ class Distributor::Client
77
82
  output.write data
78
83
  output.flush
79
84
  rescue EOFError
80
- @multiplexer.output 0, Distributor::OkJson.encode({ "command" => "close", "ch" => ch })
85
+ command "close", "ch" => ch
81
86
  end
82
87
  end
83
88
  end
@@ -10,6 +10,7 @@ class Distributor::Server
10
10
  def initialize(input, output=input)
11
11
  @connector = Distributor::Connector.new
12
12
  @multiplexer = Distributor::Multiplexer.new(output)
13
+ @on_command = Proc.new {}
13
14
 
14
15
  # reserve a command channel
15
16
  @multiplexer.reserve(0)
@@ -39,7 +40,7 @@ class Distributor::Server
39
40
  ch = run(data["args"])
40
41
  @multiplexer.output 0, Distributor::OkJson.encode({ "id" => data["id"], "command" => "ack", "ch" => ch })
41
42
  else
42
- raise "no such command: #{command}"
43
+ @on_command.call command, data
43
44
  end
44
45
  end
45
46
  end
@@ -92,6 +93,11 @@ class Distributor::Server
92
93
 
93
94
  ch
94
95
  end
96
+
97
+ def on_command(&blk)
98
+ @on_command = blk
99
+ end
100
+
95
101
  def start
96
102
  @multiplexer.output 0, Distributor::OkJson.encode({ "command" => "hello" })
97
103
  loop { @connector.listen }
@@ -1,5 +1,5 @@
1
1
  module Distributor
2
2
 
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: distributor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-15 00:00:00.000000000 Z
12
+ date: 2012-06-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
16
- requirement: &70227170941980 !ruby/object:Gem::Requirement
16
+ requirement: &70192600707700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 0.13.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70227170941980
24
+ version_requirements: *70192600707700
25
25
  description: TCP Multiplexer
26
26
  email: ddollar@gmail.com
27
27
  executables: []