Skip to content

Commit 4cf6ce3

Browse files
committed
server status
1 parent f0b2d8c commit 4cf6ce3

File tree

6 files changed

+60
-58
lines changed

6 files changed

+60
-58
lines changed

lib/scruby/graph/print.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ class Graph
33
module Print
44
class << self
55
def print(root)
6-
puts print_node(root, [], "", nil)
6+
puts visualize(root)
7+
end
8+
9+
def visualize(root)
10+
visualize_node(root, [], "", nil)
711
end
812

913
protected
1014

11-
def print_node(node, siblings, padding, param_name)
15+
def visualize_node(node, siblings, padding, param_name)
1216
is_last = node.eql? siblings.last
1317

1418
# TODO: refactor
@@ -41,7 +45,7 @@ def print_node(node, siblings, padding, param_name)
4145
("\n" if inputs.any?),
4246

4347
inputs.zip(param_names).map { |i, name|
44-
print_node(i, inputs, child_padding, name)
48+
visualize_node(i, inputs, child_padding, name)
4549
},
4650

4751
("\n" unless is_last)

lib/scruby/server.rb

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
module Scruby
77
class Server
88
class ServerError < StandardError; end
9+
class ClientInfo < Struct.new(:id, :max_logins); end
10+
911
extend Forwardable
1012

1113
include OSC
@@ -14,36 +16,31 @@ class ServerError < StandardError; end
1416
include Encode
1517
include Concurrent
1618

17-
attr_reader :client, :message_queue, :process, :options
19+
attr_reader :osc_client, :message_queue, :process, :options,
20+
:client_info
1821
private :message_queue, :process
1922

2023
def_delegators :options, :host, :port, :num_buffers
2124

2225

2326
def initialize(host: "127.0.0.1", port: 57_110, **options)
24-
@client = OSC::Client.new(port, host)
27+
@osc_client = OSC::Client.new(port, host)
2528
@message_queue = MessageQueue.new(self)
2629
@options = Options.new(**options, bind_address: host, port: port)
2730
@nodes = Nodes.new(self)
28-
29-
listen_node_changes
3031
end
3132

3233
def alive?
33-
message_queue.alive?
34+
process_alive?
3435
end
3536
alias running? alive?
3637

37-
def process_alive?
38-
process&.alive? || false
39-
end
40-
4138
def client_id
42-
@client_id ||= register_client.first
39+
client_info&.id
4340
end
4441

4542
def max_logins
46-
@max_logins ||= register_client.last
43+
client_info&.max_logins
4744
end
4845

4946
def nodes
@@ -71,15 +68,15 @@ def boot(binary: "scsynth", **opts)
7168
end
7269

7370
def boot_async(binary: "scsynth", **opts)
74-
return message_queue.sync.then { self } if process_alive?
71+
return sync.then { self } if process_alive?
7572

7673
@options = Options.new(**options, **opts)
7774
@num_buffers = options.num_buffers
7875
@process = Process.spawn(binary, options.flags, env: options.env)
7976

8077
wait_for_booted
8178
.then_flat_future { message_queue.sync }
82-
.then { register_client }
79+
.then_flat_future { register_client }
8380
.then { create_root_group }
8481
.then { self }
8582
.on_rejection { process.kill }
@@ -91,13 +88,13 @@ def quit
9188
alias stop quit
9289

9390
def quit_async
94-
return Promises.fulfilled_future(self) unless alive?
95-
91+
# return Promises.fulfilled_future(self) unless alive?
9692
send_msg("/quit")
9793

9894
receive { |msg| msg.to_a == %w(/done /quit) }
9995
.then { sleep 0.1 while process_alive? }
100-
.then { message_queue.stop }
96+
.then { message_queue.flush }
97+
.then { @client_info = nil }
10198
.then { self }
10299
end
103100
alias stop_async quit_async
@@ -125,7 +122,17 @@ def free_all
125122
# end
126123

127124
def status
128-
message_queue.status.value!
125+
status_async.value!
126+
end
127+
128+
def status_async
129+
keys = %i(ugens synths groups synth_defs avg_cpu peak_cpu
130+
sample_rate actual_sample_rate)
131+
132+
send_msg Message.new("/status")
133+
134+
receive("/status.reply", timeout: 0.2)
135+
.then { |msg| keys.zip(msg.args[1..-1]).to_h }
129136
end
130137

131138
# Sends an OSC command or +Message+ to the scsyth server.
@@ -143,9 +150,9 @@ def send_msg(message, *args)
143150

