Module: Polyphony

Defined in:
lib/polyphony.rb,
lib/polyphony/net.rb,
lib/polyphony/version.rb,
lib/polyphony/core/sync.rb,
lib/polyphony/core/debug.rb,
lib/polyphony/core/timer.rb,
lib/polyphony/core/channel.rb,
lib/polyphony/core/throttler.rb,
lib/polyphony/core/exceptions.rb,
lib/polyphony/adapters/process.rb,
lib/polyphony/core/thread_pool.rb,
lib/polyphony/extensions/kernel.rb,
lib/polyphony/core/resource_pool.rb,
ext/polyphony/polyphony.c

Overview

Polyphony API

Defined Under Namespace

Modules: Net, Process, Trace, TrapInterceptor Classes: BaseException, Cancel, Channel, ConditionVariable, Interjection, Monitor, MoveOn, Mutex, Pipe, Queue, ResourcePool, Restart, Terminate, ThreadPool, Throttler, TimeoutException, Timer

Class Method Summary collapse

Class Method Details

.backend_accept(server_socket, socket_class) ⇒ Socket

Accepts an incoming connection on the given server socket, returning an instance of the given socket class.

Parameters:

  • server_socket (Socket)

    socket to accept on

  • socket_class (Class)

    class of the socket to instantiate for the accepted connection

Returns:

  • (Socket)

    accepted connection



67
68
69
# File 'ext/polyphony/polyphony.c', line 67

VALUE Polyphony_backend_accept(VALUE self, VALUE server_socket, VALUE socket_class) {
  return Backend_accept(BACKEND(), server_socket, socket_class);
}

.backend_accept_loop(server_socket, socket_class) {|Socket| ... } ⇒ nil

Runs an infinite loop accepting connections on the given server socket, returning an instance of the given socket class.

Parameters:

  • server_socket (Socket)

    socket to accept on

  • socket_class (Class)

    class of the socket to instantiate for the accepted connection

Yields:

  • (Socket)

    accepted connection

Returns:

  • (nil)


80
81
82
# File 'ext/polyphony/polyphony.c', line 80

VALUE Polyphony_backend_accept_loop(VALUE self, VALUE server_socket, VALUE socket_class) {
  return Backend_accept_loop(BACKEND(), server_socket, socket_class);
}

.backend_close(io) ⇒ IO, Polyphony::Pipe

Closes the given IO.

Parameters:

Returns:



400
401
402
# File 'ext/polyphony/polyphony.c', line 400

VALUE Polyphony_backend_close(VALUE self, VALUE io) {
  return Backend_close(BACKEND(), io);
}

.backend_connect(io, addr, port) ⇒ Socket

Connects the given socket to the given address and port.

Parameters:

  • io (Socket)

    socket to connect

  • addr (String)

    address to connect to

  • port (Integer)

    port to connect to

Returns:

  • (Socket)

    accepted connection



93
94
95
# File 'ext/polyphony/polyphony.c', line 93

VALUE Polyphony_backend_connect(VALUE self, VALUE io, VALUE addr, VALUE port) {
  return Backend_connect(BACKEND(), io, addr, port);
}

.backend_feed_loop(io, receiver, method) ⇒ IO

Runs a feed loop, reading data from the given io, feeding it to the receiver with the given method. The loop terminates when EOF is encountered. If a block is given, it is used as the block for the method call to the receiver.

Parameters:

  • io (IO)

    io to read from

  • receiver (any)

    an object receiving the data

  • method (Symbol)

    method used to feed the data to the receiver

Returns:

  • (IO)

    io



108
109
110
# File 'ext/polyphony/polyphony.c', line 108

VALUE Polyphony_backend_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method) {
  return Backend_feed_loop(BACKEND(), io, receiver, method);
}

.backend_read(io, buffer, length, to_eof, pos) ⇒ String

Reads from the given io.

Parameters:

  • io (IO)

    io to read from

  • buffer (String, nil)

    buffer to read into

  • length (Integer)

    maximum bytes to read

  • to_eof (boolean)

    whether to read to EOF

  • pos (Integer)

    Position in the buffer to read into

Returns:

  • (String)

    buffer



123
124
125
# File 'ext/polyphony/polyphony.c', line 123

VALUE Polyphony_backend_read(VALUE self, VALUE io, VALUE buffer, VALUE length, VALUE to_eof, VALUE pos) {
  return Backend_read(BACKEND(), io, buffer, length, to_eof, pos);
}

.backend_read_loop(io, maxlen) ⇒ IO

Performs an infinite loop reading data from the given io. The loop terminates when EOF is encountered.

Parameters:

  • io (IO)

    io to read from

  • maxlen (Integer)

    maximum bytes to read

Returns:

  • (IO)

    io



136
137
138
# File 'ext/polyphony/polyphony.c', line 136

VALUE Polyphony_backend_read_loop(VALUE self, VALUE io, VALUE maxlen) {
  return Backend_read_loop(BACKEND(), io, maxlen);
}

