Skip to content

Commit

Permalink
Initial import of ideas around an automatic call distributor
Browse files Browse the repository at this point in the history
  • Loading branch information
bklang committed Oct 14, 2011
0 parents commit bb3b1b3
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.swp
Gemfile.lock
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source :rubygems
gemspec
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2011 The Adhearsion Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions ahn_acd.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
GEM_FILES = %w{
ahn_acd.gemspec
lib/ahn_acd.rb
lib/ahn_acd/queue_strategy.rb
lib/ahn_acd/round_robin.rb
lib/ahn_acd/round_robin_meetme.rb
config/ahn_acd.yml
}

Gem::Specification.new do |s|
s.name = "ahn_acd"
s.version = "0.0.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Ben Klang"]

s.date = Date.today.to_s
s.description = "Automatic Call Distributor (ACD) for Adhearsion. Currently implements only Round Robin distribution strategies."
s.email = "dev&adhearsion.com"

s.files = GEM_FILES

s.has_rdoc = true
s.homepage = "http://github.com/adhearsion/ahn_acd"
s.require_paths = ["lib"]
s.rubygems_version = "1.2.0"
s.summary = "Automatic Call Distributor for Adhearsion"

s.add_runtime_dependency 'adhearsion', ['~> 1.2.0']

s.specification_version = 2
end
1 change: 1 addition & 0 deletions config/ahn_acd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# You can use this file for component-specific configuration.
83 changes: 83 additions & 0 deletions lib/ahn_acd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#methods_for :dialplan do
#end
#
#initialization do
# COMPONENTS.ahn_acd[:queues].each do |q|
# AhnQueue.create q[:name], q[:queue_type], q[:agent_type]
# end
#end

class AhnQueue
include Singleton

def initialize
@queues = {}
end

def create(name, queue_type, agent_type = Agent)
synchronize do
@queues[name] = const_get(queue_type).new unless @queues.has_key?(name)
@queues[name].extend agent_type
end
end

def get_queue(name)
synchronize do
@queues[name]
end
end

def self.method_missing(method, *args, &block)
instance.send method, *args, &block
end

class QueuedCall
attr_accessor :call, :queued_time

def initialize(call)
@call = call
@queued_time = Time.now
end

def hold
call.execute 'StartMusicOnHold'
@latch = CountDownLatch.new 1
@latch.wait
call.execute 'StopMusicOnHold'
end

def make_ready!
@latch.countdown!
end
end

class Agent
def work(agent_call)
loop do
agent_call.execute 'Bridge', @queue.next_call
end
end
end

class CalloutAgent
def work(agent_channel)
@queue.next_call.each do |next_call|
next_call.dial agent_channel
end
end
end

class MeetMeAgent
include Agent

def work(agent_call)
loop do
agent_call.join agent_conf, @queue.next_call
end
end
end

class BridgeAgent
include Agent
end
end
17 changes: 17 additions & 0 deletions lib/ahn_acd/queue_strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'countdownlatch'

class AhnAcd
module QueueStrategy
def wrap_call(call)
QueuedCall.new(call) unless call.respond_to?(:queued_time)
end

def priority_enqueue(call)
enqueue call
end

def enqueue(call)
call.hold
end
end
end
35 changes: 35 additions & 0 deletions lib/ahn_acd/round_robin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'thread'

class AhnAcd
class RoundRobin
include QueueStrategy

def initialize
@queue = []
@conditional = ConditionVariable.new
end

def next_call
call = nil
synchronize do
@conditional.wait(@mutex) if @queue.length == 0
call = @queue.pop
end

call.make_ready!
call
end

# TODO: Add mechanism to add calls with higher priority to the front of the queue.

def enqueue(call)
call = wrap_call(call)
synchronize do
@queue << call
@conditional.signal if @queue.length == 1
end
super
end
end
end

35 changes: 35 additions & 0 deletions lib/ahn_acd/round_robin_meetme.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class AhnAcd
class RoundRobinMeetme
include QueueStrategy

def initialize(call)
@queue = []
end

def next_call
call = synchronize do
@queue.pop
end

call.make_ready!
call
end

def priority_enqueue(call)
call = wrap_call(call)

synchronize do
@queue.unshift call
end
super
end

def enqueue(call)
synchronize do
@queue << call
end
end
end
end


8 changes: 8 additions & 0 deletions spec/ahn_acd_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'rubygems'
require 'bundler'
Bundler.setup
Bundler.require

require 'adhearsion/component_manager/spec_framework'

component_name.upcase = ComponentTester.new("ahn_acd", File.dirname(__FILE__) + "/../..")

0 comments on commit bb3b1b3

Please sign in to comment.