void InterprocessServer::DoRun()
{
	shared_ptr<InterprocessIo> io = Connect();
	if(!io) {
		return;
	}

	if(IsCancelPending()) {
		return;
	}

	OnConnect(io);
}
/** Complete a state transition.
This is called by the base class when a state transition has
concluded and it provides an opportunity for the state machine 
to perform actions required immediately after this transition.

The method can also initiate a further change of state. This is
relevant when the state machine is required to perform an autonomous
transition from one state to another e.g. this occurs when several
interactions are required arising from a single external trigger.
*/
void CSuplMolrFsmSession::PostTransition()
	{
	SUPLLOG(ELogP1, "CSuplMolrFsmSession::PostTransition() Begin\n");
	// Some states are transitory i.e. they require
	// an automatic transition to the next state
	if (IsCancelPending() ||
	    ((EStateResponseReceived == iCurrentState) && (ESuplMolrCellBased == MachineType())) || 
		EStatePositionReceived		== iCurrentState ||
	 	EStateSuplSessionEnded		== iCurrentState ||
	 	EStatePosSessionEnded		== iCurrentState ||
	 	EStateLbsSessionEnded		== iCurrentState ||
	 	EStateNetConnectionClosed	== iCurrentState)
		{
		// Perform a state transition
		PerformTransition();
		}
	SUPLLOG(ELogP1, "CSuplMolrFsmSession::PostTransition() End\n");
	}
void WorkerManager::RegisterTask(const shared_ptr<Task>& task)
{
	AutoLocker lock(m_critSec);

	while(true) {
		if(IsCancelPending()) {
			return;
		}

		shared_ptr<WorkerThread> worker = GetWorker();
		if(!worker->AssignTask(task)) {
			continue;
		}

		RegisterActiveWorker(worker);
		return;
	}
}
/** State transition.
This method determines the next state to be adopted by the state machine.
@return TBool ETrue if the state has changed.
@param aForceRedo, indicates that the states entry action must be re-performed when there has been no change in state
*/
TBool CSuplMolrFsmSession::SelectNextState(TBool& aForceRedo)
	{
	SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Begin\n");
	TBool stateChanged = ETrue;
	aForceRedo = EFalse;
	
	// Regardless of current state, check first if an event has occurred
	// that implies cancelling the machine (terminate session)
	if (IsCancelPending())
		{
		SetMachineAsCancelling();
		iCurrentState = (IsSessionConnected() && iSessionInProgress)? EStateSuplSessionEnded : EStatePosSessionEnded;
		}
	else
		{
		// Set new state	
		switch (iCurrentState)
			{
			
		case EStateProcedureNull:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStateProcedureNull\n");
			iCurrentState = EStateNetConnectionStarted;
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Nex state: EStateNetConnectionStarted\n");
			break;
			
		case EStateNetConnectionStarted:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStateNetConnectionStarted\n");
			iCurrentState = EStateStartSent;
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Nex state: EStateStartSent\n");
			break;
			
		case EStateStartSent:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStateStartSent\n");
			stateChanged = DetermineStateFromStartSent();
			break;
			
		case EStateResponseReceived:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStateResponseReceived\n");
		    iCurrentState = EStatePosInitSent;
		    SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Nex state: EStatePosInitSent\n");
			break;
			
		case EStatePosInitSent:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStatePosInitSent\n");
			stateChanged = DetermineStateFromPosInitSent();
			break;

		case EStatePositioningInProgress:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStatePositioningInProgress\n");
			stateChanged = DetermineStateFromPositioningInProgress(aForceRedo);
			break;

		case EStatePositionReceived:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStatePositionReceived\n");
			iCurrentState = EStatePosSessionEnded;
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Nex state: EStatePosSessionEnded\n");
			SetMachineAsNotCancellable();
			break;
			
		case EStateSuplSessionEnded:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStateSuplSessionEnded\n");
			iCurrentState = EStatePosSessionEnded;
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Nex state: EStatePosSessionEnded\n");
			break;

		case EStatePosSessionEnded:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStatePosSessionEnded\n");
			iCurrentState = EStateLbsSessionEnded;
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Nex state: EStateLbsSessionEnded\n");
			break;
			
		case EStateLbsSessionEnded:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStateLbsSessionEnded\n");
			iCurrentState = EStateNetConnectionClosed;
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Nex state: EStateNetConnectionClosed\n");
			break;
			
		case EStateNetConnectionClosed:
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() entry state EStateNetConnectionClosed\n");
			// Procedure has completed
			iCurrentState = EStateProcedureNull;
			SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() Nex state: EStateProcedureNull\n");
			break;
			
		default:
			SUPLLOG(ELogP3, "CSuplMolrFsmSession::SelectNextState() unknown entry state\n");
			ASSERT(EFalse);
			break;
			};
		}
	SUPLLOG(ELogP1, "CSuplMolrFsmSession::SelectNextState() End\n");
	return stateChanged;
	}
void InterprocessServer::Run()
{
	while(!IsCancelPending()) {
		DoRun();
	}
}