public ServerStateContext( final ISipManager sipManager, final SipUser serverUser, final UCESipServerSettings sipSettings) { this.sipManager = sipManager; this.serverUser = serverUser; this.sipSettings = sipSettings; this.stateLock = new ReentrantLock(); this.stateChangeCondition = stateLock.newCondition(); this.takeCallLock = new ReentrantLock(); this.takeCallCondition = takeCallLock.newCondition(); this.serverCallMap = new HashMap<String, UCESipServerCall>(); this.registerRefreshThread = new RegisterRefreshThread(this.sipManager, this.serverUser); this.callMapCleanupTimer = new Timer("Server call map cleanup"); this.callMapCleanupTask = new CallMapCleanupTask(); this.callQueue = new ArrayBlockingQueue<>(sipSettings.getCallQueueCapacity()); this.isTaking = false; this.isShutdown = false; this.stateTimeoutTimer = new Timer("Sip user agent server state timeout timer"); this.currentServerState = new InitServerState(this); }
public void register() throws UCESipException, InterruptedException { stateLock.lock(); try { currentServerState.startRegister(); while (true) { if (currentServerState.getCurrentState() == ServerStates.Registered) { break; } if (currentServerState.getCurrentState() == ServerStates.Error) { // check for asynchronous error throw ((ErrorServerState) currentServerState).getError(); } stateChangeCondition.await(); } long tp = sipSettings.getServerCallMapCleanupPeriodMillis(); callMapCleanupTimer.scheduleAtFixedRate(callMapCleanupTask, tp, tp); } catch (InterruptedException e) { throw e; } finally { stateLock.unlock(); } }