.backend_recv(io, buffer, length, pos) ⇒ String

Receives data on the given io.

Parameters:

  • io (Socket)

    io to receive on

  • buffer (String, nil)

    buffer to read into

  • length (Integer)

    maximum bytes to read

  • pos (Integer)

    Position in the buffer to read into

Returns:

  • (String)

    buffer



150
151
152
# File 'ext/polyphony/polyphony.c', line 150

VALUE Polyphony_backend_recv(VALUE self, VALUE io, VALUE buffer, VALUE length, VALUE pos) {
  return Backend_recv(BACKEND(), io, buffer, length, pos);
}

.backend_recv_feed_loop(socket, receiver, method) ⇒ Socket

Runs a feed loop, receiving data on the given socket, feeding it to the receiver with the given method. The loop terminates when EOF is encountered. If a block is given, it is used as the block for the method call to the receiver.

Parameters:

  • socket (Socket)

    socket to receive on

  • receiver (any)

    an object receiving the data

  • method (Symbol)

    method used to feed the data to the receiver

Returns:



195
196
197
# File 'ext/polyphony/polyphony.c', line 195

VALUE Polyphony_backend_recv_feed_loop(VALUE self, VALUE socket, VALUE receiver, VALUE method) {
  return Backend_recv_feed_loop(BACKEND(), socket, receiver, method);
}

.backend_recv_loop(socket, maxlen) {|data| ... } ⇒ Socket

Performs an infinite loop receiving data on the given socket. The loop terminates when the socket is closed.

Parameters:

  • socket (Socket)

    socket to receive on

  • maxlen (Integer)

    maximum bytes to read

Yields:

  • (data)

    received data

Returns:



179
180
181
# File 'ext/polyphony/polyphony.c', line 179

VALUE Polyphony_backend_recv_loop(VALUE self, VALUE socket, VALUE maxlen) {
  return Backend_recv_loop(BACKEND(), socket, maxlen);
}

.backend_recvmsg(socket, buffer, maxlen, pos, flags, maxcontrollen, opts) ⇒ String

Receives a message on the given socket.

Parameters:

  • socket (UDPSocket)

    io to receive on

  • buffer (String, nil)

    buffer to read into

  • maxlen (Integer)

    maximum bytes to read

  • pos (Integer)

    Position in the buffer to read into

  • flags (Integer)

    Flags

  • maxcontrollen (Integer)

    Maximum control bytes

  • opts (Hash)

    Options

Returns:

  • (String)

    buffer



166
167
168
# File 'ext/polyphony/polyphony.c', line 166

VALUE Polyphony_backend_recvmsg(VALUE self, VALUE socket, VALUE buffer, VALUE maxlen, VALUE pos, VALUE flags, VALUE maxcontrollen, VALUE opts) {
  return Backend_recvmsg(BACKEND(), socket, buffer, maxlen, pos, flags, maxcontrollen, opts);
}

.backend_send(socket, msg, flags) ⇒ Integer

Sends data on the given socket, returning the number of bytes sent.

Parameters:

  • socket (Socket)

    socket to read from

  • msg (String)

    data to be sent

  • flags (Integer)

    Flags

Returns:

  • (Integer)

    number of bytes sent



208
209
210
# File 'ext/polyphony/polyphony.c', line 208

VALUE Polyphony_backend_send(VALUE self, VALUE socket, VALUE msg, VALUE flags) {
  return Backend_send(BACKEND(), socket, msg, flags);
}

.backend_sendmsg(socket, msg, flags, dest_sockaddr, controls) ⇒ Integer

Sends data on the given socket, returning the number of bytes sent.

Parameters:

  • socket (Socket)

    socket to read from

  • msg (String)

    data to be sent

  • flags (Integer)

    Flags

  • dest_sockaddr (any)

    Destination address

  • controls (any)

    Control data

Returns:

  • (Integer)

    number of bytes sent



222
223
224
# File 'ext/polyphony/polyphony.c', line 222

VALUE Polyphony_backend_sendmsg(VALUE self, VALUE socket, VALUE msg, VALUE flags, VALUE dest_sockaddr, VALUE controls) {
  return Backend_sendmsg(BACKEND(), socket, msg, flags, dest_sockaddr, controls);
}

.backend_sendv(socket, ary, flags) ⇒ Integer

Sends multiple strings on the given socket, returning the number of bytes sent.

Parameters:

  • socket (Socket)

    socket to read from

  • ary (Array<String>)

    data to be sent

  • flags (Integer)

    Flags

Returns:

  • (Integer)

    number of bytes sent



235
236
237
# File 'ext/polyphony/polyphony.c', line 235

VALUE Polyphony_backend_sendv(VALUE self, VALUE socket, VALUE ary, VALUE flags) {
  return Backend_sendv(BACKEND(), socket, ary, flags);
}

.backend_sleep(duration) ⇒ nil

Sleeps for the given duration, yielding execution to other fibers.

Parameters:

  • duration (Number)

    duration in seconds

