krpc 0.3.1 → 0.3.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: 02d7e464d0718d9f223f97b336d4cc861f6bb975
4
- data.tar.gz: a89bd9cfb911f51e9b2ee1979338f0a1a260289b
3
+ metadata.gz: ef6917b47511f404073511fc327657a34d46967f
4
+ data.tar.gz: 06ef229097a2524042524b155ff38c3ff110f5ee
5
5
  SHA512:
6
- metadata.gz: 058ee29c70d2cbe13a2cbf9d1016a188d18572f565c142ace38dc626e2efb62172dbf2be128000bcd61ce43cb09043a3f70cab404d8879ebeded19c0749270a7
7
- data.tar.gz: ae5432d952cb0aad317c064cd85487df95a6f85dbfa76db1dbf981a0527b241eb660df55de05f985eba42a5d5cc4a98511aba474dbf4c8a11f2ebf30b87fe4a9
6
+ metadata.gz: cf70656dc94287f0d52f00af03d338258779612a56e09957a0b02e740155477baeb0c508e95dcff5dd5d3ebeb5e31949a2eb72b1e2e0296abcf098638ba5b176
7
+ data.tar.gz: 1e54734369a2c4f72add79e9325e71698f19ba3409bb84747f9351b8df9c72a020be5d1950b516ba6ebe5984351724774e612717d77a65eaa8b5c3555d3fa4e8
data/README.md CHANGED
@@ -10,6 +10,8 @@ Installation
10
10
 
11
11
  gem install krpc
12
12
 