144151
case message
145152
when Message, Bundle
146-
client.send(message)
153+
osc_client.send(message)
147154
else
148-
client.send Message.new(message, *args)
155+
osc_client.send Message.new(message, *args)
149156
end
150157

151158
self
@@ -184,8 +191,20 @@ def dump_osc(code = 1)
184191
send_msg("/dumpOSC", code)
185192
end
186193

194+
def socket
195+
osc_client.instance_variable_get(:@socket)
196+
end
197+
187198
private
188199

200+
def process_alive?
201+
process&.alive? || false
202+
end
203+
204+
def sync
205+
message_queue.sync
206+
end
207+
189208
def update_nodes
190209
send_msg("/g_queryTree", 0, 1)
191210

@@ -227,9 +246,10 @@ def register_client
227246
send_msg("/notify", 1, 0)
228247

229248
receive("/done")
230-
.then { |m| @client_id, @max_logins = m.args.slice(1, 2) }
249+
.then { |m| m.args.slice(1, 2) }
231250
.then { |a| a.each { |v| raise ServerError, v if String === v } }
232-
.value!
251+
.then { |args| @client_info = ClientInfo.new(*args) }
252+
.then { listen_node_changes }
233253
end
234254

235255
def graph_completion_blob(message)

lib/scruby/server/message_queue.rb

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ def sync
3737
end
3838
end
3939

40-
def status
41-
keys = %i(ugens synths groups synth_defs avg_cpu peak_cpu
42-
sample_rate actual_sample_rate)
43-
44-
server.send_msg Message.new("/status")
45-
receive("/status.reply", timeout: 0.2)
46-
.then { |msg| keys.zip(msg.args[1..-1]).to_h }
47-
end
48-
4940
def run
5041
return thread if alive?
5142

@@ -59,16 +50,10 @@ def alive?
5950
end
6051
alias running? alive?
6152

62-
def stop
63-
return self unless alive?
64-
65-
thread.kill
66-
53+
def flush
6754
patterns.delete_if do |_, _, future|
6855
future.reject CancelledOperationError.new("server quitted")
6956
end
70-
71-
self
7257
end
7358

7459
private
@@ -97,7 +82,7 @@ def dispatch_msg(message)
9782
end
9883

9984
def socket
100-
server.client.instance_variable_get(:@socket)
85+
server.socket
10186
end
10287

10388
def cancellation_future(timeout)

lib/scruby/server/nodes.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ def node(idx)
4343
synchronize { graph[idx] }
4444
end
4545

46-
def print
47-
first.print
48-
end
49-
5046
def inspect
51-
print
47+
first.visualize
5248
end
5349

5450
private

lib/scruby/server_node.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ def fill
9494
def running?
9595
end
9696

97-
def register
98-
end
99-
100-
def unregister
101-
end
102-
10397
def create(*args)
10498
send_msg(creation_cmd, *args)
10599
end
@@ -112,8 +106,8 @@ def print_name
112106
"#{self.class.name.split('::').last} #{id}"
113107
end
114108

115-
def print
116-
Graph::Print.print(self)
109+
def visualize
110+
Graph::Print.visualize(self)
117111
end
118112

119113
private

spec/integration/operation_graph_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
RSpec.describe UgenGraph do
2+
let(:server) { Server.new }
3+
4+
before do
5+
allow(server).to receive(:send_msg)
6+
end
7+
28
describe "graph with float mult" do
3-
let(:server) { Server.new }
49
let(:graph) { UgenGraph.new(Out.ar(0, SinOsc.ar * 0.5), "mult") }
510

611
it { expect(graph.nodes.map(&:name))
712
.to eq %w(SinOsc BinaryOpUGen Out) }
813

914
it { expect(graph.constants.map(&:value)).to eq [ 440, 0, 0.5 ] }
1015

11-
# fBinaryOpUGen\x02\x00\x00\x00\x02\x00\x00\x00\x01\x00\x02
12-
# fBinaryOpUGen\x02\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00
13-
1416
let(:expected) do
1517
[ 83, 67, 103, 102, 0, 0, 0, 2, 0, 1, 4, 109, 117, 108, 116, 0,
1618
0, 0, 3, 67, -36, 0, 0, 0, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0,
@@ -29,6 +31,7 @@
2931

3032
it "sends graph" do
3133
expect(graph.send_to(server)).to eq(graph)
34+
expect(graph.send_to(server)).to eq(graph)
3235
end
3336
end
3437

0 commit comments

Comments
 (0)