Changing Protocols in the Universal Streaming Service |
The Universal Streaming System (USS) is an object-level protocol that is independent of the underlying primitive protocol. As the USS decomposes class objects into their primitive data elements, each primitive type must be converted to a data representation before transmission or storage. There are many traditional protocols that encode the primitive data elements, such as XDR, NDR, and ASCII.
By default, the USS uses the Network Data Representation (NDR) protocol to encode objects. Due to efficient design, NDR is the protocol used for many client/server applications. It is also the encoding scheme selected by the industry-standard Remote Procedure Call (RPC) mechanism, a traditional mechanism for C development.
Although NDR is usually the best
protocol choice for client/server communication, an alternative encoding scheme
is sometimes appropriate. The USS supports multiple primitive encoding schemes
through a protocol adapter interface defined by the abstract class os_protocol
. Streaming<ToolKit> includes two protocol adapters, os_ndr
and os_ascii . The os_ndr
class provides the high-performance translation layer for NDR support. The os_ascii
class is provided as a learning aid for developing custom protocol adapters, and
this class can be used for debugging binary streams or for transmitting across
special devices.
To specify a protocol, supply the
protocol adapter as an argument to the os_bstream
constructor.
// Build an ASCII adapter for device.
os_ascii protocol( os_adapter_for( device ) );
// Build a USS stream using the ASCII protocol.
os_bstream stream( protocol );
Note that the device adapter is chained to the protocol adapter and does not interact directly with the USS stream object. This layering concept allows the protocol adapter to maintain correct state information as it translates primitive values from the device, and also allows the protocol adapter to be used independently, providing an object-oriented interface for encoding primitive data types. This can be a useful tool if the project involves communicating with hardware devices or with legacy systems.
The following example uses the os_ascii
protocol adapter to persist time and date objects to an ASCII text file. Because
the resulting object stream is ASCII and does not contain unprintable
characters, the fstream object is not opened in
binary mode.
#include <fstream>
#include <ospace/stream.h>
#include <ospace/time.h>
#include <ospace/uss/time.h>
void
write()
{
fstream file( "objio6.txt", ios::out | ios::trunc );
os_ascii protocol( os_adapter_for( file ) );
os_bstream stream( protocol );
os_date date = os_date::today();
stream << date;
os_time time = os_time::now();
stream << time;
cout << "Wrote " << date << ", " << time << endl;
}
void
read()
{
fstream file( "objio6.txt", ios::in );
os_ascii protocol( os_adapter_for( file ) );
os_bstream stream( protocol );
os_date date;
stream >> date; // Read an os_date into date.
os_time time;
stream >> time; // Read an os_time into time.
cout << "Read " << date << ", " << time<< endl;
}
void
main()
{
os_streaming_toolkit init_streaming;
os_time_toolkit init_time;
write();
read();
}
Wrote 09/27/96, 15:57:31.981608
Read 09/27/96, 15:57:31.981608
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.