Thursday, October 1, 2015

[7.2] getCallHandler()

1. Example

              ------------------>PM
              |                           |
              |-----------------?  TL  <-----------------------
                                          |                                      |
fresher1  fresher2  fresher3  fresher4  fresher5  fresher6

   X             X            X            X            X            O ->?

Call coming in districute call.rank=0 -> Emp check every level employee to see availability
                              Emp.receive(call)
                              Emp.cannotHandle(call)
                                    - call.rank +1
                                    - distribute call.rank+1 again
                                   - find next level available Emp
                        -> Queue, no one is available
Employee
   - free
   - rank
 - CallHandler instance
    - void ReceiveCall(Call call)
   - void CallHandled(Call call)
   - void CannontHandle(Call call)
Call
   - rank
  - void reply()
 - void disconnect()
CallHandler
 - Employee callHandler(Call call)
 - void dispatchCall(Call call)
 - void getNextCall(Employee emp)

2. Implementation
Q1: getHandler() check available and check canHandle or not
A1: If not handle ==> call CallHanlder class to do the distribute job
AND callHandler call next level people to Handle call
if handled, complete, otherwise, call CallHandler class again
A1: available -> every ppl has a field called free, ==> Employee has 3 types
Employee class
fresher class
TL class
PM class
CallHandler class hook up call and Employee
Call class ??


public class CallHandler  // Hook up call and employee
{
  static final int LEVELS = 3;// we have 3 level of employees
    static final int NUM_FRESHERS = 5;// we have 5 freshers
    // Use ArrayList[] instead of ArrayList> since we know the size
    ArrayList[] employeeLevels = new ArrayList[LEVELS];
    // queue for each call's rank
    Queue[]  callQueues = new LinkedList[LEVELS];



    public CallHandler(){...}




    // *** check every level employee to see who is free now
    // @Return: Employee - the one it is free
    Employee getCallHandler(Call call)
    {
         //for (int i = 0 ; employeeLevels[call.rank].size())
         for (int level = call.rank; level < LEVELS -1; level++) 
         {
              ArrayList employeeLevel = employeeLevels[level];
              for (Employee emp: employeeLevel)
              {
                  if (emp.free)
                  {
                     return emp;
                  }
              }
         }
         return null;
    }
    



    // ** route the call to an available employee, or adds to a queue
    void dispatchCall(Call call)
    {
          // try to route the call to an employee with minimal rank
          Employee emp = getCallHandler(call);
          // Someone is free
          if (emp != null)
               emp.ReceiveCall(call);
          // No one is free
          else
              // place the call into queue according to its rank
              callQueues[call.rank].add(call);
    } 



    void getNextCall(Employee e){...}// look for call for e's rank, put this as parameter



}

class Call
{
    int rank = 0;
    public void reply(String message){...}
    public void disconnect(){...}
}
class Employee
{
    CallHandler callHandler;


    // Fields
    int rank;// 0 - fresher, 1- technical lead, 2 - product manager
    boolean free; // availability


    // Constructor 
    Employee(int rank) {this.rank = rank;}


    // Function
    void ReceiveCall(Call call){...}
    void CallHandled(Call call){...}// call is complete
    void CannotHandle(Call call)// escalate call
    {
         call.rank = rank + 1;
         CallHandler.dispatchCall(call);
         free =  = true;
         CallHandler.getNextCall(this);// look for waiting call
    }
}
class Fresher extends Employee
{
     public Fresher(){super(0);}
}
class TechLead extends Employee
{
     public TechLead() {super(1);}
}
class ProductManager extends Employee
{
     public ProductManager(){super(2);}
}


3. Similar Ones

No comments:

Post a Comment