note description: "[ A communication channel for sending and reading data from an operation system port. ]" legal : "See notice at end of class." status : "See notice at end of class."; author : "Paul Bates (paul.a.bates@gmail.com)" date : "$Date$" revision: "$Revision$" class PRT_CONNECTION inherit SAFE_DISPOSABLE inherit {NONE} SHARED_DISPOSE_UTILITIES export {NONE} all end create make feature {NONE} -- Initialization make (a_address: like address; a_factory: like stream_factory) -- Creates a new connection to an operating system port using a custom port stream factory. -- -- `a_address': The operating system port address. -- `a_factory': Portstream factory to use when creating the port's reading and writing streams. require a_address_is_valid_address: is_valid_address (a_address) a_factory_attached: attached a_factory do address := a_address stream_factory := a_factory ensure address_set: address = a_address stream_factory_set: stream_factory = a_factory end feature {NONE} -- Clean Up safe_dispose (a_disposing: BOOLEAN) -- do dispose_utils.dispose_of (internal_reader) dispose_utils.dispose_of (internal_writer) end feature -- Access frozen address: NATURAL_16 -- Operating system port address. frozen reader: PRT_STREAM_READER_I -- Reader used to read data from port. require is_interface_usable: is_interface_usable is_readable: is_readable do if attached internal_reader as l_result then Result := l_result else Result := stream_factory.create_port_reader (Current, 0) internal_reader := Result end ensure result_attached: attached Result result_consistent: Result = writer end frozen writer: PRT_STREAM_WRITER_I -- Writer used to send data to port. require is_interface_usable: is_interface_usable is_writable: is_writable do if attached internal_writer as l_result then Result := l_result else Result := stream_factory.create_port_writer (Current, 0) internal_writer := Result end ensure result_attached: attached Result result_consistent: Result = writer end feature {NONE} -- Access frozen stream_factory: PRT_STREAM_FACTORY_I -- Factory used to create port reader/writer streams. feature -- Status report is_readable: BOOLEAN -- Indicates if a port can be read from. do Result := is_interface_usable and then reader.is_readable ensure is_interface_usable: Result implies is_interface_usable reader_is_interface_usable: Result implies reader.is_interface_usable end is_writable: BOOLEAN -- Indicates if a port can be written to. do Result := is_interface_usable and then writer.is_interface_usable ensure is_interface_usable: Result implies is_interface_usable writer_is_interface_usable: Result implies writer.is_interface_usable end feature -- Query is_valid_address (a_address: NATURAL_16): BOOLEAN -- Determines if an address is valid for the current port do Result := a_address /= 0 ensure non_zero_a_address: Result implies a_address /= 0 end feature {NONE} -- Internal implementation cache internal_reader: detachable like reader -- Cached version of `reader' -- Note: Do not use directly! note options: stable attribute end internal_writer: detachable like writer -- Cached version of `writer' -- Note: Do not use directly! note options: stable attribute end invariant stream_factory_attached: attached stream_factory address_is_valid_address: is_valid_address (address) end