Returns:

  • (nil)


245
246
247
# File 'ext/polyphony/polyphony.c', line 245

VALUE Polyphony_backend_sleep(VALUE self, VALUE duration) {
  return Backend_sleep(BACKEND(), duration);
}

.backend_splice(src, dest, maxlen) ⇒ Integer

Splices data from the given source to the given destination, returning the number of bytes spliced. If maxlen is negative, splices repeatedly using absolute value of maxlen until EOF is encountered.

Parameters:

  • src (IO)

    source

  • dest (IO)

    destination

  • maxlen (Integer)

    Maximum bytes to splice

Returns:

  • (Integer)

    number of bytes spliced



259
260
261
# File 'ext/polyphony/polyphony.c', line 259

VALUE Polyphony_backend_splice(VALUE self, VALUE src, VALUE dest, VALUE maxlen) {
  return Backend_splice(BACKEND(), src, dest, maxlen);
}

.backend_timeout(duration) ⇒ any .backend_timeout(duration, exception_class) ⇒ any

Runs the given block, raising an exception if the block has not finished running before a timeout has elapsed, using the given duration. If an exception class is not given, a TimeoutError is raised.

Overloads:

  • .backend_timeout(duration) ⇒ any

    Return value of block

    Parameters:

    • duration (Number)

      timeout duration in seconds

    Returns:

    • (any)

      return value of block

  • .backend_timeout(duration, exception_class) ⇒ any

    Return value of block

    Parameters:

    • duration (Number)

      timeout duration in seconds

    • exception_class (Class)

      exception class to raise in case of timeout

    Returns:

    • (any)

      return value of block



292
293
294
# File 'ext/polyphony/polyphony.c', line 292

VALUE Polyphony_backend_timeout(int argc,VALUE *argv, VALUE self) {
  return Backend_timeout(argc, argv, BACKEND());
}

.backend_timer_loop(interval) ⇒ Object

Runs an infinite loop that calls the given block at the specified time interval.

Parameters:

  • interval (Number)

    interval in seconds



301
302
303
# File 'ext/polyphony/polyphony.c', line 301

VALUE Polyphony_backend_timer_loop(VALUE self, VALUE interval) {
  return Backend_timer_loop(BACKEND(), interval);
}

.backend_verify_blocking_mode(io, blocking) ⇒ IO, Polyphony::Pipe

Ensures the given IO is in blocking/non-blocking mode.

Parameters:

  • io (IO, Polyphony::Pipe)

    IO instance

  • blocking (boolean)

    true for blocking, false for non-blocking mode

Returns:



411
412
413
# File 'ext/polyphony/polyphony.c', line 411

VALUE Polyphony_backend_verify_blocking_mode(VALUE self, VALUE io, VALUE blocking) {
  return Backend_verify_blocking_mode(BACKEND(), io, blocking);
}

.backend_wait_event(raise) ⇒ Object

For for the current fiber to be rescheduled, resuming the fiber with its resumed value. If raise is true and the resumed value is an exception, an exception will be raised.

Parameters:

  • raise (boolean)


312
313
314
# File 'ext/polyphony/polyphony.c', line 312

VALUE Polyphony_backend_wait_event(VALUE self, VALUE raise) {
  return Backend_wait_event(BACKEND(), raise);
}

.backend_wait_io(io, write) ⇒ nil

Waits for the given IO to be readable or writeable, according to the read_or_write parameter.

Parameters:

  • io (IO)
  • write (boolean)

    false for read, true for write

Returns:

  • (nil)


324
325
326
# File 'ext/polyphony/polyphony.c', line 324

VALUE Polyphony_backend_wait_io(VALUE self, VALUE io, VALUE write) {
  return Backend_wait_io(BACKEND(), io, write);
}

.backend_waitpid(pid) ⇒ Integer

Waits for the given process to terminate, returning its exit code.

Parameters:

  • pid (Integer)

    pid

Returns:

  • (Integer)

    exit code



334
335
336
# File 'ext/polyphony/polyphony.c', line 334

VALUE Polyphony_backend_waitpid(VALUE self, VALUE pid) {
  return Backend_waitpid(BACKEND(), pid);
}

.backend_write(*args) ⇒ Object

Writes one or more strings to the given io, returning the total number of bytes written.



342
343
344
# File 'ext/polyphony/polyphony.c', line 342

VALUE Polyphony_backend_write(int argc, VALUE *argv, VALUE self) {
  return Backend_write_m(argc, argv, BACKEND());
}

.pipePolyphony::Pipe

Creates a new Polyphony::Pipe instance.

Returns:



25
26
27
# File 'lib/polyphony.rb', line 25

def pipe
  Pipe.new
end

.watch_process(cmd = nil, &block) ⇒ Object

Launches a process using either a command or a block for a forked process, waiting for the child process to terminate.



49
50
51
# File 'lib/polyphony.rb', line 49

def watch_process(cmd = nil, &block)
  Polyphony::Process.watch(cmd, &block)
end