OpenASIP  2.0
ProximLineReader.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ProximLineReader.hh
26  *
27  * Declaration of ProximLineReader class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2005 (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #ifndef TTA_PROXIM_LINE_READER_HH
34 #define TTA_PROXIM_LINE_READER_HH
35 
36 #include <iostream>
37 #include <wx/wx.h>
38 #include <string>
39 #include <queue>
40 #include "LineReader.hh"
41 #include "Application.hh"
42 #include "Exception.hh"
43 
44 /**
45  * Handles user input passing between the simulator and GUI threads in Proxim.
46  *
47  * This class is designed to act as an interface between two threads:
48  * A GUI thread handling the user interface, and a worker thread running the
49  * simulator engine. The worker thread waits for the user input
50  * by listening to the 'input_' condition. When the line reader receives
51  * input from the GUI thread, the worker thread is signaled about the input
52  * by setting the input_ condition. If the worker thread is busy and not
53  * listening to the input_ condition signal when the GUI thread signals
54  * about user input, the signal is lost and the user input is ignored.
55  * Therefore the user input should be disabled in the GUI when the worker
56  * thread is busy. ProximLineReader requests user input and sends simulator
57  * engine output to the GUI thread using SimulatorEvents and the standard
58  * wxWidgets event handling system.
59  */
60 class ProximLineReader : public LineReader {
61 public:
63  virtual ~ProximLineReader();
64 
65  virtual void initialize(
66  std::string defPrompt = "",
67  FILE* in = stdin,
68  FILE* out = stdout,
69  FILE* err = stderr);
70 
71  virtual std::string readLine(std::string prompt = "");
72 
73  virtual char charQuestion(
74  std::string question, std::string allowedChars,
75  bool caseSensitive = false, char defaultAnswer = '\0');
76 
77  void input(std::string command);
78 
79  virtual std::ostream& outputStream();
80  void output(std::string text);
81 
82  virtual void setInputHistoryLog(const std::string& historyFilename);
83 
84  std::string inputHistoryFilename() const;
85 
86 private:
87  /// Copying not allowed.
89  /// Assignment not allowed.
91  wxMutex* mutex_;
92  /// Condition, which is signaled when user input is received from the GUI.
93  wxCondition* input_;
94  /// Input prompt.
95  std::string prompt_;
96  /// Input queue.
97  std::queue<std::string> inputQueue_;
98  /// GUI to send the events to.
99  wxEvtHandler* gui_;
100  /// Output stream which converts the output to SimulatorEvents.
101  std::ostream* outputStream_;
102 
103  /// Name of the input history file.
104  std::string historyFile_;
105 
106  static const std::string DEFAULT_LOG_FILE_NAME;
107 };
108 
109 
110 /**
111  * Stream buffer for the ProximLROutputStream.
112  *
113  * This streambuffer converts the stream output to SimulatorEvents when
114  * the buffer cotnetnts is flushed.
115  */
116 class ProximLROutputBuffer : public std::streambuf {
117 public:
119  virtual ~ProximLROutputBuffer();
120 protected:
121  int overflow(int);
122  int sync();
123 private:
124  void flushBuffer();
126  const unsigned int BUFFER_SIZE;
127 
128 };
129 
130 
131 /**
132  * An output stream which converts stream output to SimulatorEvents.
133  */
134 class ProximLROutputStream : public std::ostream {
135 public:
137  virtual ~ProximLROutputStream();
138 };
139 
140 #endif
ProximLROutputBuffer::flushBuffer
void flushBuffer()
Definition: ProximLineReader.cc:279
ProximLineReader::DEFAULT_LOG_FILE_NAME
static const std::string DEFAULT_LOG_FILE_NAME
Definition: ProximLineReader.hh:106
ProximLROutputBuffer::~ProximLROutputBuffer
virtual ~ProximLROutputBuffer()
Definition: ProximLineReader.cc:237
ProximLineReader::historyFile_
std::string historyFile_
Name of the input history file.
Definition: ProximLineReader.hh:104
ProximLROutputBuffer
Definition: ProximLineReader.hh:116
ProximLROutputBuffer::lineReader_
ProximLineReader * lineReader_
Definition: ProximLineReader.hh:125
Exception.hh
ProximLineReader::operator=
ProximLineReader & operator=(const ProximLineReader &)
Assignment not allowed.
ProximLROutputBuffer::ProximLROutputBuffer
ProximLROutputBuffer(ProximLineReader *lineReader)
Definition: ProximLineReader.cc:223
ProximLROutputBuffer::BUFFER_SIZE
const unsigned int BUFFER_SIZE
Definition: ProximLineReader.hh:126
ProximLineReader::outputStream_
std::ostream * outputStream_
Output stream which converts the output to SimulatorEvents.
Definition: ProximLineReader.hh:101
ProximLROutputBuffer::sync
int sync()
Definition: ProximLineReader.cc:269
ProximLineReader::inputQueue_
std::queue< std::string > inputQueue_
Input queue.
Definition: ProximLineReader.hh:97
ProximLineReader::input
void input(std::string command)
Definition: ProximLineReader.cc:131
ProximLROutputStream
Definition: ProximLineReader.hh:134
ProximLineReader::setInputHistoryLog
virtual void setInputHistoryLog(const std::string &historyFilename)
Definition: ProximLineReader.cc:299
ProximLineReader::ProximLineReader
ProximLineReader()
Definition: ProximLineReader.cc:48
ProximLineReader::~ProximLineReader
virtual ~ProximLineReader()
Definition: ProximLineReader.cc:61
ProximLineReader::inputHistoryFilename
std::string inputHistoryFilename() const
Definition: ProximLineReader.cc:310
LineReader.hh
ProximLROutputBuffer::overflow
int overflow(int)
Definition: ProximLineReader.cc:248
ProximLineReader::charQuestion
virtual char charQuestion(std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')
Definition: ProximLineReader.cc:148
Application.hh
ProximLineReader
Definition: ProximLineReader.hh:60
ProximLineReader::initialize
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
Definition: ProximLineReader.cc:76
ProximLineReader::output
void output(std::string text)
Definition: ProximLineReader.cc:194
ProximLineReader::gui_
wxEvtHandler * gui_
GUI to send the events to.
Definition: ProximLineReader.hh:99
ProximLineReader::outputStream
virtual std::ostream & outputStream()
Definition: ProximLineReader.cc:185
ProximLineReader::readLine
virtual std::string readLine(std::string prompt="")
Definition: ProximLineReader.cc:98
ProximLROutputStream::ProximLROutputStream
ProximLROutputStream(ProximLineReader *lineReader)
Definition: ProximLineReader.cc:205
ProximLineReader::mutex_
wxMutex * mutex_
Definition: ProximLineReader.hh:91
LineReader
Definition: LineReader.hh:52
ProximLROutputStream::~ProximLROutputStream
virtual ~ProximLROutputStream()
Definition: ProximLineReader.cc:214
ProximLineReader::prompt_
std::string prompt_
Input prompt.
Definition: ProximLineReader.hh:95
ProximLineReader::input_
wxCondition * input_
Condition, which is signaled when user input is received from the GUI.
Definition: ProximLineReader.hh:93