RemoteConnection Class Reference

A RemoteConnection object provides a bidirectional connection to another RemoteConnection object on a remote machine. More...

#include <remoteconnection.h>

Inheritance diagram for RemoteConnection:

Inheritance graph
[legend]
Collaboration diagram for RemoteConnection:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 RemoteConnection (int fdin_, int fdout_, const std::string &context_=std::string())
 Constructor.
 ~RemoteConnection ()
 Destructor.
bool ready_to_read () const
 See if there is data available to read.
char sniff_next_message_type (double end_time)
 Check what the next message type is.
char get_message (std::string &result, double end_time)
 Read one message from fdin.
char get_message_chunked (double end_time)
 Prepare to read one message from fdin in chunks.
bool get_message_chunk (std::string &result, size_t at_least, double end_time)
 Read a chunk of a message from fdin.
char receive_file (const std::string &file, double end_time)
 Save the contents of a message as a file.
void send_message (char type, const std::string &s, double end_time)
 Send a message.
void send_file (char type, int fd, double end_time)
 Send the contents of a file as a message.
void do_close (bool wait)
 Shutdown the connection.

Protected Attributes

std::string context
 The context to report with errors.

Private Member Functions

void operator= (const RemoteConnection &)
 Don't allow assignment.
 RemoteConnection (const RemoteConnection &)
 Don't allow copying.
void read_at_least (size_t min_len, double end_time)
 Read until there are at least min_len bytes in buffer.

Private Attributes

int fdin
 The file descriptor used for reading.
int fdout
 The file descriptor used for writing.
std::string buffer
 Buffer to hold unprocessed input.
off_t chunked_data_left
 Remaining bytes of message data still to come over fdin for a chunked read.


Detailed Description

A RemoteConnection object provides a bidirectional connection to another RemoteConnection object on a remote machine.

The connection is implemented using a pair of file descriptors. Messages with a single byte type code and arbitrary data as the contents can be sent and received.

Definition at line 94 of file remoteconnection.h.


Constructor & Destructor Documentation

RemoteConnection::RemoteConnection ( const RemoteConnection  )  [private]

Don't allow copying.

RemoteConnection::RemoteConnection ( int  fdin_,
int  fdout_,
const std::string &  context_ = std::string() 
)

Constructor.

Definition at line 61 of file remoteconnection.cc.

References context.

RemoteConnection::~RemoteConnection (  ) 

Destructor.

Definition at line 75 of file remoteconnection.cc.


Member Function Documentation

void RemoteConnection::do_close ( bool  wait  ) 

Shutdown the connection.

Parameters:
wait If true, wait for the remote end to close the connection before returning.

Definition at line 648 of file remoteconnection.cc.

References close_fd_or_socket(), dummy, fdin, fdout, LOGCALL_VOID, MSG_SHUTDOWN, and send_message().

Referenced by RemoteDatabase::do_close().

char RemoteConnection::get_message ( std::string &  result,
double  end_time 
)

Read one message from fdin.

Parameters:
[out] result Message data.
end_time If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns:
Message type code.

Definition at line 471 of file remoteconnection.cc.

References buffer, fdin, LOGCALL, read_at_least(), and RETURN.

Referenced by Xapian::DatabaseReplica::Internal::apply_db_copy(), Xapian::DatabaseReplica::Internal::apply_next_changeset(), RemoteDatabase::get_message(), and ReplicateTcpServer::handle_one_connection().

bool RemoteConnection::get_message_chunk ( std::string &  result,
size_t  at_least,
double  end_time 
)

Read a chunk of a message from fdin.

You must call get_message_chunked() before calling this method.

Parameters:
inout] result Message data. This is appended to, so if you read more than needed the previous time, leave the excess in result.
at_least Return at least this many bytes in result, unless there isn't enough data left in the message (in which case all remaining data is read and false is returned).
end_time If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns:
true if at least at_least bytes are now in result.

Definition at line 550 of file remoteconnection.cc.

References buffer, chunked_data_left, fdin, LOGCALL, read_at_least(), and RETURN.

Referenced by FlintDatabaseReplicator::apply_changeset_from_conn(), ChertDatabaseReplicator::apply_changeset_from_conn(), BrassDatabaseReplicator::apply_changeset_from_conn(), FlintDatabaseReplicator::process_changeset_chunk_base(), ChertDatabaseReplicator::process_changeset_chunk_base(), BrassDatabaseReplicator::process_changeset_chunk_base(), FlintDatabaseReplicator::process_changeset_chunk_blocks(), ChertDatabaseReplicator::process_changeset_chunk_blocks(), and BrassDatabaseReplicator::process_changeset_chunk_blocks().

char RemoteConnection::get_message_chunked ( double  end_time  ) 

Prepare to read one message from fdin in chunks.

Sometimes a message can be sufficiently large that you don't want to read it all into memory before processing it. Also, it may be more efficient to process it as you go.

This method doesn't actually return any message data - call get_message_chunk() to do that.

Parameters:
end_time If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns:
Message type code.

Definition at line 510 of file remoteconnection.cc.

References buffer, chunked_data_left, fdin, LOGCALL, read_at_least(), and RETURN.

