Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Professional C++ [eng].pdf
Скачиваний:
284
Добавлен:
16.08.2013
Размер:
11.09 Mб
Скачать

Chapter 24

void deserialize(const string& inData)

{

// Use an input stream to read the values back in the same order. istringstream inStream(inData);

if (!inStream.good()) {

cerr << “Error deserializing!” << endl; } else {

inStream >> mItemNumber; inStream >> mQuantity; inStream >> mCustomerNumber;

}

}

Simply by providing a way to convert an object to and from a string, you can transmit a representation of the object through a network. If your program makes heavy use of serialization, you may decide to include operator<< and operator>> in every class to provide serialization.

Remote Procedure Calls

A remote procedure call (RPC) refers to the conceptual behavior of calling a method or function that executes on another machine. In C++, this behavior is conceptual in nature because the language doesn’t provide any actual mechanism to call a function that isn’t in some way linked into the actual program binary. Rather, RPC in C++ generally involves calling a stub method locally that is actually hiding some networking code that obtains the result from a remote host.

By using the networking capabilities of your operating system and the serialization techniques shown earlier, it is possible to write your own RPC mechanism. You might define a class that represents a remote host, containing a number of stub methods that are actually executed on the remote host but return the result to the local caller. Such a class definition follows, containing stub methods that can be used to obtain information about the status of the remote machine or perform a restart.

class RemoteHost

{

public:

/**

*Creates a remote host, which is available at the

*given address

*/

RemoteHost(const string& inAddress);

int getNumConnectedUsers() const; int getAvailableMemory() const ; int getAvailableDiskSpace() const ;

void restartNow();

protected:

string mAddress;

};

700

Exploring Distributed Objects

The implementations of these methods would still be defined normally, but would make use of the networking library to obtain the actual result from the remote machine. The actual code would vary depending on the networking library, but here is some pseudocode that makes use of the networking functions defined in the previous section:

int RemoteHost::getAvailableMemory() const

{

//Send the string “getAvailableMemory()” to the remote host, instructing

//it to send back its available memory.

send(mAddress, “getAvailableMemory()”);

//Get the result from the remote host. string result = read();

//Convert result into an int.

//Return the int result.

}

The implementation on the remote host would need to parse and interpret the messages it receives in order to respond with the correct result. Here is some pseudocode for the remote host implementation.

void respondToRPC(const string& inRequestHost, const string& inMessage)

{

string response = “”;

// Look at the message to determine which operation is requested. if (inMessage == “getAvailableMemory()”) {

//Use a local function to get the available memory on this machine. int memAvail = getAvailMem();

//Convert the result into a string and put it in the response variable.

}else if (...) {

//Handle other messages.

}

// Send the response back to the requestor. send(inRequestHost, response);

}

The preceding pseudocode examples look simple, but a number of complications would quickly arise if you tried to turn it into actual production code. Some of the issues you would encounter are:

How do you deal with different data types, including complex types such as objects?

How do you deal with RPC calls that aren’t recognized by the remote machine?

How do you deal with versioning? What if some remote machines are upgraded and no longer respond in the same way as others?

How do you deal with network errors, missing hosts, overloaded networks, and so on?

How do you deal with platform issues, such as byte order differences?

For these reasons, as well as for the usual avoidance of reinventing the wheel, programmers typically use an existing RPC package that facilitates this type of interaction. The rest of this chapter covers CORBA and XML, two very different technologies that can both aid in RPC communication.

701