From 31f20c7ac0ab5bdcdea75155d9ff5e6236f9cf3d Mon Sep 17 00:00:00 2001 From: CharonInferar Date: Mon, 7 Apr 2014 01:56:46 +0200 Subject: [PATCH] Fix at suspicion of concurrency problem in surveyingservice --- src/services/SurveyService.java | 89 +++++++++++++++++---------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/src/services/SurveyService.java b/src/services/SurveyService.java index 1f14fe8d..333db3f7 100644 --- a/src/services/SurveyService.java +++ b/src/services/SurveyService.java @@ -86,48 +86,47 @@ public class SurveyService implements INetworkDispatch { } public void ServiceProcessing(){ - // All tools sampling - SurveyTool removeTool=null; - for (SurveyTool surveyTool : activeSurveyTools){ - if (surveyTool.getCurrentlySurveying()){ - // Check if survey process has finished - if (System.currentTimeMillis()>surveyTool.getLastSurveyTime()+3000){ - removeTool = surveyTool; - surveyTool.setCurrentlySurveying(false); - surveyTool.sendConstructSurveyMapMessage(); + synchronized(activeSurveyTools){ + // All tools sampling + SurveyTool removeTool=null; + for (SurveyTool surveyTool : activeSurveyTools){ + if (surveyTool.getCurrentlySurveying()){ + // Check if survey process has finished + if (System.currentTimeMillis()>surveyTool.getLastSurveyTime()+3000){ + removeTool = surveyTool; + surveyTool.setCurrentlySurveying(false); + surveyTool.sendConstructSurveyMapMessage(); + } + } + if (surveyTool.getCurrentlySampling()){ + // Check if sampling process has finished + if ((System.currentTimeMillis()>surveyTool.getLastSampleTime()+3000) && !surveyTool.getCurrentlyCoolingDown()){ + surveyTool.setCurrentlyCoolingDown(true); + // Update inventory + handleSamplingStages(surveyTool); + + } + // Check if sampling recovery is over + long sampleRecoveryTime = surveyTool.getRecoveryTime(); + if (System.currentTimeMillis()>surveyTool.getLastSampleTime()+sampleRecoveryTime && ! surveyTool.isExceptionalState()){ + // kick off another sampling attempt + surveyTool.setCurrentlyCoolingDown(false); + continueSampling(surveyTool); + } + if (surveyTool.getUser().getPosture()!=1){ + surveyTool.setExceptionalState(false); + surveyTool.setCurrentlySampling(false); + removeTool = surveyTool; + if (surveyTool.getUser().getPosture()==0) + surveyTool.getUser().sendSystemMessage("You stand up", (byte) 0); + + surveyTool.getUser().sendSystemMessage("@survey:sample_cancel", (byte) 0); + } } } - if (surveyTool.getCurrentlySampling()){ - // Check if sampling process has finished - if ((System.currentTimeMillis()>surveyTool.getLastSampleTime()+3000) && !surveyTool.getCurrentlyCoolingDown()){ - surveyTool.setCurrentlyCoolingDown(true); - // Update inventory - handleSamplingStages(surveyTool); - - } - // Check if sampling recovery is over - long sampleRecoveryTime = surveyTool.getRecoveryTime(); - if (System.currentTimeMillis()>surveyTool.getLastSampleTime()+sampleRecoveryTime && ! surveyTool.isExceptionalState()){ - // kick off another sampling attempt - surveyTool.setCurrentlyCoolingDown(false); - continueSampling(surveyTool); - } - if (surveyTool.getUser().getPosture()!=1){ - surveyTool.setExceptionalState(false); - surveyTool.setCurrentlySampling(false); - removeTool = surveyTool; - if (surveyTool.getUser().getPosture()==0) - surveyTool.getUser().sendSystemMessage("You stand up", (byte) 0); - - surveyTool.getUser().sendSystemMessage("@survey:sample_cancel", (byte) 0); - } - } - } - if (removeTool!=null) - activeSurveyTools.remove(removeTool); // remove after notification - - - + if (removeTool!=null) + activeSurveyTools.remove(removeTool); // remove after notification + } } public void handleSamplingStages(SurveyTool surveyTool){ @@ -567,8 +566,10 @@ public class SurveyService implements INetworkDispatch { } public void addActiveSurveyTool(SurveyTool tool){ - if (! activeSurveyTools.contains(tool)) - activeSurveyTools.add(tool); + synchronized(activeSurveyTools){ + if (! activeSurveyTools.contains(tool)) + activeSurveyTools.add(tool); + } } public Vector getActiveSurveyTools(){ @@ -576,7 +577,9 @@ public class SurveyService implements INetworkDispatch { } public void removeActiveSurveyTool(SurveyTool surveyTool){ - activeSurveyTools.remove(surveyTool); + synchronized(activeSurveyTools){ + activeSurveyTools.remove(surveyTool); + } } public boolean toolIsInList(SurveyTool surveyTool){