3.10.2013 - PIPeer release, PIConsole now can work as server and remote client. Remote console test program in directory "remote_console"

This commit is contained in:
peri4
2013-10-03 16:04:02 +04:00
parent 9111640ca8
commit 4b90f2818e
56 changed files with 6422 additions and 673 deletions

View File

@@ -28,23 +28,13 @@
// function executed from threaded read, pass ThreadedReadData, readedData, sizeOfData
typedef bool (*ReadRetFunc)(void * , uchar * , int );
// events:
// void opened()
// void closed()
//
// handlers:
// bool open()
// bool open(const PIString & path)
// bool open(const DeviceMode & type)
// bool open(const PIString & path, const DeviceMode & type)
// bool close()
// bool initialize()
// void flush()
class PIP_EXPORT PIIODevice: public PIThread
{
PIOBJECT(PIIODevice)
public:
//! Constructs a empty PIIODevice
PIIODevice();
//! \brief Open modes for PIIODevice
@@ -55,7 +45,7 @@ public:
};
PIIODevice(const PIString & path, DeviceMode type = ReadWrite, bool initNow = true);
virtual ~PIIODevice() {stop(); write_thread.stop(); if (opened_) {closeDevice(); if (!opened_) closed();}}
virtual ~PIIODevice() {stop(); if (opened_) {closeDevice(); if (!opened_) closed();}}
//! Current open mode of device
DeviceMode mode() const {return mode_;}
@@ -63,67 +53,119 @@ public:
//! Current path of device
PIString path() const {return path_;}
//! return \b true if mode is ReadOnly or ReadWrite
//! Set path of device
void setPath(const PIString & path) {path_ = path;}
//! Return \b true if mode is ReadOnly or ReadWrite
bool isReadable() const {return (mode_ & ReadOnly);}
//! return \b true if mode is WriteOnly or ReadWrite
//! Return \b true if mode is WriteOnly or ReadWrite
bool isWriteable() const {return (mode_ & WriteOnly);}
bool isInitialized() const {return init_;}
//! return \b true if device is successfully opened
//! Return \b true if device is successfully opened
bool isOpened() const {return opened_;}
//! return \b true if device is closed
//! Return \b true if device is closed
bool isClosed() const {return !opened_;}
//! return \b true if device can read \b now
//! Return \b true if device can read \b now
bool canRead() const {return opened_ && (mode_ & ReadOnly);}
//! return \b true if device can write \b now
//! Return \b true if device can write \b now
bool canWrite() const {return opened_ && (mode_ & WriteOnly);}
// Enable timer to periodically open device until it will be opened
//! Set execution of \a open enabled while threaded read on closed device
void setReopenEnabled(bool yes = true) {reopen_enabled_ = yes;}
//! Set timeout in milliseconds between \a open tryings if reopen is enabled
void setReopenTimeout(int msecs = 1000) {reopen_timeout_ = msecs;}
//! Return reopen enable
bool isReopenEnabled() const {return reopen_enabled_;}
//! Return reopen timeout
int reopenTimeout() {return reopen_timeout_;}
// set return function executed when successful read in thread
/** \brief Set "threaded read slot"
* \details Set external static function of threaded read that will be executed
* at every successful threaded read. Function should have format
* "bool func(void * data, uchar * readed, int size)" */
void setThreadedReadSlot(ReadRetFunc func) {ret_func_ = func;}
//! Set custom data that will be passed to "threaded read slot"
void setThreadedReadData(void * d) {ret_data_ = d;}
/** \brief Set size of threaded read buffer
* \details Default size is 4096 bytes. If your device can read at single read
* more than 4096 bytes you should use this function to adjust buffer size */
void setThreadedReadBufferSize(int new_size) {buffer_tr.resize(new_size);}
//! Return size of threaded read buffer
int threadedReadBufferSize() const {return buffer_tr.size_s();}
//! Return content of threaded read buffer
const uchar * threadedReadBuffer() const {return buffer_tr.data();}
bool isThreadedRead() const {return isRunning();}
void startThreadedRead() {if (!isRunning()) start();}
void startThreadedRead(ReadRetFunc func) {ret_func_ = func; if (!isRunning()) start();}
//! Return \b true if threaded read is started
bool isThreadedRead() const {return isRunning();}
//! Start threaded read
void startThreadedRead() {if (!isRunning()) PIThread::start();}
//! Start threaded read and assign "threaded read slot" to "func"
void startThreadedRead(ReadRetFunc func) {ret_func_ = func; if (!isRunning()) PIThread::start();}
//! Stop threaded read
void stopThreadedRead() {PIThread::stop();}
//! Return \b true if threaded write is started
bool isThreadedWrite() const {return write_thread.isRunning();}
//! Start threaded write
void startThreadedWrite() {if (!write_thread.isRunning()) write_thread.startOnce();}
//! Stop threaded write
void stopThreadedWrite() {write_thread.stop();}
//! Clear threaded write task queue
void clearThreadedWriteQueue() {write_thread.lock(); write_queue.clear(); write_thread.unlock();}
//! Start both threaded read and threaded write
void start() {startThreadedRead(); startThreadedWrite();}
//! Stop both threaded read and threaded write and if "wait" block until both threads are stop
void stop(bool wait = false) {stopThreadedRead(); stopThreadedWrite(); if (wait) while (write_thread.isRunning() || isRunning()) msleep(1);}
// Read from device to "read_to" maximum "max_size" bytes, return readed bytes count
virtual int read(void * read_to, int max_size) {piCout << "[PIIODevice] \"read\" not implemented!"; return -2;}
// Write to device "data" maximum "max_size" bytes, return written bytes count
virtual int write(const void * data, int max_size) {piCout << "[PIIODevice] \"write\" not implemented!"; return -2;}
//! Reimplement this function to read from your device
virtual int read(void * read_to, int max_size) {piCoutObj << "[PIIODevice] \"read\" not implemented!"; return -2;}
// Read from device maximum "max_size" bytes and return them as PIByteArray
//! Reimplement this function to write to your device
virtual int write(const void * data, int max_size) {piCoutObj << "[PIIODevice] \"write\" not implemented!"; return -2;}
//! Read from device maximum "max_size" bytes and return them as PIByteArray
PIByteArray read(int max_size) {buffer_in.resize(max_size); int ret = read(buffer_in.data(), max_size); if (ret < 0) return PIByteArray(); return buffer_in.resized(ret);}
//! Write to device "data"
int write(const PIByteArray & data) {return write(data.data(), data.size_s());}
//! Add task to threaded write queue and return task ID
ullong writeThreaded(const void * data, int max_size) {return writeThreaded(PIByteArray(data, uint(max_size)));}
//! Add task to threaded write queue and return task ID
ullong writeThreaded(const PIByteArray & data);
EVENT_HANDLER(bool, open) {if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
EVENT_HANDLER1(bool, open, const PIString &, _path) {path_ = _path; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
EVENT_HANDLER1(bool, open, const DeviceMode &, _type) {mode_ = _type; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
@@ -153,6 +195,12 @@ public:
//! \fn bool open(const PIString & path, const DeviceMode & mode)
//! \brief Open device with path "path" and mode "mode"
//! \fn bool close()
//! \brief Close device
//! \fn bool initialize()
//! \brief Initialize device
//! \}
//! \vhandlers
//! \{
@@ -174,19 +222,22 @@ public:
//! \brief Raise if read thread succesfull read some data
//! \fn void threadedWriteEvent(ullong id, int written_size)
//! \brief Raise if write thread succesfull write some data of queue item with id "id"
//! \brief Raise if write thread succesfull write some data of task with ID "id"
//! \}
protected:
// Function executed before first openDevice() or from constructor
//! Function executed before first \a openDevice() or from constructor
virtual bool init() {return true;}
// Functions to open and close device, return value will set to "opened_" variable
//! Reimplement to open device, return value will be set to "opened_" variable
virtual bool openDevice() = 0; // use path_, type_, opened_, init_ variables
//! Reimplement to close device, inverse return value will be set to "opened_" variable
virtual bool closeDevice() {return true;} // use path_, type_, opened_, init_ variables
// Function executed when thread read some data, default implementation execute external slot "ret_func_"
//! Function executed when thread read some data, default implementation execute external slot "ret_func_"
virtual bool threadedRead(uchar * readed, int size) {if (ret_func_ != 0) return ret_func_(ret_data_, readed, size); return true;}
void terminate();
@@ -194,7 +245,7 @@ protected:
PIString path_;
DeviceMode mode_;
ReadRetFunc ret_func_;
bool init_, opened_, thread_started_, reopen_enabled_;
bool init_, opened_, thread_started_, reopen_enabled_, raise_threaded_read_;
int reopen_timeout_;
void * ret_data_;