13
+ or install the latest pre-release version (if [available](https://rubygems.org/gems/krpc/versions)): `gem install krpc --pre`
14
+
13
15
  Basic usage
14
16
  -------
15
17
 
@@ -94,7 +96,7 @@ SpaceCenter.transform_position(
94
96
  to :ReferenceFrame - The reference frame to covert the position vector to.
95
97
  ) :Array[Float, Float, Float] - The corresponding position vector in reference frame to.
96
98
 
97
- Converts a position vector from one reference frame to another.
99
+ Converts a position vector from one reference frame to another.
98
100
  => nil
99
101
  ```
100
102
 
@@ -132,7 +134,7 @@ pos_stream.remove #note: dead code - just as an example
132
134
 
133
135
  Want to know more?
134
136
  -------
135
- * Read official **kRPC documentation** at http://krpc.github.io/krpc/, with many great [tutorials and examples](http://krpc.github.io/krpc/tutorials.html).
136
- * Refer to **kRPC-rb documentation** at http://tewu.github.io/krpc-rb/doc
137
+ * Read official **kRPC documentation** at https://krpc.github.io/krpc, with many great [tutorials and examples](https://krpc.github.io/krpc/tutorials.html).
138
+ * Refer to **kRPC-rb documentation** at https://tewu.github.io/krpc-rb-apidocs
137
139
  * See official **kRPC forum thread** at http://forum.kerbalspaceprogram.com/threads/69313
138
140
 
@@ -9,28 +9,27 @@ Gem::Specification.new do |s|
9
9
  s.version = KRPC::VERSION
10
10
  s.authors = ["Tomasz Więch"]
11
11
  s.email = ["tewu.dev@gmail.com"]
12
-
12
+
13
13
  s.summary = "Client library for kRPC"
14
14
  s.description = "kRPC-rb is a Ruby client library for kRPC, a Kerbal Space Program mod that allows you to control KSP from external scripts running outside of the game."
15
15
  s.homepage = "https://github.com/TeWu/krpc-rb"
16
16
  s.license = "GPL-3.0"
17
-
18
- s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(doc|test|spec|features|bin/TestServer)/}) || f.start_with?('.') }
17
+
18
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(doc|test|spec|features|bin)/|Rakefile|CHANGELOG}) || f.start_with?('.') }
19
19
  s.require_paths = ["lib"]
20
20
  s.extra_rdoc_files = ["README.md"]
21
21
  s.rdoc_options << "--markup" << "markdown" <<
22
- "--format" << "hanna" <<
23
22
  "--title" << "kRPC-rb API Docs" <<
24
23
  "--main" << "README.md"
25
-
24
+
26
25
  s.required_ruby_version = ">= 2.1.0"
27
26
 
28
- s.add_runtime_dependency "google-protobuf", "~> 3.0.0.alpha.5"
29
- s.add_runtime_dependency "colorize", "~> 0.7"
30
- s.add_runtime_dependency "nokogiri", "~> 1.6"
31
- s.add_runtime_dependency "hanna-nouveau", "~> 0.4"
32
- s.add_development_dependency "bundler", "~> 1.11"
27
+ s.add_runtime_dependency "google-protobuf", "~> 3.3"
28
+ s.add_runtime_dependency "colorize", "~> 0.8"
29
+ s.add_runtime_dependency "nokogiri", "~> 1.8"
30
+ s.add_development_dependency "bundler", "~> 1.15"
33
31
  s.add_development_dependency "pry", "~> 0.10"
34
- s.add_development_dependency "rspec", "~> 3.4"
35
- s.add_development_dependency "rake", "~> 11.1"
32
+ s.add_development_dependency "rspec", "~> 3.6"
33
+ s.add_development_dependency "rake", "~> 12.0"
34
+ s.add_development_dependency "hanna-nouveau", "~> 1.0"
36
35
  end
@@ -4,13 +4,13 @@ require 'krpc/client'
4
4
 
5
5
  module KRPC
6
6
  class << self
7
-
7
+
8
8
  # Connect to a kRPC server, generate services API and return Client object. If the block is
9
9
  # given, then it's called passing Client object and the connection to kRPC server is closed
10
10
  # at the end of the block.
11
11
  def connect(*args, &block)
12
12
  Client.new(*args).connect!(&block)
13
13
  end
14
-
14
+
15
15
  end
16
16
  end
@@ -8,73 +8,73 @@ module KRPC
8
8
  attrs.any?{|a| a.start_with? prefix }
9
9
  end
10
10
  alias_method :asw?, :is_any_start_with?
11
-
11
+
12
12
  def is_a_property_accessor(attrs) asw?(attrs,"Property.") end
13
13
  def is_a_property_getter(attrs) asw?(attrs,"Property.Get(") end
14
14
  def is_a_property_setter(attrs) asw?(attrs,"Property.Set(") end
15
15
  def is_a_class_method_or_property_accessor(attrs) asw?(attrs,"Class.") end
16
16
  def is_a_class_method(attrs) asw?(attrs,"Class.Method(") end
17
- def is_a_class_static_method(attrs) asw?(attrs,"Class.StaticMethod(") end
17
+ def is_a_class_static_method(attrs) asw?(attrs,"Class.StaticMethod(") end
18
18
  def is_a_class_property_accessor(attrs) asw?(attrs,"Class.Property.") end
19
19
  def is_a_class_property_getter(attrs) asw?(attrs,"Class.Property.Get(") end
20
20
  def is_a_class_property_setter(attrs) asw?(attrs,"Class.Property.Set(") end
21
-
21
+
22
22
  def get_service_name(attrs)
23
23
  if is_a_class_method(attrs) || is_a_class_static_method(attrs)
24
- attrs.each do |a|
24
+ attrs.each do |a|
25
25
  return $1 if /^Class\.(?:Static)?Method\(([^,\.]+)\.[^,]+,[^,]+\)$/ =~ a
26
26
  end
27
27
  elsif is_a_class_property_accessor(attrs)
28
- attrs.each do |a|
28
+ attrs.each do |a|
29
29
  return $1 if /^Class\.Property.(?:Get|Set)\(([^,\.]+)\.[^,]+,[^,]+\)$/ =~ a
30
30
  end
31
31
  end
32
32
  raise(ValueError, "Procedure attributes are not a class method or property accessor")
33
33
  end
34
-
34
+
35
35
  def get_class_name(attrs)
36
36
  if is_a_class_method(attrs) || is_a_class_static_method(attrs)
37
- attrs.each do |a|
37
+ attrs.each do |a|
38
38
  return $1 if /^Class\.(?:Static)?Method\([^,\.]+\.([^,\.]+),[^,]+\)$/ =~ a
39
39
  end
40
40
  elsif is_a_class_property_accessor(attrs)
41
- attrs.each do |a|
41
+ attrs.each do |a|
42
42
  return $1 if /^Class\.Property.(?:Get|Set)\([^,\.]+\.([^,]+),[^,]+\)$/ =~ a
43
43
  end
44
44
  end
45
45
  raise(ValueError, "Procedure attributes are not a class method or property accessor")
46
46
  end
47
-
47
+
48
48
  def get_property_name(attrs)
49
49
  if is_a_property_accessor(attrs)
50
- attrs.each do |a|
50
+ attrs.each do |a|
51
51
  return $1 if /^Property\.(?:Get|Set)\((.+)\)$/ =~ a
52
52
  end
53
53
  end
54
54
  raise(ValueError, "Procedure attributes are not a property accessor")
55
55
  end
56
-
56
+
57
57
  def get_class_method_or_property_name(attrs)
58
58
  if is_a_class_method(attrs) || is_a_class_static_method(attrs) || is_a_class_property_accessor(attrs)
59
- attrs.each do |a|
59
+ attrs.each do |a|
60
60
  return $1 if /^Class\.(?:(?:Static)?Method|Property\.(?:Get|Set))\([^,]+,([^,]+)\)$/ =~ a
61
61
  end
62
62
  end
63
63
  raise(ValueError, "Procedure attributes are not a class method or class property accessor")
64
64
  end
65
-
65
+
66
66
  def get_parameter_type_attrs(pos, attrs)
67
- attrs.map do |a|
67
+ attrs.map do |a|
68
68
  (/^ParameterType\(#{pos}\).(.+)$/ =~ a) ? $1 : nil
69
69
  end.compact
70
70
  end
71
-
71
+
72
72
  def get_return_type_attrs(attrs)
73
- attrs.map do |a|
73
+ attrs.map do |a|
74
74
  (/^ReturnType.(.+)$/ =~ a) ? $1 : nil
75
75
  end.compact
76
76
  end
77
-
77
+
78
78
  end
79
79
  end
80
80
  end
@@ -9,11 +9,11 @@ require 'krpc/core_extensions'
9
9
  require 'krpc/krpc.pb'
10
10
 
11
11
  module KRPC
12
-
12
+
13
13
  ##
14
- # A kRPC client, through which all Remote Procedure Calls are made. To make RPC calls client
14
+ # A kRPC client, through which all Remote Procedure Calls are made. To make RPC calls client
15
15
  # must first connect to server. This can be achieved by calling Client#connect or Client#connect!
16
- # methods. Client object can connect and disconnect from the server many times during it's
16
+ # methods. Client object can connect and disconnect from the server many times during it's
17
17
  # lifetime. RPCs can be made by calling Client#execute_rpc method. After generating services API (with
18
18
  # Client#generate_services_api! call), RPCs can be also made using
19
19
  # `client.service_name.procedure_name(parameter, ...)`
@@ -32,9 +32,9 @@ module KRPC
32
32
 
33
33
  include Doc::SuffixMethods
34
34
 
35
- attr_reader :name, :rpc_connection, :stream_connection, :streams_manager, :krpc
35
+ attr_reader :name, :rpc_connection, :stream_connection, :streams_manager, :core
36
36
 
37
- # Create new Client object, optionally specifying IP address and port numbers on witch kRPC
37
+ # Create new Client object, optionally specifying IP address and port numbers on witch kRPC
38
38
  # server is listening and the name for this client (up to 32 bytes of UTF-8 encoded text).
39
39
  def initialize(name: DEFAULT_NAME, host: Connection::DEFAULT_SERVER_HOST, rpc_port: Connection::DEFAULT_SERVER_RPC_PORT, stream_port: Connection::DEFAULT_SERVER_STREAM_PORT)
40
40
  @name = name
@@ -42,13 +42,13 @@ module KRPC
42
42
  @stream_connection = StreamConnection.new(rpc_connection, host, stream_port)
43
43
  @streams_manager = Streaming::StreamsManager.new(self)
44
44
  @services = {}
45
- @krpc = Services::KRPC.new(self)
46
- Doc.add_docstring_info(false, self.class, "krpc", return_type: @krpc.class, xmldoc: "<doc><summary>Core kRPC service, e.g. for querying for the available services. Most of this functionality is used internally by the Ruby client and therefore does not need to be used directly from application code.</summary></doc>")
45
+ @core = Services::Core.new(self)
46
+ Doc.add_docstring_info(false, self.class, "core", return_type: @core.class, xmldoc: "<doc><summary>Core kRPC service, e.g. for querying for the available services. Most of this functionality is used internally by the Ruby client and therefore does not need to be used directly from application code. This service is hardcoded (in kRPC Ruby client) version of 'krpc' service, so 1) it is available even before services API is generated, but 2) can be out of sync with 'krpc' service.</summary></doc>")
47
47
  end
48
-
48
+
49
49
  # Connect to a kRPC server on the IP address and port numbers specified during this client
50
50
  # object creation and return `self`. Calling this method while the client is already connected
51
- # will raise an exception. If the block is given, then it's called passing `self` and the
51
+ # will raise an exception. If the block is given, then it's called passing `self` and the
52
52
  # connection to kRPC server is closed at the end of the block.
53
53
  def connect(&block)
54
54
  rpc_connection.connect
@@ -57,9 +57,9 @@ module KRPC
57
57
  call_block_and_close(block) if block_given?
58
58
  self
59
59
  end
60
-
61
- # Connect to a kRPC server, generate services API and return `self`. Shorthand for calling
62
- # Client#connect and Client#generate_services_api! subsequently. If the block is given, then
60
+
61
+ # Connect to a kRPC server, generate services API and return `self`. Shorthand for calling
62
+ # Client#connect and Client#generate_services_api! subsequently. If the block is given, then
63
63
  # it's called passing `self` and the connection to kRPC server is closed at the end of the block.
64
64
  def connect!(&block)
65
65
  connect
@@ -67,8 +67,8 @@ module KRPC
67
67
  call_block_and_close(block) if block_given?
68
68
  self
69
69
  end
70
-
71
- # Close connection to kRPC server. Returns `true` if the connection has closed or `false` if
70
+
71
+ # Close connection to kRPC server. Returns `true` if the connection has closed or `false` if
72
72
  # the client had been already disconnected.
73
73
  def close
74
74
  streams_manager.remove_all_streams
@@ -81,16 +81,16 @@ module KRPC
81
81
  def connected?
82
82
  rpc_connection.connected?
83
83
  end
84
-
85
- # Interrogates the server to find out what functionality it provides and dynamically creates
84
+
85
+ # Interrogates the server to find out what functionality it provides and dynamically creates
86
86
  # all of the classes and methods that form services API. For each service that server provides:
87
- #
87
+ #
88
88
  # 1. Class `KRPC::Services::{service name here}`, and module `KRPC::Gen::{service name here}`
89
89
  # are created.
90
90
  # 2. `KRPC::Gen::{service name here}` module is filled with dynamically created classes.
91
- # 3. Those classes in turn are filled with dynamically created methods, that form API for
91
+ # 3. Those classes in turn are filled with dynamically created methods, that form API for
92
92
  # this service.
93
- # 4. Instance method `{service name here}` is created in this client object that returns
93
+ # 4. Instance method `{service name here}` is created in this client object that returns
94
94
  # `KRPC::Services::{service name here}` object. This object is entry point for accessing
95
95
  # functionality provided by `{service name here}` service.
96
96
  #
@@ -108,10 +108,9 @@ module KRPC
108
108
  def generate_services_api!
109
109
  return self if services_api_generated?
110
110
  raise(Exception, "Can't generate services API while not connected to server -- call Client#connect! to connect to server and generate services API in one call") if not connected?
111
-
112
- resp = krpc.get_services
111
+
112
+ resp = core.get_services
113
113
  resp.services.each do |service_msg|
114
- next if service_msg.name == "KRPC"
115
114
  service_class = Services.create_service(service_msg)
116
115
  method_name = service_class.class_name.underscore
117
116
  self.class.instance_eval do
@@ -123,12 +122,12 @@ module KRPC
123
122
  end
124
123
  self
125
124
  end
126
-
125
+
127
126
  # Returns `true` if services API has been already generated, `false` otherwise.
128
127
  def services_api_generated?
129
128
  respond_to? :space_center or respond_to? :test_service
130
129
  end
131
-
130
+
132
131
  # Execute an RPC.
133
132
  def execute_rpc(service, procedure, args=[], kwargs={}, param_names=[], param_types=[], param_default=[], return_type: nil)
134
133
  send_request(service, procedure, args, kwargs, param_names, param_types, param_default)
@@ -159,11 +158,11 @@ module KRPC
159
158
  end
160
159
 
161
160
  protected #----------------------------------
162
-
161
+
163
162
  def construct_arguments(args, kwargs, param_names, param_types, param_default, required_params_count)
164
163
  param_names_symbols = param_names.map(&:to_sym)
165
164
  kwargs_remaining = kwargs.count
166
-
165
+
167
166
  req_args = param_names_symbols.map.with_index do |name, i|
168
167
  is_kwarg = kwargs.has_key? name
169
168
  kwargs_remaining -= 1 if is_kwarg
@@ -189,7 +188,7 @@ module KRPC
189
188
  PB::Argument.new(position: i, value: v)
190
189
  end
191
190
  end.compact
192
-
191
+
193
192
  raise ArgumentErrorSig.new("keyword arguments for non existing parameters: #{(kwargs.keys - param_names_symbols).join(", ")}") unless kwargs_remaining == 0
194
193
  req_args
195
194
  end
@@ -198,16 +197,16 @@ module KRPC
198
197
  req = build_request(service, procedure, args, kwargs, param_names, param_types, param_default)
199
198
  rpc_connection.send Encoder.encode_request(req)
200
199
  end
201
-
200
+
202
201
  def receive_response
203
202
  resp_length = rpc_connection.recv_varint
204
203
  resp_data = rpc_connection.recv resp_length
205
204
  resp = PB::Response.decode(resp_data)
206
205
  end
207
-
206
+
208
207
  def call_block_and_close(block)
209
208
  begin block.call(self) ensure close end
210
209
  end
211
-
210
+
212
211
  end
213
212
  end
@@ -10,18 +10,18 @@ module KRPC
10
10
  DEFAULT_SERVER_HOST = "127.0.0.1"
11
11
  DEFAULT_SERVER_RPC_PORT = 50000
12
12
  DEFAULT_SERVER_STREAM_PORT = 50001
13
-
13
+
14
14
  attr_reader :host, :port, :socket
15
-
15
+
16
16
  def initialize(host, port)
17
17
  @host, @port = host, port
18
18
  end
19
-
19
+
20
20
  # Connect and perform handshake.
21
21
  def connect
22
22
  if connected? then raise(ConnectionError, "Already connected")
23
- else
24
- @socket = TCPSocket.open(host, port)
23
+ else
24
+ @socket = TCPSocket.open(host, port)
25
25
  begin
26
26
  handshake
27
27
  rescue Exception => e
@@ -31,7 +31,7 @@ module KRPC
31
31
  end
32
32
  self
33
33
  end
34
-
34
+
35
35
  # Close connection and clean up.
36
36
  def close
37
37
  if connected?
@@ -40,20 +40,20 @@ module KRPC
40
40
  true
41
41
  else false end
42
42
  end
43
-
43
+
44
44
  # Return `true` if connected to a server, `false` otherwise.
45
45
  def connected?
46
46
  !socket.nil? && !socket.closed?
47
47
  end
48
-
48
+
49
49
  def handshake; end
50
50
  def cleanup; end
51
-
51
+
52
52
  def send(msg) @socket.send(msg, 0) end
53
53
  def recv(maxlen = 1)
54
54
  maxlen == 0 ? "" : @socket.read(maxlen)
55
55
  end
56
-
56
+
57
57
  def recv_varint
58
58
  int_val = 0
59
59
  shift = 0
@@ -65,9 +65,9 @@ module KRPC
65
65
  raise(RuntimeError, "too many bytes when decoding varint") if shift >= 64
66
66
  end
67
67
  end
68
-
68
+
69
69
  protected #----------------------------------
70
-
70
+
71
71
  def trim_fill(str, len, fill_char = "\x00")
72
72
  str = str.encode("UTF-8")[0, len]
73
73
  str + fill_char*(len-str.length)
@@ -78,7 +78,7 @@ module KRPC
78
78
  # TCP connection for sending RPC calls and retrieving it's results.
79
79
  class RPCConnection < Connection
80
80
  attr_reader :name, :client_id
81
-
81
+
82
82
  def initialize(name, host = DEFAULT_SERVER_HOST, port = DEFAULT_SERVER_RPC_PORT)
83
83
  super host, port
84
84
  @name = name
@@ -90,7 +90,7 @@ module KRPC
90
90
  send trim_fill(name, Encoder::NAME_LENGTH)
91
91
  @client_id = recv Decoder::GUID_LENGTH
92
92
  end
93
-
93
+
94
94
  # Clean up - sets `@client_id` to `nil`.
95
95
  def cleanup
96
96
  @client_id = nil
@@ -101,20 +101,20 @@ module KRPC
101
101
  # TCP connection for streaming.
102
102
  class StreamConnection < Connection
103
103
  attr_reader :rpc_connection
104
-
104
+
105
105
  def initialize(rpc_connection, host = DEFAULT_SERVER_HOST, port = DEFAULT_SERVER_STREAM_PORT)
106
106
  super host, port
107
107
  @rpc_connection = rpc_connection
108
108
  end
109
-
109
+
110
110
  # Perform handshake with kRPC server, sending `client_id` retrieved from `rpc_connection`.
111
111
  def handshake
112
- raise(ConnectionError, "RPC connection must obtain client_id before stream connection can perform valid handshake - closing stream connection") if rpc_connection.client_id.nil?
112
+ raise(ConnectionError, "RPC connection must obtain client_id before stream connection can perform valid handshake - closing stream connection") if rpc_connection.client_id.nil?
113
113
  send Encoder::STREAM_HELLO_MESSAGE
114
114
  send rpc_connection.client_id
115
115
  resp = recv Decoder::OK_LENGTH
116
116
  raise ConnectionError unless resp == Decoder::OK_MESSAGE
117
117
  end
118
118
  end
119
-
119
+
120
120
  end