Class IOPort

Index Home MAE > MAE Architecture > MAE Data Types > Class Index > Class IOPort

Summary
Public Methods
IOPort(resourceName)
bool open(resourceName, mode)
inline bool valid() const
inline bool active() const
static inline bool blocked()
inline bool eof() const
inline int getErrno() const
inline const string& getErrmsg() const
inline string getName() const
bool readready() const
int getchar(blocking)
string gets(blocking)
int read(rawdata, size, nmemb, blocking)
int ungetc(c)
bool writeready() const
int putchar(c)
int puts(text)
int write(rawdata)
inline long tell() const
int seek(offset)
void rewind()
bool lock(exclusive, wait)
void unlock()
int close()
static void closeall()
static int stat(pathname, statbuf)
static StringList getDirFilenames(path, includeDirs, includeFiles)
static bool isIOPortMessage(request)
static bool handleMessage(request, param)
void clearCallback()
void setCharCallback(cbfn, cbdata)
void setLineCallback(cbfn, cbdata)
void setFileCallback(cbfn, cbdata)
Private Properties
Private Methods
static IOPortData& getMBuf(name, create)
static void rmMBuf(name)
bool blockingWait(response)
static void serviceInput(data)

#include <IOPort.h >

Summary

Access a MAE I/O resource.

This class is designed to be very close to standard I/O routines to make migratation as easy as possible.

The fundamental difference is that IOPort does not directly access the file system; it accesses named MAE resources.  See tioreg for managing these resources.  The IoGW daemon handles these I/O requests.  IOPort talks to to IoGW via the IoAPI class.

Note that IOPort can be a substitute for FILE*. An instance of IOPort does not contain the vitals like file position, so it is save to make copies of IOPort - like you would a FILE*.

Public Methods

IOPort(resourceName)

const string& resourceName

resource name to connect to; the resource name may be a filename for the MAE filesystem or a unique keyword for .

Default value: ""


Constructor - connect to a named resource.

Use active() to verify connection to resource was successful.

bool open(resourceName, mode)

const string& resourceName

resource name to connect to

const string& mode

existing resources already know if they are readable/writable, but when accessing/creating new resources in directory resources, specify the mode

Default value: ""


Connect/Open the named resource. Analogous to fopen().

Return value: true upon success, false upon failure

inline bool valid() const

Determine if this port connection is valid.

A valid port has a resource name.

This does not reflect whether the connection is open or active; see active().

Return value: true if valid, false if not

inline bool active() const

Determine if this port is open and working.

If EOF or there's an error, the port is not considered active.

Return value: true if port is actrive, false if not

static inline bool blocked()

Determine if we are I/O operation blocked

Return value: true if blocked, false if not

inline bool eof() const

Determine if end-of-file has occurred.

Return value: true on EOF, false if not

inline int getErrno() const

Query the error code of the last I/O operation.

Return value: see errno.h

inline const string& getErrmsg() const

Query the error message of the last I/O operation.

Return value: see errno.h

inline string getName() const

Return the name of the MAE I/O resource.

Return value: name of resource, null string if not valid

bool readready() const

Determine if a character is ready to read on input (so a blocking getchar() is safe).

Return value: true if a character is ready, false if not

int getchar(blocking)

bool blocking

true to wait for a character, false if no wait

Default value: true


Get/Receive a character from the port. Analogous to fgetc().

Return value: the character receive, EOF if end-of-file, or 0 if no character ready

string gets(blocking)

bool blocking

true to wait for text, false if no wait

Default value: true


Get/Receive a line of text (blocking) up to \n from the port. Analogous to fgets().

The \n is included in the returned string.

Return value: the text line received, null string on end-of-file

int read(rawdata, size, nmemb, blocking)

RawData& rawdata

(output) where the inbound data will be stored

size_t size

size of record to read (in bytes)

size_t nmemb

number of records to read

Default value: 1

bool blocking

true to wait for text, false if no wait

Default value: true


Read a number of records of binary data from the port.  Analogous to fread().

The result should have size*nmemb characgters in it; if less,

then less data was available than requested.

Return value: number of records read; null string on EOF

int ungetc(c)

int c

the character to un-read


Un-read a character read from input.

Return value: c upon success, EOF on failure

bool writeready() const

Determine if we can write a character to the port.

Return value: true if writable, false if not

int putchar(c)

int c

