Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/MessageQueue.cs @ 5599

Last change on this file since 5599 was 5599, checked in by ascheibe, 13 years ago

#1233

  • rename 'Slave' namespace to 'SlaveCore' (and assemblies etc) to avoid problems with 'Slave' class
  • use svcutil (OKB-style)
File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Threading;
25
26namespace HeuristicLab.Clients.Hive.SlaveCore {
27
28  /// <summary>
29  /// Queue for the communication between threads and AppDomains. The Queue is threadsafe and uses a semaphore
30  /// </summary>
31  public class MessageQueue : MarshalByRefObject {
32
33    private static MessageQueue instance = null;
34
35    private Queue<MessageContainer> queue = null;
36    private Semaphore semaphore = null;
37
38    /// <summary>
39    /// Returns the Instance of the MessageQueue. If the instance is null, it will be created.
40    /// This is a Implementation of the Singleton pattern
41    /// </summary>
42    /// <returns>the MessageQueue Instance</returns>
43    public static MessageQueue GetInstance() {
44      if (instance == null) {
45        instance = new MessageQueue();
46      }
47      return instance;
48    }
49
50    /// <summary>
51    /// Sets Lease time to infinite
52    /// </summary>
53    /// <returns>nothing</returns>
54    public override object InitializeLifetimeService() {
55      return null;
56    }
57
58    /// <summary>
59    /// Creates a new MessageQueue Object.
60    /// A new Queue and a Semaphore is created. The Semaphore is set to a max size of 5000.
61    /// </summary>
62    private MessageQueue() {
63      queue = new Queue<MessageContainer>();
64      semaphore = new Semaphore(0, 5000);
65    }
66
67    /// <summary>
68    /// Returns the oldest MessageContainer Object from the Queue.
69    /// </summary>
70    /// <returns>the oldest MessageContainer Object</returns>
71    public MessageContainer GetMessage() {
72      semaphore.WaitOne();
73      lock (this) {
74        if (queue.Count > 0) {
75          return queue.Dequeue();
76        }
77      }
78      return null;
79    }
80
81    /// <summary>
82    /// Adds a MessageContainer Object to the Queue
83    /// </summary>
84    /// <param name="messageContainer">the MessageContainer</param>
85    public void AddMessage(MessageContainer messageContainer) {
86      lock (this) {
87        queue.Enqueue(messageContainer);
88        semaphore.Release();
89      }
90    }
91
92    /// <summary>
93    /// Adds a message to the Queue. The MessageContainer Object is built in the Method
94    /// </summary>
95    /// <param name="message">the Message</param>
96    public void AddMessage(MessageContainer.MessageType message) {
97      lock (this) {
98        queue.Enqueue(new MessageContainer(message));
99        semaphore.Release();
100      }
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.