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

Chapter 24

if (test.mName == test2.mName) {

cout

<< “Names are equivalent!” << endl;

} else {

 

cout

<< “ERROR: Names are not equivalent!” << endl;

}

 

if (test.mPriority == test2.mPriority) {

cout

<< “Priorities are equivalent!” << endl;

} else {

 

cout

<< “ERROR: Priorities are not equivalent!” << endl;

}

 

if (test.mData == test2.mData) {

cout

<< “Data is equivalent!” << endl;

} else {

 

cout

<< “ERROR: Data is not equivalent!” << endl;

}

 

XMLPlatformUtils::Terminate();

return 0;

}

Using the Distributed Object

Now that Simple objects can read themselves from XML and write themselves to XML, they are fully XML serializable. XML serialization is the foundation for using XML as a distributed object technology. The other piece to the puzzle is the transmission of XML serialized objects between different machine and applications.

Just as with the traditional serialization schemes described earlier in this chapter, you can use XML serialization with any network or data interchange technology that you wish. You can write a program that emails serialized objects around, or compresses them and sends them as binary data over a network.

Because XML is merely a syntax, the programmer is left to decide on the exact semantics of the XML content and the mechanism of its transmission.

SOAP (Simple Object Access Protocol)

One of the “killer apps” for XML is the exchange of data over a network. As you already know, XML is perfect for such applications because it is easy to work with and recognized by all platforms. The major downside is that applications that are exchanging data via XML need to be in agreement on the particular semantics of the XML data being exchanged. With only XML at your disposal, you couldn’t write an application that made RPC-style calls to somebody else’s application without obtaining the format of the XML they are expecting.

SOAP is an XML-based standard for exchanging data. It provides a standard way to make RPC-style requests, provide metadata about XML, represent simple and complex data types in XML (using XML schemas), and handle errors. By using SOAP-based XML as a data exchange format, applications can communicate without reinventing the wheel.

726

Exploring Distributed Objects

An Introduction to SOAP

This section introduces some of the terminology used in SOAP without getting into the nitty-gritty details of the syntax. The details of implementing SOAP applications are best left to SOAP-specific texts. Additionally, a number of emerging SOAP frameworks and hardware appliances spare programmers the details of the SOAP syntax by wrapping it in a programmatic or graphical interface. So, while you may have to look at raw SOAP data for debugging purposes, it’s becoming less likely that you’ll have to write it by hand.

All of the data in a SOAP message is contained in a SOAP Envelope. The envelope is divided into two parts — the SOAP Header and the SOAP Body. As you may have guessed, the SOAP Header contains metainformation about the message. For example, because XML is a plain text–readable format, it is highly susceptible to malicious changes as it moves through the network. The header can contain digital signatures that are used to verify the integrity of a SOAP message.

The contents of the SOAP Body vary depending on the style of SOAP being used. Document-style SOAP messages simply provide an XML payload in the SOAP Body. An application that wants to move XMLserialized data from one machine to another using the SOAP standard would most likely take advantage of Document-style SOAP. Here is an example of a Document-style SOAP message:

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/ “> <soap:Body>

<dialogue>

<sentence speaker=”Marni”>Let’s go get some ice cream.</sentence> <sentence speaker=”Scott”>After I’m done writing this C++

book.</sentence>

</dialogue>

</soap:Body>

</soap:Envelope>

RPC-style SOAP is a more structured type of SOAP message that is used to make requests to remote machines and receive responses. In an RPC-style request, the SOAP Body contains a description of the request being made on the remote machine, including parameters to the request. Here is a simple RPC request to a method that adds two numbers:

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/ “> <soap:Body>

<myNS:AddNumbers xmlns:myNS=”mynamespace”> <myNS:arg1>7</myNS:arg1>

<myNS:arg2>4</myNS:arg2>

</myNS:AddNumbers>

</soap:Body>

</soap:Envelope>

The SOAP Body of the response to an RPC-style request contains an XML element containing the results of the RPC call:

727

Chapter 24

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/ “> <soap:Body>

<myNS:AddNumbersResponse xmlns:myNS=”mynamespace”> <myNS:result>11</myNS:result>

</myNS:AddNumbersResponse>

</soap:Body>

</soap:Envelope>

While some of the syntax may still be a mystery, hopefully you have a good idea of what is contained in a SOAP message. You should also begin to see the power of using SOAP for distributed applications — using only simple XML, you can issue requests and receive responses from any application that speaks SOAP. No elaborate, expensive, or platform-specific technologies are required. SOAP also has an advantage over non-SOAP serialized XML because the semantics are specified. If two people were to write applications that communicated via serialized XML, they would have to agree on the names of attributes and elements. If they used SOAP, they could focus on the specifics of their application.

SOAP is quickly gaining popularity as a data exchange mechanism for businesses and for Web services. Many of the existing SOAP frameworks are written for Java (with the notable exception of Microsoft’s

.NET which tends to have C# in mind). However, there is nothing platform specific about SOAP. In fact, SOAP may be the perfect way to expose your C++ applications to a wider audience using other languages.

Summar y

In this chapter, you’ve seen how you can write new types of applications by using network technologies. You’ve learned about the mechanisms for distributed communication — serialization and remote procedure calls. You’ve also been exposed to several ways to implement these technologies, including custom serialization, CORBA, XML, and SOAP.

Distributed computing truly opens up a new world of possibilities for your applications. Now that you know the concepts and the different technologies, you have the basic requirements for the next distributed killer app.

728

Incorporating Techniques

and Frameworks

One of the major themes of this book has been the adoption of reusable techniques and patterns. As a programmer, you tend to face similar problems repeatedly. With an arsenal of diverse approaches, you can save yourself time by applying the proper technique to a given problem.

This chapter focuses on design techniques — C++ idioms that aren’t necessarily built-in parts of the language, but are nonetheless frequently used. The first part of this chapter covers the language features in C++ that are common but involve easy-to-forget syntax. Most of this material is a review, but it is a useful reference tool when the syntax escapes you. The topics covered include:

Starting a class from scratch

Extending a class with a subclass

Throwing and catching exceptions

Reading from a file

Writing to a file

Defining a template class

The second part of this chapter focuses on higher-level techniques that build upon C++ language features. These techniques offer a better way to accomplish everyday programming tasks. Topics include:

Smart pointers with reference counting

The double-dispatch technique

Mix-in classes