the character to send


Send a character to the port. Analogous to fputc().

Return value: c upon success, EOF on failure

int puts(text)

const string& text

the text to send


Send text to the port. Analogous to fputs().

Return value: 0+ on success, EOF on failure

int write(rawdata)

const RawData& rawdata

the data to send


Send binary data to the port. Analogous to fwrite().

Return value: 0+ on success, EOF on failure

inline long tell() const

Return the position in the file - number of bytes read/written. Analogous to ftell().

Return value: file position (none is 0)

int seek(offset)

long offset

the new file pointer position


Move the file pointer to a specified point in the file from the start of the file. Analogous to fseek().

Note that this has no effect on resources that stream data, only on files.

Return value: 0 upon success, EOF on failure

void rewind()

Move the file pointer to the beginning of the file. Analogous to rewind().

Note that this has no effect on resources that stream data, only on files.

bool lock(exclusive, wait)

bool exclusive

Flag: true if nothing else allowed to read or write to this resource, false if other apps can read this resource

bool wait

Flag: true if app should wait until the lock has been released - this will block the app

Default value: true


Attempt to lock access to this file resource.

If an exclusive lock has already been obtained, then it can be switched to shared.

Return value: true if the lock was acquired, false if it was not acquired

void unlock()

Release a lock from this resource/file.  Other resources will be allowed to read or write once unlocked.

int close()

Close this port, stopping any further reads or writes.

Return value: 0 upon success, EOF on failure

static void closeall()

Close all open connections

static int stat(pathname, statbuf)

const string& pathname

must be a MAE virtual filesystem pathname (file or directory)

MaeStat& statbuf

(output) a structure to contain the various details about the pathname


Equivalent to stat() in Standard C Library, get statistics about the pathname provided.

Return value: errno for the operation: 0 upon success ENOENT if file does not exist

static StringList getDirFilenames(path, includeDirs, includeFiles)

const string& path

the directory from which to get filename

bool includeDirs

= Flag: include directory names

Default value: true

bool includeFiles

= Flag: include filenames

Default value: true


Get filename and/or directory name entries in the MAE virtual file system directory provided.

Use this instead of opendir()/readdir()/closedir().

Return value: list of filenames

static bool isIOPortMessage(request)

const string& request

the request keyword in question


Determine if a MAE message is an IOPort message from iogw.

Use this call in your .mreg with #msgvalid file to check if you should IOPort::handleMessage().

This call is valid for any/all port connection(s).

Return value: true if request is a for IOPOrt from iogw

static bool handleMessage(request, param)

const string& request

the request keyword in question

const XMLData& param

parameters with data associated with request


Handle a MAE message from iogw.

This call is valid for any/all port connection(s).

Return value: true if processed successfully, false on error or invalid request

void clearCallback()

Clear any receive input callbacks already setup.

void setCharCallback(cbfn, cbdata)

IOPortDataCB cbfn

the callback function to call

void* cbdata

(optional) some data to pass to the callback function


Setup a receive input callback for whenever an input character is received.

This will also be called when EOF is reached.

void setLineCallback(cbfn, cbdata)

IOPortDataCB cbfn

the callback function to call

void* cbdata

(optional) some data to pass to the callback function


Setup a receive input callback for whenever an input line is received.

A line is terminated by \n, which must be prevent before cbfn is called.

This will also be called when EOF is reached.

void setFileCallback(cbfn, cbdata)

IOPortDataCB cbfn

the callback function to call

void* cbdata

(optional) some data to pass to the callback function


Setup a receive input callback for whenever an input file is received.

The entire contents of the file has been received.

Private Properties

string name

Name of the resource of this port

static vector <IOPortData > mbuf

The IOPort buffers. Only one buffer per resource within the program.


Private Methods

static IOPortData& getMBuf(name, create)

const string& name

name of resource to find its buffer

bool create

Default value: false


Return the mbuf for the named resource

Return value: an IOPortData buffer, which has eof set if invalid

static void rmMBuf(name)

const string& name

name of resource to remove its buffer


Remove an I/O buffer

bool blockingWait(response)

const string& response

block until this response - open, input, tell


Block until we get a response from iogw

Return value: true if response received, false if something happened (like eof)

static void serviceInput(data)

const IOPortData& data

the buffer to service


When new input or EOF is received, this method is called to notify the

app if any callbacks were setup.