Referenced by FlintDatabaseReplicator::apply_changeset_from_conn(), ChertDatabaseReplicator::apply_changeset_from_conn(), and BrassDatabaseReplicator::apply_changeset_from_conn().

void RemoteConnection::operator= ( const RemoteConnection  )  [private]

Don't allow assignment.

void RemoteConnection::read_at_least ( size_t  min_len,
double  end_time 
) [private]

Read until there are at least min_len bytes in buffer.

If for some reason this isn't possible, throws NetworkError.

Parameters:
min_len Minimum number of bytes required in buffer.
end_time If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0), then keep trying indefinitely.

Definition at line 84 of file remoteconnection.cc.

References Assert, buffer, CHUNKSIZE, context, fdin, LOGCALL_VOID, LOGLINE, and RealTime::now().

Referenced by get_message(), get_message_chunk(), get_message_chunked(), receive_file(), and sniff_next_message_type().

bool RemoteConnection::ready_to_read (  )  const

See if there is data available to read.

Returns:
true if there is data waiting to be read.

Definition at line 181 of file remoteconnection.cc.

References buffer, fdin, LOGCALL, and RETURN.

Referenced by RemoteDatabase::get_remote_stats().

char RemoteConnection::receive_file ( const std::string &  file,
double  end_time 
)

Save the contents of a message as a file.

Parameters:
file Path to file to save the message data into. If the file exists it will be overwritten.
end_time If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns:
Message type code.

Definition at line 590 of file remoteconnection.cc.

References buffer, CHUNKSIZE, fdin, LOGCALL, msvc_posix_open(), O_BINARY, Xapian::InMemory::open(), read_at_least(), RETURN, and write_all().

Referenced by Xapian::DatabaseReplica::Internal::apply_db_copy().

void RemoteConnection::send_file ( char  type,
int  fd,
double  end_time 
)

Send the contents of a file as a message.

Parameters:
type Message type code.
fd File containing the message data.
end_time If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.

Definition at line 318 of file remoteconnection.cc.

References AssertRel, CHUNKSIZE, context, encode_length(), fdout, LOGCALL_VOID, LOGLINE, RealTime::now(), and stat().

Referenced by FlintDatabase::send_whole_database(), ChertDatabase::send_whole_database(), BrassDatabase::send_whole_database(), FlintDatabase::write_changesets_to_fd(), ChertDatabase::write_changesets_to_fd(), and BrassDatabase::write_changesets_to_fd().

void RemoteConnection::send_message ( char  type,
const std::string &  s,
double  end_time 
)

Send a message.

Parameters:
type Message type code.
s Message data.
end_time If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.

Definition at line 205 of file remoteconnection.cc.

References context, encode_length(), fdout, LOGCALL_VOID, LOGLINE, RealTime::now(), and Xapian::Internal::str().

Referenced by do_close(), RemoteServer::send_message(), RemoteDatabase::send_message(), FlintDatabase::send_whole_database(), ChertDatabase::send_whole_database(), BrassDatabase::send_whole_database(), FlintDatabase::write_changesets_to_fd(), ChertDatabase::write_changesets_to_fd(), BrassDatabase::write_changesets_to_fd(), and Xapian::DatabaseMaster::write_changesets_to_fd().

char RemoteConnection::sniff_next_message_type ( double  end_time  ) 

Check what the next message type is.

This must not be called after a call to get_message_chunked() until get_message_chunk() has returned false to indicate the whole message has been received.

Other than that restriction, this may be called at any time to determine what the next message waiting to be processed is: it will not affect subsequent calls which read messages.

Parameters:
end_time If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns:
Message type code.

Definition at line 458 of file remoteconnection.cc.

References buffer, fdin, LOGCALL, read_at_least(), and RETURN.

Referenced by Xapian::DatabaseReplica::Internal::apply_db_copy(), and Xapian::DatabaseReplica::Internal::apply_next_changeset().


Member Data Documentation

std::string RemoteConnection::buffer [private]

Remaining bytes of message data still to come over fdin for a chunked read.

Definition at line 120 of file remoteconnection.h.

Referenced by get_message_chunk(), and get_message_chunked().

std::string RemoteConnection::context [protected]

The context to report with errors.

Subclasses are allowed to manage this.

Definition at line 153 of file remoteconnection.h.

Referenced by RemoteServer::msg_writeaccess(), read_at_least(), RemoteConnection(), RemoteServer::RemoteServer(), send_file(), and send_message().

int RemoteConnection::fdin [private]

The file descriptor used for reading.

If this is -1, the connection is unidirection and write-only. If both fdin and fdout are -1, then the connection has been closed.

Definition at line 106 of file remoteconnection.h.

Referenced by do_close(), get_message(), get_message_chunk(), get_message_chunked(), read_at_least(), ready_to_read(), receive_file(), and sniff_next_message_type().

int RemoteConnection::fdout [private]

The file descriptor used for writing.

If this is -1, the connection is unidirection and read-only. If both fdin and fdout are -1, then the connection has been closed. It is valid for fdout to be the same as fdin.

Definition at line 114 of file remoteconnection.h.

Referenced by do_close(), send_file(), and send_message().


The documentation for this class was generated from the following files:

Documentation for Xapian (version 1.2.13).
Generated on 9 Jan 2013 by Doxygen 1.5.9.