Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Common/3.2/MessageQueue.cs @ 2291

Last change on this file since 2291 was 1775, checked in by kgrading, 16 years ago

fixed small changes in logging, messagequeues and the jobfetching (#467)

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.Threading;
26using HeuristicLab.Hive.Contracts;
27
28namespace HeuristicLab.Hive.Client.Common {
29
30  /// <summary>
31  /// Queue for the communication between threads and AppDomains. The Queue is threadsafe and uses a semaphore
32  /// </summary>
33  public class MessageQueue : MarshalByRefObject {
34
35    private static MessageQueue instance = null;
36   
37    private Queue<MessageContainer> queue = null;
38    private Semaphore semaphore = null;
39
40    /// <summary>
41    /// Returns the Instance of the MessageQueue. If the instance is null, it will be created.
42    /// This is a Implementation of the Singleton pattern
43    /// </summary>
44    /// <returns>the MessageQueue Instance</returns>
45    public static MessageQueue GetInstance() {
46      if (instance == null) {
47        instance = new MessageQueue();
48      }
49      return instance;
50    }
51
52    /// <summary>
53    /// Sets Lease time to infinite
54    /// </summary>
55    /// <returns>nothing</returns>
56    public override object InitializeLifetimeService() {
57      return null;
58    }
59
60    /// <summary>
61    /// Creates a new MessageQueue Object.
62    /// A new Queue and a Semaphore is created. The Semaphore is set to a max size of 5000.
63    /// </summary>
64    private MessageQueue() {
65      queue = new Queue<MessageContainer>();
66      semaphore = new Semaphore(0, 5000);
67    }
68
69    /// <summary>
70    /// Returns the oldest MessageContainer Object from the Queue.
71    /// </summary>
72    /// <returns>the oldest MessageContainer Object</returns>
73    public MessageContainer GetMessage() {
74      semaphore.WaitOne();
75      lock (this) {
76        if (queue.Count > 0) {
77          return queue.Dequeue();
78        }
79      }
80      return null;
81    }
82
83    /// <summary>
84    /// Adds a MessageContainer Object to the Queue
85    /// </summary>
86    /// <param name="messageContainer">the MessageContainer</param>
87    public void AddMessage(MessageContainer messageContainer) {
88      lock (this) {
89        queue.Enqueue(messageContainer);
90        semaphore.Release();
91      }
92    }
93
94    /// <summary>
95    /// Adds a message to the Queue. The MessageContainer Object is built in the Method
96    /// </summary>
97    /// <param name="message">the Message</param>
98    public void AddMessage(MessageContainer.MessageType message) {
99      lock (this) {
100        queue.Enqueue(new MessageContainer(message));
101        semaphore.Release();
102      }
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.