class Mumble::Client

Constants

CODEC_OPUS

Attributes

channels[R]
users[R]

Public Class Methods

new(host, port=64738, username="RubyClient", password="") { |config| ... } click to toggle source
# File lib/mumble-ruby2/client.rb, line 14
def initialize(host, port=64738, username="RubyClient", password="")
  @users, @channels = {}, {}
  @callbacks = Hash.new { |h, k| h[k] = [] }

  @config = Mumble.configuration.dup.tap do |c|
    c.host = host
    c.port = port
    c.username = username
    c.password = password
  end
  yield(@config) if block_given?
end

Public Instance Methods

cert_manager() click to toggle source
# File lib/mumble-ruby2/client.rb, line 49
def cert_manager
  @cert_manager ||= CertManager.new @config.username, @config.ssl_cert_opts
end
connect() click to toggle source
# File lib/mumble-ruby2/client.rb, line 27
def connect
  @conn = Connection.new @config.host, @config.port, cert_manager
  @conn.connect

  init_callbacks
  version_exchange
  authenticate

  spawn_threads :read, :ping
  connected? # just to get a nice return value
end
connected?() click to toggle source
# File lib/mumble-ruby2/client.rb, line 45
def connected?
  @connected ||= false
end
disconnect() click to toggle source
# File lib/mumble-ruby2/client.rb, line 39
def disconnect
  kill_threads
  @conn.disconnect
  @connected = false
end
find_channel(name) click to toggle source
# File lib/mumble-ruby2/client.rb, line 108
def find_channel(name)
  channels.values.find { |c| c.name == name }
end
find_user(name) click to toggle source
# File lib/mumble-ruby2/client.rb, line 104
def find_user(name)
  users.values.find { |u| u.name == name }
end
join_channel(channel) click to toggle source
# File lib/mumble-ruby2/client.rb, line 71
def join_channel(channel)
  id = channel_id channel
  send_user_state(session: @session, channel_id: id)
  channels[id]
end
me() click to toggle source
# File lib/mumble-ruby2/client.rb, line 63
def me
  users[@session]
end
move_user(user, channel) click to toggle source
# File lib/mumble-ruby2/client.rb, line 77
def move_user(user, channel)
  cid = channel_id channel
  uid = user_session user
  send_user_state(session: uid, channel_id: cid)
  channels[cid]
end
on_connected(&block) click to toggle source
# File lib/mumble-ruby2/client.rb, line 112
def on_connected(&block)
  @callbacks[:connected] << block
end
player() click to toggle source
# File lib/mumble-ruby2/client.rb, line 58
def player
  raise NoSupportedCodec unless @codec
  @audio_streamer ||= AudioPlayer.new @codec, @conn, @config.sample_rate, @config.bitrate
end
recorder() click to toggle source
# File lib/mumble-ruby2/client.rb, line 53
def recorder
  raise NoSupportedCodec unless @codec
  @recorder ||= AudioRecorder.new self, @config.sample_rate
end
remove_callback(symbol, callback) click to toggle source
# File lib/mumble-ruby2/client.rb, line 116
def remove_callback(symbol, callback)
  @callbacks[symbol].delete callback
end
set_comment(comment="") click to toggle source
# File lib/mumble-ruby2/client.rb, line 67
def set_comment(comment="")
  send_user_state(comment: comment)
end
text_channel(channel, string) click to toggle source
# File lib/mumble-ruby2/client.rb, line 94
def text_channel(channel, string)
  id = channel_id channel
  send_text_message(channel_id: [id], message: string)
  channels[id]
end
text_channel_img(channel, file) click to toggle source
# File lib/mumble-ruby2/client.rb, line 100
def text_channel_img(channel, file)
  text_channel(channel, ImgReader.msg_from_file(file))
end
text_user(user, string) click to toggle source
# File lib/mumble-ruby2/client.rb, line 84
def text_user(user, string)
  session = user_session user
  send_text_message(session: [user_session(user)], message: string)
  users[session]
end
text_user_img(user, file) click to toggle source
# File lib/mumble-ruby2/client.rb, line 90
def text_user_img(user, file)
  text_user(user, ImgReader.msg_from_file(file))
end

Private Instance Methods

authenticate() click to toggle source
# File lib/mumble-ruby2/client.rb, line 186
def authenticate
  send_authenticate({
    username: @config.username,
    password: @config.password,
    opus: true
  })
end
channel_id(channel) click to toggle source
# File lib/mumble-ruby2/client.rb, line 198
def channel_id(channel)
  channel = find_channel(channel) if channel.is_a? String
  id = channel.respond_to?(:channel_id) ? channel.channel_id : channel

  raise ChannelNotFound unless @channels.has_key? id
  id
end
codec_negotiation(message) click to toggle source
# File lib/mumble-ruby2/client.rb, line 194
def codec_negotiation(message)
  @codec = CODEC_OPUS if message.opus
end
encode_version(major, minor, patch) click to toggle source
# File lib/mumble-ruby2/client.rb, line 214
def encode_version(major, minor, patch)
  (major << 16) | (minor << 8) | (patch & 0xFF)
end
init_callbacks() click to toggle source
# File lib/mumble-ruby2/client.rb, line 146
def init_callbacks
  on_server_sync do |message|
    @session = message.session
    @connected = true
    @callbacks[:connected].each { |c| c.call }
  end
  on_channel_state do |message|
    if channel = channels[message.channel_id]
      channel.update message.to_hash
    else
      channels[message.channel_id] = Channel.new(self, message.to_hash)
    end
  end
  on_channel_remove do |message|
    channels.delete(message.channel_id)
  end
  on_user_state do |message|
    if user = users[message.session]
      user.update(message.to_hash)
    else
      users[message.session] = User.new(self, message.to_hash)
    end
  end
  on_user_remove do |message|
    users.delete(message.session)
  end
  on_codec_version do |message|
    codec_negotiation(message)
  end
end
ping() click to toggle source
# File lib/mumble-ruby2/client.rb, line 137
def ping
  send_ping timestamp: Time.now.to_i
  sleep(20)
end
read() click to toggle source
# File lib/mumble-ruby2/client.rb, line 131
def read
  message = @conn.read_message
  sym = message.class.to_s.demodulize.underscore.to_sym
  run_callbacks sym, Hashie::Mash.new(message.to_hash)
end
run_callbacks(sym, *args) click to toggle source
# File lib/mumble-ruby2/client.rb, line 142
def run_callbacks(sym, *args)
  @callbacks[sym].each { |c| c.call *args }
end
user_session(user) click to toggle source
# File lib/mumble-ruby2/client.rb, line 206
def user_session(user)
  user = find_user(user) if user.is_a? String
  id = user.respond_to?(:session) ? user.session : user

  raise UserNotFound unless @users.has_key? id
  id
end
version_exchange() click to toggle source
# File lib/mumble-ruby2/client.rb, line 177
def version_exchange
  send_version({
    version: encode_version(1, 2, 10),
    release: "mumble-ruby2 #{Mumble::VERSION}",
    os: %x{uname -s}.strip,
    os_version: %x{uname -v}.strip
  })
end