Exemplo n.º 1
0
        public void runThread()
        {
            try
            {
                logwrite.write("FinesseReceiver runThread", " Finesse Recv Thread Start !!");

                writeStream.ReadTimeout = Timeout.Infinite;

                Event evt = null;

                if (writeStream == null)
                {
                    logwrite.write("FinesseReceiver runThread", "writeStream null");
                }

                int    BUFFERSIZE = sock.ReceiveBufferSize;
                byte[] buffer     = new byte[BUFFERSIZE];
                int    bytelen    = 0;

                StringBuilder sb = new StringBuilder();



                while ((bytelen = writeStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    string message = Encoding.UTF8.GetString(buffer, 0, bytelen);
                    message = message.Replace("&lt;", "<");
                    message = message.Replace("&gt;", ">");
                    message = message.Replace("\n", "");
                    message = message.Trim();

                    //Console.WriteLine(message);
                    logwrite.write("FinesseReceiver runThread", message.Replace("\n", ""));

                    int endIndex  = 0;
                    int subLength = 0;

                    while (message.Length > 0)
                    {
                        //Console.WriteLine("message Len : " + message.Length);
                        if (message.StartsWith("<message"))
                        {
                            endIndex = message.IndexOf("</message>");
                            if (endIndex > -1)
                            {
                                subLength = endIndex + "</message>".Length;
                                string resultStr = message.Substring(0, subLength);

                                evt = xmlParser.parseXML(resultStr);
                                finesseObj.raiseEvent(evt);
                                //Console.WriteLine("\n\n1. result -> " + resultStr);
                                message = message.Substring(subLength, message.Length - subLength);
                            }
                            else
                            {
                                sb.Append(message);
                                break;
                            }
                        }
                        else
                        {
                            endIndex = message.IndexOf("</message>");
                            if (endIndex > -1)
                            {
                                subLength = endIndex + "</message>".Length;
                                string resultStr = message.Substring(0, subLength);

                                if (sb.ToString().Length > 0)
                                {
                                    sb.Append(resultStr);
                                    resultStr = sb.ToString().Replace("&gt;", ">").Replace("&lt;", "<");
                                    evt       = xmlParser.parseXML(resultStr);
                                    finesseObj.raiseEvent(evt);
                                    //Console.WriteLine("\n\n2. result -> " + sb.ToString());
                                    sb = new StringBuilder();
                                }
                                message = message.Substring(subLength, message.Length - subLength);
                            }
                            else
                            {
                                if (sb.ToString().Length > 0)
                                {
                                    sb.Append(message);
                                    break;
                                }
                                else
                                {
                                    sb = new StringBuilder();
                                    break;
                                }
                            }
                        }
                    }
                }



                /*
                 * while ((bytelen = writeStream.Read(buffer, 0, buffer.Length)) > 0)
                 * {
                 *
                 *  string message = Encoding.UTF8.GetString(buffer, 0, bytelen);
                 *  message = message.Replace("&lt;", "<");
                 *  message = message.Replace("&gt;", ">");
                 *
                 *  message = message.Replace("\n", "");
                 *
                 *  logwrite.write("FinesseReceiver runThread", message.Replace("\n", ""));
                 *
                 *  int startIndex = message.IndexOf("<message");
                 *  int endIndex = message.IndexOf("</message>");
                 *
                 *  int endTaglen = "</message>".Length;
                 *  int messagelen = message.Length;
                 *
                 *  string tempStr = "";
                 *
                 *  if (startIndex > -1)
                 *  {
                 *      if (endIndex > -1)
                 *      {
                 *          sb.Append(message.Substring(startIndex, endIndex + endTaglen - startIndex));
                 *          evt = xmlParser.parseXML(sb.ToString());
                 *          finesseObj.raiseEvent(evt);
                 *          //Console.WriteLine("result -> " + sb.ToString());
                 *          sb = new StringBuilder();
                 *          if (message.Length > endIndex + endTaglen)
                 *          {
                 *              // 완료 XML 뒤에 데이터가 있는경우
                 *              int start = endIndex + endTaglen;
                 *              tempStr = message.Substring(start, messagelen - start);
                 *
                 *              startIndex = tempStr.IndexOf("<message");
                 *              endIndex = tempStr.IndexOf("</message>");
                 *
                 *              if (startIndex > -1)
                 *              {
                 *                  if (endIndex > -1)
                 *                  {
                 *                      // 완료 XML 이 뒤에 또 붙은 경우
                 *                      start = endIndex + endTaglen;
                 *                      tempStr = tempStr.Substring(startIndex, start - startIndex);
                 *                      sb.Append(tempStr);
                 *                      evt = xmlParser.parseXML(sb.ToString());
                 *                      finesseObj.raiseEvent(evt);
                 *                      //Console.WriteLine("result -> " + sb.ToString());
                 *                      sb = new StringBuilder();
                 *                  }
                 *                  else
                 *                  {
                 *                      // 완료 XML 이 아닌경우
                 *                      tempStr = tempStr.Substring(startIndex, tempStr.Length);
                 *                      sb.Append(tempStr);
                 *                  }
                 *              }
                 *
                 *          }
                 *
                 *      }
                 *      else
                 *      {
                 *          // 마지막 <message>이 또 붙어서 XML 이 끝이 나지 않은 경우
                 *          tempStr = message.Substring(startIndex, messagelen - startIndex);
                 *          sb = new StringBuilder();
                 *          sb.Append(tempStr);
                 *      }
                 *  }
                 *  else if (endIndex > -1)
                 *  {
                 *      tempStr = message.Substring(0, endIndex + endTaglen);
                 *      sb.Append(tempStr);
                 *      evt = xmlParser.parseXML(sb.ToString());
                 *      finesseObj.raiseEvent(evt);
                 *      //Console.WriteLine("result -> " + sb.ToString());
                 *      sb = new StringBuilder();
                 *  }
                 *
                 * }
                 */
            }
            catch (Exception e)
            {
                if (reader != null)
                {
                    reader.Close();
                    reader = null;
                }

                if (writeStream != null)
                {
                    writeStream.Close();
                    writeStream = null;
                }
                logwrite.write("FinesseReceiver runThread", e.ToString());
            }
            finally
            {
                finesseClient.sessionClose();
                finesseClient.setXMPPAuth(false);   // Finesse 세션이 끊어지면 다른 서버로 재접속 할떄 XMPP 인증이 필요하기 때문에 Flag 세팅

                // 사용자가 Disconnect 를 요청하지 않고 세션이 끊어진 경우 재접속 시도
                if (!finesseClient.getDisconnectReq())
                {
                    logwrite.write("FinesseReceiver runThread", "########## Finesse Session Closed !! ##########");

                    Event evt = new ErrorEvent();
                    evt.setEvtCode(EVENT_TYPE.ON_DISCONNECTION);
                    evt.setEvtMsg("Finesse Session Disconnected");
                    evt.setCurFinesseIP(finesseClient.getCurrentServerIP());
                    finesseObj.raiseEvent(evt);

                    if (finesseClient.finesseReConnect() == ERRORCODE.SUCCESS)
                    {
                        logwrite.write("FinesseReceiver runThread", " TRY TO CHECK AGENT PREVIOUS STATE");
                        // XMPP 인증 성공하면 이전 상담원 상태를 가져온다.
                        finesseClient.checkAgentState();
                    }
                    else
                    {
                        // 서버 세션이 끊어지고, 재접속이 안될시 서버 프로세스가 올라올때까지 감지하는 스레드 시작한다.
                        ISocketSender finesseSender = new FinesseSender(logwrite, finesseClient);
                        ThreadStart   ts            = new ThreadStart(finesseSender.runThread);
                        Thread        thread        = new Thread(ts);
                        thread.Start();
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void raiseCallEvent(CallEvent evt)
        {
            string evtCode    = evt.getEvtCode();
            string evtMessage = evt.getEvtMsg();

            callDataTable = evt.getCallVariable();

            evtMessage = evtMessage.Replace("\n", "");

            StringBuilder callState = new StringBuilder();

            foreach (DictionaryEntry item in evt.getCallStateTable())
            {
                callState.Append(item.Key).Append("^").Append(item.Value).Append("|");
            }
            if (callState.ToString().EndsWith("|"))
            {
                string tempStr = callState.ToString().Substring(0, callState.ToString().Length - 1);
                callState = new StringBuilder();
                callState.Append(tempStr);
            }

            StringBuilder callAction = new StringBuilder();

            foreach (string str in evt.getActionList())
            {
                callAction.Append(str).Append("^");
            }
            if (callAction.ToString().EndsWith("^"))
            {
                string tempStr = callAction.ToString().Substring(0, callAction.ToString().Length - 1);
                callAction = new StringBuilder();
                callAction.Append(tempStr);
            }

            switch (evtCode)
            {
            case EVENT_TYPE.ON_CONNECTION:
                logwrite.write("raiseEvent", ":::::::::::::::::::::::::::::::::::: GetEventOnConnection ::::::::::::::::::::::::::::::::::::");
                logwrite.write("raiseEvent", evtMessage);
                logwrite.write("raiseEvent", "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
                GetEventOnConnection(evt.getCurFinesseIP(), evt.getCurAemsIP(), evt.getCurIspsIP(), evtMessage);
                break;

            case EVENT_TYPE.ON_DISCONNECTION:
                logwrite.write("raiseEvent", ":::::::::::::::::::::::::::::::::::: GetEventOnDisConnection ::::::::::::::::::::::::::::::::::::");
                logwrite.write("raiseEvent", evtMessage);
                logwrite.write("raiseEvent", "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
                GetEventOnDisConnection(evt.getCurFinesseIP(), evt.getCurAemsIP(), evt.getCurIspsIP(), evtMessage);
                break;

            case EVENT_TYPE.ALERTING:
                writeCallEventLog("GetEventOnCallAlerting", evt);
                setActiveDialogID(evt);
                GetEventOnCallAlerting(evt.getDialogID(), evt.getCallType(), evt.getFromAddress(), evt.getToAddress(), callState.ToString(), callAction.ToString());

                if (agentCurrentState.Equals(AGENTSTATE.NOT_READY))
                {
                    // 상담원 현재 상태가 NOTREADY 인데 Alerting 이벤트가 발생했다는건, 내선인입콜 케이스
                    isInternalCall = true;
                }
                else
                {
                    isInternalCall = false;
                }
                break;

            case EVENT_TYPE.FAILED:
                writeCallEventLog("GetEventOnCallFailed", evt);
                setActiveDialogID(evt);
                GetEventOnCallFailed(evt.getDialogID(), evt.getCallType(), evt.getFromAddress(), evt.getToAddress(), callState.ToString(), callAction.ToString());
                break;

            case EVENT_TYPE.ACTIVE:
                writeCallEventLog("GetEventOnCallActive", evt);
                setActiveDialogID(evt);
                GetEventOnCallActive(evt.getDialogID(), evt.getCallType(), evt.getFromAddress(), evt.getToAddress(), callState.ToString(), callAction.ToString());

                if (evt.getToAddress() != null && evt.getToAddress().Equals(phonePadNum))
                {
                    // 폰패드 컨퍼런스
                    logwrite.write("raiseEvent", "PhonePad Conference Start");
                    phonePadCallID = dialogID;      // phonePad 콜 구분을 위한 DialogID 세팅
                    fnConference();
                }
                break;

            case EVENT_TYPE.HELD:
                writeCallEventLog("GetEventOnCallHeld", evt);
                //setActiveDialogID(evt);
                GetEventOnCallHeld(evt.getDialogID(), evt.getCallType(), evt.getFromAddress(), evt.getToAddress(), callState.ToString(), callAction.ToString());
                break;

            case EVENT_TYPE.INITIATING:
                writeCallEventLog("GetEventOnCallInitiating", evt);
                setActiveDialogID(evt);
                GetEventOnCallInitiating(evt.getDialogID(), evt.getCallType(), evt.getFromAddress(), evt.getToAddress(), callState.ToString(), callAction.ToString());
                break;

            case EVENT_TYPE.INITIATED:
                writeCallEventLog("GetEventOnCallInitiated", evt);
                setActiveDialogID(evt);
                GetEventOnCallInitiated(evt.getDialogID(), evt.getCallType(), evt.getFromAddress(), evt.getToAddress(), callState.ToString(), callAction.ToString());
                break;


            case EVENT_TYPE.WRAP_UP:
                writeCallEventLog("GetEventOnCallWrapUp", evt);
                // checkTable(callEvent.getCallVariable());

                removeDialogID(evt);
                GetEventOnCallWrapUp(evt.getDialogID(), evt.getCallType(), evt.getFromAddress(), evt.getToAddress(), callState.ToString(), callAction.ToString());
                break;

            case EVENT_TYPE.DROPPED:
                writeCallEventLog("GetEventOnCallDropped", evt);
                // checkTable(callEvent.getCallVariable());
                removeDialogID(evt);

                GetEventOnCallDropped(evt.getDialogID(), evt.getCallType(), evt.getFromAddress(), evt.getToAddress(), callState.ToString(), callAction.ToString());
                if (evt.getCallType().Equals(CALL.CONFERENCE) && evt.getDialogID().Equals(phonePadCallID))
                {
                    // 폰패드 이후 Dropped 이벤트일 경우 폰패드 결과를 요청한다.
                    getPhonePadInfo();
                }
                if (isInternalCall)
                {
                    // NOTREADY 상태에서 내선콜이 인입되었다가 Alerting 도중에 콜이 종료되면 상담원 이전상태 이벤트를 Finesse  가 주지 않아
                    // 임의적으로 상담원상태 요청 이후 상태 이벤트를 발생시킨다.
                    AgentStateVO agentStateVO = FinesseClient.checkAgentState();      // 이전 상담원 상태체크
                    AgentEvent   evt_         = new AgentEvent();
                    evt_.setEvtMsg(agentStateVO.getXmppMsg());
                    evt_.setAgentState(agentStateVO.getState());
                    evt_.setReasonCode(agentStateVO.getReasonCode());
                    evt_.setEvtCode(EVENT_TYPE.ON_AGENTSTATE_CHANGE);
                    raiseEvent(evt_);
                }
                break;

            default:
                logwrite.write("raiseEvent", ":::::::::::::::::::::::::::::::::::: UNKWON EVENT ::::::::::::::::::::::::::::::::::::");
                logwrite.write("raiseEvent", evtMessage);
                logwrite.write("raiseEvent", "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
                setActiveDialogID(evt);
                break;
            }
        }