1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<META name="GENERATOR" content="hevea 1.10">
<LINK rel="stylesheet" type="text/css" href="omniORB.css">
<TITLE>Interface Type Checking</TITLE>
</HEAD>
<BODY >
<A HREF="omniORB006.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
<A HREF="omniORB008.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
<HR>
<H1 CLASS="chapter"><A NAME="htoc88">Chapter 7</A>  Interface Type Checking</H1><P>
<A NAME="ch_intf"></A></P><P>This chapter describes the mechanism used by omniORB to ensure type
safety when object references are exchanged across the network. This
mechanism is handled completely within the ORB. There is no
programming interface visible at the application level. However, for
the sake of diagnosing the problem when there is a type violation, it
is useful to understand the underlying mechanism in order to interpret
the error conditions reported by the ORB.</P><H2 CLASS="section"><A NAME="toc35"></A><A NAME="htoc89">7.1</A>  Introduction</H2><P>In GIOP/IIOP, an object reference is encoded as an Interoperable
Object Reference (IOR) when it is sent across a network connection.
The IOR contains a Repository ID (RepoId) and one or more
communication profiles. The communication profiles describe where and
how the object can be contacted. The RepoId is a string which uniquely
identifies the IDL interface of the object.</P><P>Unless the <TT>ID</TT> pragma is specified in the IDL, the ORB generates
the RepoId string in the so-called OMG IDL Format<SUP><A NAME="text16" HREF="#note16">1</A></SUP>. For instance, the RepoId for the <TT>Echo</TT>
interface used in the examples of chapter <A HREF="omniORB002.html#chap:basic">2</A> is
<TT>IDL:Echo:1.0</TT>.</P><P>When interface inheritance is used in the IDL, the ORB always sends the
RepoId of the most derived interface. For example:</P><DIV CLASS="lstlisting"> <I>// IDL</I>
<B>interface</B> A {
...
};
<B>interface</B> B : A {
...
};
<B>interface</B> C {
<B>void</B> op(<B>in</B> A arg);
};</DIV><DIV CLASS="lstlisting"> <I>// C++</I>
C_ptr server;
B_ptr objB;
A_ptr objA = objB;
server->op(objA); <I>// Send B as A</I></DIV><P>In the example, the operation <TT>C::op()</TT> accepts an object reference
of type <TT>A</TT>. The real type of the reference passed to <TT>C::op()</TT>
is <TT>B</TT>, which inherits from <TT>A</TT>. In this case, the RepoId of
<TT>B</TT>, and not that of <TT>A</TT>, is sent across the network.</P><P>The GIOP/IIOP specification allows an ORB to send a null string in the
RepoId field of an IOR. It is up to the receiving end to work out the
real type of the object. omniORB never sends out null strings as
RepoIds, but it may receive null RepoIds from other ORBs. In that
case, it will use the mechanism described below to ensure type safety.</P><H2 CLASS="section"><A NAME="toc36"></A><A NAME="htoc90">7.2</A>  Interface Inheritance</H2><P>When the ORB receives an IOR of interface type B when it expects the
type to be A, it must find out if B inherits from A. When the ORB has
no local knowledge of the type B, it must work out the type of B
dynamically.</P><P>The CORBA specification defines an Interface Repository (IR) from
which IDL interfaces can be queried dynamically. In the above
situation, the ORB could contact the IR to find out the type of B.
However, this approach assumes that an IR is always available and
contains the up-to-date information of all the interfaces used in the
domain. This assumption may not be valid in many applications.</P><P>An alternative is to use the <TT>_is_a()</TT> operation to work out the
actual type of an object. This approach is simpler and more robust
than the previous one because no 3rd party is involved, so this is
what omniORB does.</P><DIV CLASS="lstlisting"><B>class</B> Object{
CORBA::Boolean _is_a(<B>const</B> <B>char</B>* type_id);
};</DIV><P>The <TT>_is_a()</TT> operation is part of the <TT>CORBA::Object</TT>
interface and must be implemented by every object. The input argument
is a RepoId. The function returns true(1) if the object is really an
instance of that type, including if that type is a base type of the
most derived type of that object.</P><P>In the situation above, the ORB would invoke the <TT>_is_a()</TT>
operation on the object and ask if the object is of type A
<EM>before</EM> it processes any application invocation on the object.</P><P>Notice that the <TT>_is_a()</TT> call is <EM>not</EM> performed when the IOR
is unmarshalled. It is performed just prior to the first application
invocation on the object. This leads to some interesting failure modes
if B reports that it is not an A. Consider the following example:</P><DIV CLASS="lstlisting"><I>// IDL</I>
<B>interface</B> A { ... };
<B>interface</B> B : A { ... };
<B>interface</B> D { ... };
<B>interface</B> C {
A op1();
<B>Object</B> op2();
};</DIV><DIV CLASS="lstlisting"><FONT SIZE=1> 1</FONT> <I>// C++</I>
<FONT SIZE=1> 2</FONT> C_ptr objC;
<FONT SIZE=1> 3</FONT> A_ptr objA;
<FONT SIZE=1> 4</FONT> CORBA::Object_ptr objR;
<FONT SIZE=1> 5</FONT>
<FONT SIZE=1> 6</FONT> objA = objC->op1();
<FONT SIZE=1> 7</FONT> (<B>void</B>) objA->_non_existent();
<FONT SIZE=1> 8</FONT>
<FONT SIZE=1> 9</FONT> objR = objC->op2();
<FONT SIZE=1> 10</FONT> objA = A::_narrow(objR);</DIV><P>If the stubs of A,B,C,D are linked into the executable and:</P><DL CLASS="description"><DT CLASS="dt-description">
<B>Case 1</B></DT><DD CLASS="dd-description"> <TT>C::op1()</TT> and <TT>C::op2()</TT> return a B. Lines 6–10
complete successfully. The remote object is only contacted at line 7.</DD><DT CLASS="dt-description"><B>Case 2</B></DT><DD CLASS="dd-description"> <TT>C::op1()</TT> and <TT>C::op2()</TT> return a D. This condition
only occurs if the runtime of the remote end is buggy. Even though the
IDL definitions show that D is not derived from A, omniORB gives it
the benefit of the doubt, in case it actually has a more derived
interface that is derived from both A and D. At line 7, the object is
contacted to ask if it is an A. The answer is no, so a
<TT>CORBA::INV_OBJREF</TT> exception is raised. At line 10, the narrow
operation will fail, and objA will be set to nil.
</DD></DL><P>If only the stubs of A are linked into the executable and:</P><DL CLASS="description"><DT CLASS="dt-description">
<B>Case 1</B></DT><DD CLASS="dd-description"> <TT>C::op1()</TT> and <TT>C::op2()</TT> return a B. Lines 6–10
complete successfully. When lines 7 and 10 are executed, the object is
contacted to ask if it is an A.</DD><DT CLASS="dt-description"><B>Case 2</B></DT><DD CLASS="dd-description"> <TT>C::op1()</TT> and <TT>C::op2()</TT> return a D. This condition
only occurs if the runtime of the remote end is buggy. Line 6
completes and no exception is raised. At line 7, the object is
contacted to ask if it is an A. If the answer is no, a
<TT>CORBA::INV_OBJREF</TT> exception is raised. At line 10, the narrow
operation will fail, and objA will be set to nil.
</DD></DL><HR CLASS="footnoterule"><DL CLASS="thefootnotes"><DT CLASS="dt-thefootnotes">
<A NAME="note16" HREF="#text16">1</A></DT><DD CLASS="dd-thefootnotes">For further
details of the repository ID formats, see section 10.6 in the CORBA
2.6 specification.
</DD></DL>
<HR>
<A HREF="omniORB006.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
<A HREF="omniORB008.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
|