Free cookie consent management tool by TermsFeed Policy Generator

Changeset 251


Ignore:
Timestamp:
05/13/08 23:56:26 (16 years ago)
Author:
gkronber
Message:

bug-fixes in grid-client (#149)
also cleaned up code in the grid-client

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Grid/ClientForm.cs

    r246 r251  
    4646    private object connectionLock = new object();
    4747    private bool stopped;
     48    private const int CONNECTION_RETRY_TIMEOUT_SEC = 10;
     49    private const int MAX_RETRIES = 10;
    4850
    4951    public ClientForm() {
     
    101103
    102104    private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    103       byte[] engineXml;
     105      byte[] engineXml = null;
     106      // first stop the timer!
    104107      fetchOperationTimer.Stop();
    105       if(stopped) return;
    106       try {
    107         bool success;
     108      bool gotEngine = false;
     109      lock(connectionLock) {
     110        if(stopped) return;
     111        try {
     112          gotEngine = engineStore.TryTakeEngine(out currentGuid, out engineXml);
     113        } catch(TimeoutException timeoutException) {
     114          currentEngine = null;
     115          currentGuid = Guid.Empty;
     116          // timeout -> just start the timer again
     117          fetchOperationTimer.Interval = 5000;
     118          fetchOperationTimer.Start();
     119        } catch(CommunicationException communicationException) {
     120          // connection problem -> reset connection and start the timer again
     121          ResetConnection();
     122          currentEngine = null;
     123          currentGuid = Guid.Empty;
     124          fetchOperationTimer.Interval = 5000;
     125          fetchOperationTimer.Start();
     126        }
     127      }
     128      // got engine from server and user didn't press stop -> execute the engine
     129      if(gotEngine && !stopped) {
     130        currentEngine = RestoreEngine(engineXml);
     131        if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
     132        currentEngine.Finished += new EventHandler(currentEngine_Finished); // register event-handler that sends result back to server and restarts timer
     133        currentEngine.Execute();
     134      } else {
     135        // ok we didn't get engine -> if the user didn't press stop this means that the server doesn't have engines for us
     136        // if the user pressed stop we must not start the timer
     137        if(!stopped) {
     138          if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Waiting for engine"; }); } else statusTextBox.Text = "Waiting for engine";
     139          // start the timer again
     140          fetchOperationTimer.Interval = 5000;
     141          fetchOperationTimer.Start();
     142        }
     143      }
     144    }
     145
     146    void currentEngine_Finished(object sender, EventArgs e) {
     147      IEngine engine = (IEngine)sender;
     148      byte[] resultXml = SaveEngine(engine);
     149      bool success = false;
     150      int retries = 0;
     151      do {
    108152        lock(connectionLock) {
    109           if(factory.State != CommunicationState.Opened) {
    110             ResetConnection();
    111           }
    112           success = engineStore.TryTakeEngine(out currentGuid, out engineXml);
    113         }
    114         if(success && !stopped) {
    115           currentEngine = RestoreEngine(engineXml);
    116           if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
    117           currentEngine.Finished += delegate(object src, EventArgs args) {
    118             if(factory.State == CommunicationState.Opened && !currentEngine.Canceled) {
    119               byte[] resultXml = SaveEngine(currentEngine);
    120               lock(connectionLock) {
    121                 engineStore.StoreResult(currentGuid, resultXml);
    122               }
    123               currentGuid = Guid.Empty;
    124               currentEngine = null;
    125               fetchOperationTimer.Interval = 100;
    126               fetchOperationTimer.Start();
     153          if(!stopped) {
     154            try {
     155              engineStore.StoreResult(currentGuid, resultXml);
     156              success = true;
     157            } catch(TimeoutException timeoutException) {
     158              success = false;
     159              retries++;
     160              Thread.Sleep(TimeSpan.FromSeconds(CONNECTION_RETRY_TIMEOUT_SEC));
     161            } catch(CommunicationException communicationException) {
     162              ResetConnection();
     163              success = false;
     164              retries++;
     165              Thread.Sleep(TimeSpan.FromSeconds(CONNECTION_RETRY_TIMEOUT_SEC));
    127166            }
    128           };
    129           currentEngine.Execute();
    130         } else {
    131           if(!stopped) {
    132             if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Waiting for engine"; }); } else statusTextBox.Text = "Waiting for engine";
    133             fetchOperationTimer.Interval = 5000;
    134             fetchOperationTimer.Start();
    135167          }
    136168        }
    137       } catch(TimeoutException timeoutException) {
    138         currentEngine = null;
    139         currentGuid = Guid.Empty;
    140         fetchOperationTimer.Interval = 5000;
    141         fetchOperationTimer.Start();
    142       } catch(CommunicationException communicationException) {
    143         currentEngine = null;
    144         currentGuid = Guid.Empty;
    145         fetchOperationTimer.Interval = 5000;
    146         fetchOperationTimer.Start();
    147       }
     169      } while(!success && retries < MAX_RETRIES);
     170      // ok if we could store the result it's probable that the server can send us another engine use a small time-interval
     171      if(success)
     172        fetchOperationTimer.Interval = 100;
     173      else fetchOperationTimer.Interval = CONNECTION_RETRY_TIMEOUT_SEC; // if there were problems -> sleep for a longer time
     174      // clear state
     175      currentEngine = null;
     176      currentGuid = Guid.Empty;
     177      // start the timer
     178      fetchOperationTimer.Start();
    148179    }
     180
    149181    private ProcessingEngine RestoreEngine(byte[] engine) {
    150182      GZipStream stream = new GZipStream(new MemoryStream(engine), CompressionMode.Decompress);
Note: See TracChangeset for help on using the changeset viewer.