Changeset 4343 for branches/3.3-Hive


Ignore:
Timestamp:
08/30/10 15:17:19 (12 years ago)
Author:
cneumuel
Message:

made disposing of cached WCF-Client object safer (by using IDisposable pattern)

Location:
branches/3.3-Hive/sources/HeuristicLab.Hive
Files:
1 added
1 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Calendar/3.3/Properties/AssemblyInfo.cs

    r4342 r4343  
    5858// [assembly: AssemblyVersion("1.0.*")]
    5959[assembly: AssemblyVersion("3.3.0.0")]
    60 [assembly: AssemblyFileVersion("3.3.0.4338")]
     60[assembly: AssemblyFileVersion("3.3.0.4342")]
  • branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment.Views/3.3/MenuItems/ConnectionSetupMenuItem.cs

    r4342 r4343  
    4444      new ConnectionSetupView().Show();
    4545    }
     46    public override int Position {
     47      get { return 1600; }
     48    }
    4649  }
    4750}
  • branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Communication/3.3/HeuristicLab.Hive.Slave.Communication-3.3.csproj

    r4338 r4343  
    122122      <DependentUpon>Settings.settings</DependentUpon>
    123123    </Compile>
    124     <Compile Include="SlaveServicePool.cs" />
     124    <Compile Include="SlaveServiceCache.cs" />
    125125    <Compile Include="WcfService.cs" />
    126126  </ItemGroup>
  • branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Communication/3.3/WcfService.cs

    r4338 r4343  
    7878    public event EventHandler Connected;
    7979
    80     SlaveFacadeServicePool servicePool;
     80    SlaveServiceCache servicePool;
    8181
    8282    /// <summary>
     
    9292    /// </summary>
    9393    public void Connect() {
    94       SlaveService.ISlaveFacade client = null;
    95       try {
    96         Logger.Debug("Starting the Connection Process");
    97         if (String.Empty.Equals(ServerIp)) {
    98           Logger.Info("No Server IP set!");
    99           return;
    100         }
    101         servicePool = new SlaveFacadeServicePool(ServerIp);
    102 
    103         Logger.Debug("Creating the new connection proxy");
    104         client = servicePool.CreateStreamedSlaveFacade();
    105         Logger.Debug("Created the new connection proxy");
    106 
    107         ConnState = NetworkEnum.WcfConnState.Connected;
    108         ConnectedSince = DateTime.Now;
    109 
    110         if (Connected != null) {
    111           Connected(this, new EventArgs());
    112         }
    113         if (ConnState == NetworkEnum.WcfConnState.Failed) {
    114           ConnectionRestored(this, new EventArgs());
    115         }
    116       }
    117       catch (Exception ex) {
    118         HandleNetworkError(ex);
    119       }
    120       finally {
    121         servicePool.DisposeSlaveClient(client);
     94      servicePool = new SlaveServiceCache(ServerIp);
     95      using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     96        try {
     97          Logger.Debug("Starting the Connection Process");
     98          if (String.Empty.Equals(ServerIp)) {
     99            Logger.Info("No Server IP set!");
     100            return;
     101          }
     102          ConnState = NetworkEnum.WcfConnState.Connected;
     103          ConnectedSince = DateTime.Now;
     104
     105          if (Connected != null) {
     106            Connected(this, new EventArgs());
     107          }
     108          if (ConnState == NetworkEnum.WcfConnState.Failed) {
     109            ConnectionRestored(this, new EventArgs());
     110          }
     111        }
     112        catch (Exception ex) {
     113          HandleNetworkError(ex);
     114        }
    122115      }
    123116    }
     
    145138    /// </summary>
    146139    public void Login(SlaveDto slaveInfo) {
    147       SlaveService.ISlaveFacade client = null;
    148       try {
    149         if (ConnState == NetworkEnum.WcfConnState.Connected) {
    150           Logger.Debug("STARTED: Login Sync");
    151           client = servicePool.CreateSlaveFacade();
    152           Response res = client.Login(slaveInfo);
    153           if (res.StatusMessage != ResponseStatus.Ok) {
    154             Logger.Error("FAILED: Login Failed! " + res.StatusMessage);
    155             throw new Exception(res.StatusMessage.ToString());
    156           } else {
    157             Logger.Info("ENDED: Login succeeded" + res.StatusMessage);
    158             LoggedIn = true;
    159           }
    160         }
    161       }
    162       catch (Exception e) {
    163         HandleNetworkError(e);
    164       }
    165       finally {
    166         servicePool.DisposeSlaveClient(client);
     140      using (DisposableSlaveFacadeClient client = servicePool.GetSlaveFacade()) {
     141        try {
     142          if (ConnState == NetworkEnum.WcfConnState.Connected) {
     143            Logger.Debug("STARTED: Login Sync");
     144            Response res = client.Login(slaveInfo);
     145            if (res.StatusMessage != ResponseStatus.Ok) {
     146              Logger.Error("FAILED: Login Failed! " + res.StatusMessage);
     147              throw new Exception(res.StatusMessage.ToString());
     148            } else {
     149              Logger.Info("ENDED: Login succeeded" + res.StatusMessage);
     150              LoggedIn = true;
     151            }
     152          }
     153        }
     154        catch (Exception e) {
     155          HandleNetworkError(e);
     156        }
    167157      }
    168158    }
     
    175165    public void GetJobAsync(Guid guid) {
    176166      if (LoggedIn) {
     167        DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade();
    177168        Logger.Debug("STARTED: Fetching of Jobs from Server for Slave");
    178         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    179         //client.GetStreamedJobAsync(guid);
    180169        client.BeginGetStreamedJob(guid, (ar => {
    181170          if (ar.IsCompleted) {
     
    213202              if (memStream != null)
    214203                memStream.Dispose();
     204
     205              servicePool.ReleaseSlaveClient(client);
    215206            }
    216207          } else
    217208            HandleNetworkError(new FaultException("GetJobAsync did not complete"));
    218 
    219           servicePool.DisposeSlaveClient(client);
    220209        }), null);
    221210      }
     
    231220    public void GetFinishedJobResultAsync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
    232221      if (LoggedIn) {
     222        DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade();
    233223        Logger.Debug("STARTED: Sending back the finished job results");
    234224        Logger.Debug("Building stream");
     
    236226        Logger.Debug("Builded stream");
    237227        Logger.Debug("Making the call");
    238         //proxy.StoreFinishedJobResultStreamedAsync(stream, stream);
    239         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
     228
    240229        client.BeginStoreFinishedJobResultStreamed(stream, (ar => {
    241           Logger.Debug("Finished storing the job");
    242           if (stream != null)
    243             stream.Dispose();
    244 
    245           if (ar.IsCompleted) {
    246             var res = client.EndStoreFinishedJobResultStreamed(ar);
    247             StoreFinishedJobResultCompletedEventArgs args = new StoreFinishedJobResultCompletedEventArgs(new object[] { res }, null, false, null);
    248             Logger.Debug("calling the Finished Job Event");
    249             GetFinishedJobResultCompleted(this, args);
    250             Logger.Debug("ENDED: Sending back the finished job results");
    251           } else {
    252             HandleNetworkError(new FaultException("GetFinishedJobResultAsync did not complete"));
    253           }
    254           servicePool.DisposeSlaveClient(client);
     230          try {
     231            Logger.Debug("Finished storing the job");
     232            if (stream != null)
     233              stream.Dispose();
     234
     235            if (ar.IsCompleted) {
     236              var res = client.EndStoreFinishedJobResultStreamed(ar);
     237              StoreFinishedJobResultCompletedEventArgs args = new StoreFinishedJobResultCompletedEventArgs(new object[] { res }, null, false, null);
     238              Logger.Debug("calling the Finished Job Event");
     239              GetFinishedJobResultCompleted(this, args);
     240              Logger.Debug("ENDED: Sending back the finished job results");
     241            } else {
     242              HandleNetworkError(new FaultException("GetFinishedJobResultAsync did not complete"));
     243            }
     244          }
     245          finally {
     246            servicePool.ReleaseSlaveClient(client);
     247          }
    255248        }), null);
    256249      }
     
    263256    public void ProcessSnapshotAsync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
    264257      if (LoggedIn) {
     258        DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade();
     259
    265260        Stream stream = GetStreamedJobResult(clientId, jobId, result, percentage, exception);
    266         //proxy.ProcessSnapshotStreamedAsync(stream, stream);
    267         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    268261        client.BeginProcessSnapshotStreamed(stream, (ar => {
    269           if (stream != null)
    270             stream.Dispose();
    271 
    272           if (ar.IsCompleted) {
    273             var res = client.EndStoreFinishedJobResultStreamed(ar);
    274             ProcessSnapshotCompletedEventArgs args = new ProcessSnapshotCompletedEventArgs(new object[] { res }, null, false, null);
    275             ProcessSnapshotCompleted(this, args);
     262          try {
     263            if (stream != null)
     264              stream.Dispose();
     265
     266            if (ar.IsCompleted) {
     267              var res = client.EndStoreFinishedJobResultStreamed(ar);
     268              ProcessSnapshotCompletedEventArgs args = new ProcessSnapshotCompletedEventArgs(new object[] { res }, null, false, null);
     269              ProcessSnapshotCompleted(this, args);
     270            } else {
     271              HandleNetworkError(new FaultException("ProcessSnapshotAsync did not complete"));
     272            }
     273          }
     274          finally {
     275            servicePool.ReleaseSlaveClient(client);
     276          }
     277        }), null);
     278      }
     279    }
     280
     281    #endregion
     282
     283    /// <summary>
     284    /// Methods for sending the periodically Heartbeat
     285    /// </summary>
     286    #region Heartbeat
     287
     288    public event System.EventHandler<ProcessHeartBeatCompletedEventArgs> ProcessHeartBeatCompleted;
     289    public void ProcessHeartBeatSync(HeartBeatData hbd) {
     290      if (LoggedIn) {
     291        using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     292          Logger.Debug("STARTING: sending heartbeat");
     293          var res = client.ProcessHeartBeat(hbd);
     294
     295          if (res.StatusMessage == ResponseStatus.Ok) {
     296            ProcessHeartBeatCompleted(this, new ProcessHeartBeatCompletedEventArgs(new object[] { res }, null, false, null));
     297            Logger.Debug("ENDED: sending heartbeats");
    276298          } else {
    277             HandleNetworkError(new FaultException("ProcessSnapshotAsync did not complete"));
    278           }
    279           servicePool.DisposeSlaveClient(client);
    280         }), null);
    281       }
    282     }
    283 
    284     #endregion
    285 
    286     /// <summary>
    287     /// Methods for sending the periodically Heartbeat
    288     /// </summary>
    289     #region Heartbeat
    290 
    291     public event System.EventHandler<ProcessHeartBeatCompletedEventArgs> ProcessHeartBeatCompleted;
    292     public void ProcessHeartBeatAsync(HeartBeatData hbd) {
    293       if (LoggedIn) {
    294         Logger.Debug("STARTING: sending heartbeat");
    295         //proxy.ProcessHeartBeatAsync(hbd);
    296         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    297         client.BeginProcessHeartBeat(hbd, (ar => {
    298           if (ar.IsCompleted) {
    299             var res = client.EndProcessHeartBeat(ar);
    300             if (res.StatusMessage == ResponseStatus.Ok) {
    301               ProcessHeartBeatCompleted(this, new ProcessHeartBeatCompletedEventArgs(new object[] { res }, null, false, null));
    302               Logger.Debug("ENDED: sending heartbeats");
    303             } else {
    304               Logger.Error("FAILED: sending heartbeats: " + res.StatusMessage.ToString());
    305             }
    306           } else {
    307             HandleNetworkError(new FaultException("ProcessHeartBeatAsync did not complete"));
    308           }
    309           servicePool.DisposeSlaveClient(client);
    310         }), null);
     299            Logger.Error("FAILED: sending heartbeats: " + res.StatusMessage.ToString());
     300          }
     301        }
    311302      }
    312303    }
     
    337328
    338329    public ResponseResultReceived StoreFinishedJobResultsSync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
    339       SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    340       ResponseResultReceived res = client.StoreFinishedJobResultStreamed(GetStreamedJobResult(clientId, jobId, result, percentage, exception));
    341       servicePool.DisposeSlaveClient(client);
    342       return res;
     330      using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     331        ResponseResultReceived res = client.StoreFinishedJobResultStreamed(GetStreamedJobResult(clientId, jobId, result, percentage, exception));
     332        return res;
     333      }
    343334    }
    344335
     
    346337      try {
    347338        Logger.Debug("STARTING: Sync call: IsJobStillNeeded");
    348         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    349         Response res = client.IsJobStillNeeded(jobId);
    350         servicePool.DisposeSlaveClient(client);
    351         Logger.Debug("ENDED: Sync call: IsJobStillNeeded");
    352         return res;
     339        using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     340          Response res = client.IsJobStillNeeded(jobId);
     341          Logger.Debug("ENDED: Sync call: IsJobStillNeeded");
     342          return res;
     343        }
    353344      }
    354345      catch (Exception e) {
     
    360351    public ResponseResultReceived ProcessSnapshotSync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception) {
    361352      try {
    362         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    363         var res = client.ProcessSnapshotStreamed(GetStreamedJobResult(clientId, jobId, result, percentage, exception));
    364         servicePool.DisposeSlaveClient(client);
    365         return res;
     353        using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     354          return client.ProcessSnapshotStreamed(GetStreamedJobResult(clientId, jobId, result, percentage, exception));
     355        }
    366356      }
    367357      catch (Exception e) {
     
    373363    public IEnumerable<CachedHivePluginInfoDto> RequestPlugins(List<HivePluginInfoDto> requestedPlugins) {
    374364      try {
    375         Logger.Debug("STARTED: Requesting Plugins for Job");
    376         Logger.Debug("STARTED: Getting the stream");
    377         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    378         Stream stream = client.GetStreamedPlugins(requestedPlugins.ToArray());
    379         servicePool.DisposeSlaveClient(client);
    380         Logger.Debug("ENDED: Getting the stream");
    381         BinaryFormatter formatter = new BinaryFormatter();
    382         Logger.Debug("STARTED: Deserializing the stream");
    383         ResponseList<CachedHivePluginInfoDto> response = (ResponseList<CachedHivePluginInfoDto>)formatter.Deserialize(stream);
    384         Logger.Debug("ENDED: Deserializing the stream");
    385         if (stream != null)
    386           stream.Dispose();
    387         return response.List;
     365        using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     366          Logger.Debug("STARTED: Requesting Plugins for Job");
     367          Logger.Debug("STARTED: Getting the stream");
     368          Stream stream = client.GetStreamedPlugins(requestedPlugins.ToArray());
     369          Logger.Debug("ENDED: Getting the stream");
     370          BinaryFormatter formatter = new BinaryFormatter();
     371          Logger.Debug("STARTED: Deserializing the stream");
     372          ResponseList<CachedHivePluginInfoDto> response = (ResponseList<CachedHivePluginInfoDto>)formatter.Deserialize(stream);
     373          Logger.Debug("ENDED: Deserializing the stream");
     374          if (stream != null)
     375            stream.Dispose();
     376          return response.List;
     377        }
    388378      }
    389379      catch (Exception e) {
     
    396386      try {
    397387        Logger.Debug("STARTED: Logout");
    398         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    399         client.Logout(guid);
    400         servicePool.DisposeSlaveClient(client);
     388        using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     389          client.Logout(guid);
     390        }
    401391        Logger.Debug("ENDED: Logout");
    402392      }
     
    409399      try {
    410400        Logger.Debug("STARTED: Syncing Calendars");
    411         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    412         ResponseCalendar cal = client.GetCalendar(clientId);
    413         servicePool.DisposeSlaveClient(client);
    414         Logger.Debug("ENDED: Syncing Calendars");
    415         return cal;
     401        using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     402          ResponseCalendar cal = client.GetCalendar(clientId);
     403          Logger.Debug("ENDED: Syncing Calendars");
     404          return cal;
     405        }
    416406      }
    417407      catch (Exception e) {
     
    424414      try {
    425415        Logger.Debug("STARTED: Setting Calendar status to: " + state);
    426         SlaveService.ISlaveFacade client = servicePool.CreateStreamedSlaveFacade();
    427         Response resp = client.SetCalendarStatus(clientId, state);
    428         servicePool.DisposeSlaveClient(client);
    429         Logger.Debug("ENDED: Setting Calendar status to: " + state);
    430         return resp;
     416        using (DisposableSlaveFacadeClient client = servicePool.GetStreamedSlaveFacade()) {
     417          Response resp = client.SetCalendarStatus(clientId, state);
     418          Logger.Debug("ENDED: Setting Calendar status to: " + state);
     419          return resp;
     420        }
    431421      }
    432422      catch (Exception e) {
  • branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Core/3.3/HeartbeatManager.cs

    r4342 r4343  
    104104
    105105                Logger.Debug("Sending Heartbeat: " + heartBeatData);
    106                 wcfService.ProcessHeartBeatAsync(heartBeatData);
     106                wcfService.ProcessHeartBeatSync(heartBeatData);               
    107107              }
    108108
Note: See TracChangeset for help on using the changeset viewer.