MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Connection.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Connection.h Multithread communicator
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef CONNECTION_H
28 #define CONNECTION_H
29 
30 #include "ThreadQueue.h"
31 #include "Mutex.h"
32 #include "itype.h"
33 #include "Logger.h"
34 #include <assert.h>
35 
36 namespace MFM
37 {
44  class Connection
45  {
46  private:
47 
51  Mutex m_lock;
52 
56  ThreadQueue m_outbuffer, m_inbuffer;
57 
62  bool m_connected;
63 
64  public:
65 
71  {
72  m_connected = false;
73  }
74 
79  {
80  }
81 
90  void SetConnected(bool value)
91  {
92  m_connected = value;
93  }
94 
102  bool IsConnected()
103  {
104  return m_connected;
105  }
106 
119  {
120  return m_lock.IsLockedByAnother();
121  }
122 
128  bool Lock()
129  {
130  /*
131  if (m_lock.IHoldThisLock()) {
132  LOG.Debug("Connection %p already owns lock",(void*) this);
133  return false;
134  }
135  */
136  return m_lock.TryLock();
137  }
138 
144  void Unlock()
145  {
146  m_lock.Unlock();
147  }
148 
161  void ReadBlocking(bool child, u8* buffer, u32 length)
162  {
163  ThreadQueue& queue = child ? m_outbuffer : m_inbuffer;
164 
165  queue.ReadBlocking(buffer, length);
166  }
167 
184  u32 Read(bool child, u8* buffer, u32 length)
185  {
186  ThreadQueue& queue = child ? m_outbuffer : m_inbuffer;
187 
188  return queue.Read(buffer, length);
189  }
190 
197  void PeekRead(bool output, u8* buffer, u32 index, u32 length)
198  {
199  ThreadQueue& queue = output ? m_outbuffer : m_inbuffer;
200 
201  queue.PeekRead(buffer, index, length);
202  }
203 
219  void Write(bool child, u8* buffer, u32 length)
220  {
221  ThreadQueue& queue = !child ? m_outbuffer : m_inbuffer;
222 
223  queue.Write(buffer, length);
224  }
225 
231  {
232  return m_inbuffer.BytesAvailable();
233  }
234 
240  {
241  return m_outbuffer.BytesAvailable();
242  }
243 
244  void LogAllPackets()
245  {
246 
247  }
248 
249  void ReportConnectionStatus(Logger::Level level, bool owned) ;
250 
251  };
252 }
253 
254 #endif /* CONNECTION_H */
bool IsLockedByAnother()
Definition: Connection.h:118
u32 Read(u8 *bytes, u32 length)
Definition: ThreadQueue.cpp:75
Level
Definition: Logger.h:52
bool TryLock()
Definition: Mutex.h:190
void ReadBlocking(bool child, u8 *buffer, u32 length)
Definition: Connection.h:161
void Write(u8 *bytes, u32 length)
Definition: ThreadQueue.cpp:20
void PeekRead(u8 *toBuffer, u32 index, u32 length)
Definition: ThreadQueue.cpp:91
Definition: Mutex.h:42
void SetConnected(bool value)
Definition: Connection.h:90
bool IsLockedByAnother()
Definition: Mutex.h:277
~Connection()
Definition: Connection.h:78
u32 Read(bool child, u8 *buffer, u32 length)
Definition: Connection.h:184
void Unlock()
Definition: Connection.h:144
Connection()
Definition: Connection.h:70
bool Lock()
Definition: Connection.h:128
u32 OutputByteCount()
Definition: Connection.h:239
void ReadBlocking(u8 *bytes, u32 length)
Definition: ThreadQueue.cpp:61
bool IsConnected()
Definition: Connection.h:102
void Unlock()
Definition: Mutex.h:258
u32 BytesAvailable()
Definition: ThreadQueue.cpp:112
Definition: ThreadQueue.h:42
u32 InputByteCount()
Definition: Connection.h:230
void Write(bool child, u8 *buffer, u32 length)
Definition: Connection.h:219
void PeekRead(bool output, u8 *buffer, u32 index, u32 length)
Definition: Connection.h:197
Definition: Connection.h:44