Monitor
CommandManager.cs
Go to the documentation of this file.
1 using System.Threading;
2 
3 namespace monitor
4 {
10  public class CommandManager
11  {
15  public delegate void CommandReceivedEvent(string msg, byte[] buffer);
16  public CommandReceivedEvent commandReceivedEvent = null;
17 
21  private System.Timers.Timer waitTimer = new System.Timers.Timer();
22  private ManualResetEvent waitEvent = new ManualResetEvent(false);
23 
27  private bool waitForAcknowledge = false;
28 
32  private string messageReceived = null;
33 
37  private bool isBusy = false;
38 
43  {
44  AnswerReceived,
45  Timeout,
46  Busy
47  };
48 
53  public CommandManager(CommandReceivedEvent callback)
54  {
56 
57  this.commandReceivedEvent += callback;
58  waitTimer.Elapsed += OnMessageTimeout;
59  }
60 
66  {
67  Client.Close();
68  }
69 
75  public bool Open(string hostname)
76  {
77  return this.Open(hostname, Client.defaultPort);
78  }
79 
86  public bool Open(string hostname, int port)
87  {
88  return Client.Open(hostname, port);
89  }
90 
94  public void Close()
95  {
96  Client.Close();
97  }
98 
104  private void OnMessageReception(string message, byte[] buffer)
105  {
106  waitTimer.Stop(); // Stop timeout stopwatch
107 
108  this.messageReceived = message;
109  isBusy = false;
110 
111  // if SendCommand wait for an acknowledge, release semaphore waitEvent
112  // so that SendCommand will be able to read received answer
113  // Received answer will not be sent to upper level
114  if (waitForAcknowledge)
115  {
116  waitForAcknowledge = false;
117  waitEvent.Set(); // Envoi de l'evenement
118  }
119  else
120  // if sendCommand doesn't wait for an acknowledge, message received
121  // is for upper level, so call callback
122  {
123 
124  waitForAcknowledge = false;
125 
126  this.commandReceivedEvent?.Invoke(message, buffer);
127  }
128  }
129 
135  private void OnMessageTimeout(object sender, System.Timers.ElapsedEventArgs e)
136  {
137  messageReceived = null;
138  // set buffer and message as null to indicate that no message was received
139  // and call to OnMessagereception is due to timeout
140  OnMessageReception(messageReceived, null);
141  }
142 
150  public CommandManagerStatus SendCommand(string cmd, out string answer, double timeout)
151  {
152  CommandManagerStatus status = CommandManagerStatus.AnswerReceived;
153  answer = null;
154 
155 
156  if (isBusy) status = CommandManagerStatus.Busy;
157  else
158  {
159  isBusy = true;
160 
161  Client.Write(cmd);
162 
163  if (timeout > 0) // la commande attend un acquitement
164  {
165  waitForAcknowledge = true;
166  waitTimer.Interval = timeout;
167  waitTimer.Start();
168 
169  waitEvent.WaitOne();
170  waitEvent.Reset(); // remise à zero pour une prochaine commande
171 
172  if (this.messageReceived == null) // timeout: connection au serveur defectueuse
173  {
174  status = CommandManagerStatus.Timeout;
175  }
176  }
177  else isBusy = false;
178 
179  answer = this.messageReceived;
180  this.messageReceived = null;
181  }
182 
183  return status;
184  }
185  }
186 }
void OnMessageTimeout(object sender, System.Timers.ElapsedEventArgs e)
Callback called by stopwatch on timeout
const int defaultPort
Default server port number
Definition: Client.cs:20
bool Open(string hostname)
Open the specified hostname server, using default port number.
static void Write(string mes)
Write a string to server
Definition: Client.cs:198
Command Manager. Use for timeout managment during reception of data Used as intermediate layer betwee...
delegate void CommandReceivedEvent(string msg, byte[] buffer)
Callback for sending received data to upper level
CommandManagerStatus SendCommand(string cmd, out string answer, double timeout)
Sends a command to TCP server
bool isBusy
flag indicating command manager is currently busy waiting an acknowledge
void OnMessageReception(string message, byte[] buffer)
Callback called by Client class after reception of new message
CommandReceivedEvent commandReceivedEvent
System.Timers.Timer waitTimer
Timer for managing timeout
ManualResetEvent waitEvent
Definition: Client.cs:5
static bool Open(string host)
Open connection to server "host", on default port number.
Definition: Client.cs:68
Static class for TCP client
Definition: Client.cs:10
static void Close()
Close connection to server
Definition: Client.cs:120
CommandManagerStatus
Available status when sending command
static ReadEvent readEvent
Definition: Client.cs:61
bool Open(string hostname, int port)
Open connection to server "host", with port number "port"
string messageReceived
received message
CommandManager(CommandReceivedEvent callback)
Initializes a new instance of the T:monitor.CommandManager class.
~CommandManager()
Releases unmanaged resources and performs other cleanup operations before the T:monitor.CommandManager is reclaimed by garbage collection.
bool waitForAcknowledge
Flag to tell rogram to wait for an acknowledge from server
void Close()
Close connection to server