Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/20/09 13:56:55 (15 years ago)
Author:
kgrading
Message:

splitted the sendfinishedjob / snapshot method in two seperate methods, fixed the locking, added real memory management (#529)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Hive.Client.Core/Core.cs

    r1371 r1379  
    5050  /// The core component of the Hive Client
    5151  /// </summary>
    52   public class Core: MarshalByRefObject {
    53     //Todo: Ask wagner
    54     public static Object Locker { get; set; }   
     52  public class Core: MarshalByRefObject {       
    5553    public static bool abortRequested { get; set; }
    5654       
     
    6563    /// Main Method for the client
    6664    /// </summary>
    67     public void Start() {
    68       Core.Locker = new Object();
     65    public void Start() {     
    6966      abortRequested = false;
    7067
     
    7976      wcfService = WcfService.Instance;
    8077      wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
    81       wcfService.SendJobCompleted += new EventHandler<SendJobCompletedEventArgs>(wcfService_PullJobCompleted);
    82       wcfService.ProcessJobResultCompleted += new EventHandler<ProcessJobResultCompletedEventArgs>(wcfService_SendJobResultCompleted);
     78      wcfService.SendJobCompleted += new EventHandler<SendJobCompletedEventArgs>(wcfService_SendJobCompleted);
     79      wcfService.StoreFinishedJobResultCompleted += new EventHandler<StoreFinishedJobResultCompletedEventArgs>(wcfService_StoreFinishedJobResultCompleted);
     80      wcfService.ProcessSnapshotCompleted += new EventHandler<ProcessSnapshotCompletedEventArgs>(wcfService_ProcessSnapshotCompleted);
    8381      wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
    8482      wcfService.ServerChanged += new EventHandler(wcfService_ServerChanged);
     
    105103        DetermineAction(container);
    106104      }
    107     }
     105    }   
    108106
    109107    /// <summary>
     
    127125        //Snapshot is ready and can be sent back to the Server
    128126        case MessageContainer.MessageType.SnapshotReady:
    129           ThreadPool.QueueUserWorkItem(new WaitCallback(GetSnapshot), container.JobId);
    130           //Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
    131           //ssr.Start(container.JobId);         
     127          ThreadPool.QueueUserWorkItem(new WaitCallback(GetSnapshot), container.JobId);         
    132128          break;
    133129        //Pull a Job from the Server
     
    137133        //A Job has finished and can be sent back to the server
    138134        case MessageContainer.MessageType.FinishedJob:
    139           ThreadPool.QueueUserWorkItem(new WaitCallback(GetFinishedJob), container.JobId);
    140           //Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
    141           //finThread.Start(container.JobId);         
     135          ThreadPool.QueueUserWorkItem(new WaitCallback(GetFinishedJob), container.JobId);         
    142136          break;     
    143137        //Hard shutdown of the client
     
    158152
    159153        if (WcfService.Instance.ConnState == NetworkEnum.WcfConnState.Loggedin) {
    160           wcfService.ProcessJobResultAsync(ConfigManager.Instance.GetClientInfo().ClientId,
     154          wcfService.StoreFinishedJobResultAsync(ConfigManager.Instance.GetClientInfo().ClientId,
    161155            jId,
    162156            sJob,
     
    166160        } else {         
    167161          JobStorageManager.PersistObjectToDisc(wcfService.ServerIP, wcfService.ServerPort, jId, sJob);
    168           lock (Core.Locker) {
     162          lock (engines) {
    169163            AppDomain.Unload(appDomains[jId]);
    170164            appDomains.Remove(jId);
     
    182176      long jId = (long)jobId;
    183177      byte[] obj = engines[jId].GetSnapshot();
    184       wcfService.ProcessJobResultAsync(ConfigManager.Instance.GetClientInfo().ClientId,
     178      wcfService.ProcessSnapshotAsync(ConfigManager.Instance.GetClientInfo().ClientId,
    185179        jId,
    186180        obj,
     
    202196    }   
    203197
    204     void wcfService_PullJobCompleted(object sender, SendJobCompletedEventArgs e) {
     198    void wcfService_SendJobCompleted(object sender, SendJobCompletedEventArgs e) {
    205199      if (e.Result.StatusMessage != ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT) {
    206200        bool sandboxed = true;
     
    209203        AppDomain appDomain = PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.Job.Id.ToString(), sandboxed, typeof(TestJob));
    210204        appDomain.UnhandledException += new UnhandledExceptionEventHandler(appDomain_UnhandledException);
    211         lock (Locker) {                   
     205        lock (engines) {                   
    212206          if (!jobs.ContainsKey(e.Result.Job.Id)) {
    213207            jobs.Add(e.Result.Job.Id, e.Result.Job);
     
    228222    }
    229223   
    230     //Todo: Seperate this method into 2: Finished jobs and Snapshots
    231     void wcfService_SendJobResultCompleted(object sender, ProcessJobResultCompletedEventArgs e) {
     224
     225    void wcfService_StoreFinishedJobResultCompleted(object sender, StoreFinishedJobResultCompletedEventArgs e) {
    232226      if (e.Result.Success) {       
    233         lock (Locker) {
     227        lock(engines) {       
    234228          //if the engine is running again -> we sent an snapshot. Otherwise the job was finished
    235229          //this method has a risk concerning race conditions.
    236230          //better expand the sendjobresultcompltedeventargs with a boolean "snapshot?" flag
    237           if (e.Result.finished == false) {
    238             Logging.Instance.Info(this.ToString(), "Snapshot for Job " + e.Result.JobId + " transmitted");
    239           } else {
    240             AppDomain.Unload(appDomains[e.Result.JobId]);
    241             appDomains.Remove(e.Result.JobId);
    242             engines.Remove(e.Result.JobId);
    243             jobs.Remove(e.Result.JobId);
    244             ClientStatusInfo.JobsProcessed++;
    245             Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
    246           }
     231          AppDomain.Unload(appDomains[e.Result.JobId]);
     232          appDomains.Remove(e.Result.JobId);
     233          engines.Remove(e.Result.JobId);
     234          jobs.Remove(e.Result.JobId);
     235          ClientStatusInfo.JobsProcessed++;
     236          Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
    247237        }       
    248238      } else {       
     
    251241    }
    252242
     243    void wcfService_ProcessSnapshotCompleted(object sender, ProcessSnapshotCompletedEventArgs e) {
     244      Logging.Instance.Info(this.ToString(), "Snapshot " + e.Result.JobId + " has been transmitted according to plan");
     245    }
     246
    253247    //Todo: First stop all threads, then terminate
    254248    void wcfService_ServerChanged(object sender, EventArgs e) {
    255249      Logging.Instance.Info(this.ToString(), "ServerChanged has been called");
    256       lock (Locker) {
     250      lock (engines) {
    257251        foreach (KeyValuePair<long, AppDomain> entries in appDomains)
    258252          AppDomain.Unload(appDomains[entries.Key]);
Note: See TracChangeset for help on using the changeset viewer.