Class: Fabriq::SkypeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/fabriq/skype_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ SkypeProxy

Returns a new instance of SkypeProxy.



6
7
8
9
10
11
12
# File 'lib/fabriq/skype_proxy.rb', line 6

def initialize(adapter)
  @adapter = adapter
  @incoming_mutex = Mutex.new
  @outgoing_mutex = Mutex.new
  @incoming_message_callbacks = []
  @outgoing_messages = []
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



4
5
6
# File 'lib/fabriq/skype_proxy.rb', line 4

def adapter
  @adapter
end

#outgoing_messagesObject

Returns the value of attribute outgoing_messages.



4
5
6
# File 'lib/fabriq/skype_proxy.rb', line 4

def outgoing_messages
  @outgoing_messages
end

Instance Method Details

#handle_incoming_messages_synchronizedObject



47
48
49
50
51
# File 'lib/fabriq/skype_proxy.rb', line 47

def handle_incoming_messages_synchronized
  @incoming_mutex.synchronize do
    invoke_incoming_message_subscribers(@received_messages.shift)
  end
end

#handle_outgoing_messages_synchronizedObject



53
54
55
56
57
58
59
60
# File 'lib/fabriq/skype_proxy.rb', line 53

def handle_outgoing_messages_synchronized
  @outgoing_mutex.synchronize do
    if @outgoing_messages.count > 0
      message = @outgoing_messages.shift
      @adapter.send_message(message)
    end
  end
end

#invoke_incoming_message_subscribers(message) ⇒ Object



62
63
64
65
66
# File 'lib/fabriq/skype_proxy.rb', line 62

def invoke_incoming_message_subscribers(message)
  @incoming_message_callbacks.each do |callback|
    run_in_thread { callback.call(message) }
  end
end

#on_incoming_message(&callback) ⇒ Object



14
15
16
# File 'lib/fabriq/skype_proxy.rb', line 14

def on_incoming_message(&callback)
  @incoming_message_callbacks << callback
end

#startObject



25
26
27
28
# File 'lib/fabriq/skype_proxy.rb', line 25

def start
  subscribe_adapter_message_received
  start_queue_worker_threads
end

#start_incoming_queue_workerObject



35
36
37
38
39
# File 'lib/fabriq/skype_proxy.rb', line 35

def start_incoming_queue_worker
  run_in_throttled_loop do
    handle_incoming_messages_synchronized
  end
end

#start_outgoing_queue_workerObject



41
42
43
44
45
# File 'lib/fabriq/skype_proxy.rb', line 41

def start_outgoing_queue_worker
  run_in_throttled_loop do
    handle_outgoing_messages_synchronized
  end
end

#start_queue_worker_threadsObject



30
31
32
33
# File 'lib/fabriq/skype_proxy.rb', line 30

def start_queue_worker_threads
  run_in_thread { start_incoming_queue_worker }
  run_in_thread { start_outgoing_queue_worker }
end

#subscribe_adapter_message_receivedObject



18
19
20
21
22
23
# File 'lib/fabriq/skype_proxy.rb', line 18

def subscribe_adapter_message_received
  @received_messages = []
  @adapter.message_received do |message|
    @received_messages << message
  end
end