File: MnesiaCorbaSessionExamples.html

package info (click to toggle)
erlang-doc-html 1%3A10.b.1a-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 22,488 kB
  • ctags: 9,933
  • sloc: erlang: 505; ansic: 323; perl: 61; sh: 45; makefile: 39
file content (351 lines) | stat: -rw-r--r-- 11,881 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.2 -->
<HTML>
<HEAD>
  <TITLE>CORBA Session Examples</TITLE>
  <SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
      ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="5"><!-- Empty --></A>
<H2>5 CORBA Session Examples</H2>
<A NAME="5.1"><!-- Empty --></A>
<H3>5.1 A CORBA Session in Erlang</H3>

<P> Following is a simplified example of a client written in Erlang to access <CODE>Mnesia</CODE> via
the CORBA session interface. The type definitions are the same as in the 
<A HREF="MnesiaSessionExamples.html#exdatadef">previous chapter</A>.

<P>To begin with include the necessary files.
<PRE>
%% Include to get the mnesia types, used in create table
-include_lib(&#34;mnesia_session/include/mnesia.hrl&#34;).   

%% Include to get the corba types i.e. any
-include_lib(&#34;orber/include/corba.hrl&#34;).

%% Include my own types
-include(&#34;persons.hrl&#34;).  

</PRE>

<P>Find the connector and create a private session.
<PRE>
start_corba_session(Host, Port) -&#62;
    %% Lookup the inital corba name sever
    Addr = &#34;iiop://&#34; ++ atom_to_list(Host) ++ &#34;:&#34; ++ integer_to_list(Port),
    NS = corba:resolve_initial_references_remote(&#34;NameService&#34;, [Addr]),

    %% Create a corba name object 
    NC = lname_component:set_id(lname_component:create(),
                                &#34;mnesia_corba_connector&#34;),    
    N = lname:insert_component(lname:create(), 1, NC),

    %% Lookup the object reference to the factory mnesia_corba_connector
    Cok = 'CosNaming_NamingContext':resolve(NS, N),

    %% Use the factory to create a session
    Sok = mnesia_corba_connector:connect(Cok),
    {Cok, Sok}.

</PRE>

<P> In Erlang, objects are represented as processes. To access the method 
of the object, call the method where the first argument is the object 
reference, as in <CODE>mnesia_connector:connect(ConnPid)</CODE>. All functions
in the IDL specification have an additional first argument which is 
the object reference, more information is given in the <CODE>IC</CODE> and <CODE>Orber</CODE> 
documentation.

<P> To create a table with the structure defined in the IDL definition, we 
use the function create_person_table. 


<P>Define the table properties and call the function:

<PRE>
create_person_table(ObjKey) -&#62;

    %% Define the table properties
    Attrs = [atom_to_list(F) || F &#60;- record_info(fields, persons_person)],
    TabDef = #mnesia_TableDef{type = bag, mode = read_write, 
                              ram_copies = [],
                              disc_copies = [], 
                              disc_only_copies = [], 
                              index_list = [4],  %% Index on married_to
                              attributes = Attrs, 
                              %% NOTE that the record name must be exactly 
                              %% the same as the name of the 
                              %% structure/record to be used.
                              record_name = &#34;persons_person&#34;},    

    Res = mnesia_corba_session:create_table(ObjKey, &#34;persons&#34;, TabDef),
    case Res of 
        {ok, &#34;&#34;} -&#62;    
            ok;
        Else -&#62;
            io:format(&#34;ERROR: ~s~n&#34;, [Else])
    end.

</PRE>

<P> In this example the insert and a read operation looks like: 

<PRE>
insert_person(SessionKey, Name, Sex, Age, Phone, Mt, Ch) -&#62;
    Data = #persons_data{sex = Sex, age = Age, phone = Phone},
    Person = #persons_person{name = Name, personData = Data, 
                             married_to = Mt, children = Ch},
    
    Any = #any{typecode = persons_person:tc(), value = Person},
    {ok, &#34;&#34;} = mnesia_corba_session:dirty_write(SessionKey, &#34;persons&#34;, Any),
    Person.

</PRE>

<PRE>
get_person(SessionKey, Name) -&#62;
    Obj = #any{typecode = {tk_string, 0}, value = Name},
    {ok, RObjs, &#34;&#34;} = 
        mnesia_corba_session:dirty_read(SessionKey, &#34;persons&#34;, Obj),
    RObj = hd(RObjs),
    RObj#any.value.

</PRE>

<P> Notice the usage of the <CODE>any</CODE> type in both the read and write operations. 
<CODE>erlang::term</CODE> in the IDL specification is represented as the 
CORBA type <CODE>any</CODE> when using the CORBA session interface. The
<CODE>any</CODE> type is represented as a 2-tuple containing type-code and
value, the user must in Erlang insert the type-code.
<A NAME="5.2"><!-- Empty --></A>
<H3>5.2 A CORBA Session in Java</H3>

<P>This example is the same but using Java and OrbixWeb.
<P>For this example to work,
the following specifications need to be compiled:

<P>
<UL>

<LI>
compilation of the user defined IDL specifications <FONT SIZE="-2">(<CODE>UNIX: , Windows: </CODE>)</FONT>
</LI>


<LI>
${OTP_ROOT}/lib/mnesia_session-Version/src/mnesia_corba_session.idl <FONT SIZE="-2">(<CODE>UNIX: , Windows: </CODE>)</FONT>
</LI>


<LI>
${OTP_ROOT}/lib/ic-Version/include/erlang.idl <FONT SIZE="-2">(<CODE>UNIX: , Windows: </CODE>)</FONT>
</LI>


<LI>
${OTP_ROOT}/lib/orber-Version/COSS/CosNaming/cos_naming.idl <FONT SIZE="-2">(<CODE>UNIX: , Windows: </CODE>)</FONT>
</LI>


<LI>
${OTP_ROOT}/lib/orber-Version/examples/Stack/InitialReferences.idl <FONT SIZE="-2">(<CODE>UNIX: , Windows: </CODE>)</FONT>
</LI>


</UL>

<P>Import CORBA and CosNaming Classes:
<PRE>
import CosNaming._NamingContextRef;
import CosNaming.Name;
import IE.Iona.Orbix2._CORBA;
import IE.Iona.Orbix2.CORBA.*;

</PRE>

<P>Find the connector and create a private session.
<PRE>
    public static mnesia._corba_sessionRef 
        start_corba_session(String args[])
    {
        mnesia._corba_sessionRef mcsRef = null;
        mnesia._corba_connectorRef mccRef = null;
        CORBA._InitialReferencesRef init;
        _NamingContextRef nsContext;
        Name name;
        _ObjectRef initRef, nsRef, objRef;
        Orber.InitialReference ir = new Orber.InitialReference();
        
        String srvHost = new String(args[0]);
        Integer srvPort = new Integer(args[1]);
        try
        {
            // For an explanation about initial reference handling see 
            // the &#34;Interoperable Naming Service&#34; specification.
                
            // Create Initial reference (objectkey &#34;INIT&#34;).
            String s = ir.stringified_ior(srvHost, srvPort.intValue());
            initRef = _CORBA.Orbix.string_to_object(s);
            init = CORBA.InitialReferences._narrow(initRef);
            // Fetch name service reference.
            nsRef = init.get(&#34;NameService&#34;);
            nsContext = CosNaming.NamingContext._narrow(nsRef);
            // Create a name
            name = new Name(1);
            name.buffer[0] = 
                new CosNaming.NameComponent(&#34;mnesia_corba_connector&#34;, &#34;&#34;);
            try
            {
                objRef = nsContext.resolve(name);
            }
            catch(UserException n)
            {
                System.out.println(&#34;Unexpected exception: &#34; + n.toString());
                return null;
            }
            mccRef = mnesia.corba_connector._narrow(objRef);
                
            // Create and return the session reference
            mcsRef = mccRef.connect();
            return mcsRef;
        }
        catch(SystemException se)
        {
            System.out.println(&#34;Unexpected exception: &#34; + se.toString());
            se.printStackTrace();
            return null;
        }
    }

</PRE>

<P>Create the person table
<PRE>
    public static void create_person_table(mnesia._corba_sessionRef mcsRef)
    {
        String name = &#34;persons&#34;;
        mnesia.TableDef def = new mnesia.TableDef();
        def.type = mnesia.SetOrBag.bag;
        def.mode = mnesia.AccessMode.read_write;
        def.ram_copies = new mnesia.NodeList(0);
        def.disc_copies = new mnesia.NodeList(0);
        def.disc_only_copies = new mnesia.NodeList(0);
        mnesia.Indices idxs = new mnesia.Indices(1);
        idxs.buffer[0] = 4;
        def.index_list = idxs;
        mnesia.AttrNames attrs = new mnesia.AttrNames(4);
        attrs.buffer[0] = &#34;name&#34;;
        attrs.buffer[1] = &#34;personData&#34;;
        attrs.buffer[2] = &#34;married_to&#34;;
        attrs.buffer[3] = &#34;children&#34;;
        def.attributes = attrs;
        def.record_name = &#34;persons_person&#34;; // The used IDL type

        StringHolder reason;
        reason = new StringHolder();
        try
        {
            if(mnesia.Status.ok != mcsRef.create_table(name, def, reason))
                System.out.println(&#34;Create Table Error &#34; + reason.value); 
        }
        catch( SystemException se)
        {
            System.out.println(&#34;Unexpected exception: &#34; + se.toString());
            return;
        } 
    }

</PRE>

<P>The insert operation. Observe the encapsulation of person in the 
<CODE>mnesia.Record</CODE>. Java or OrbixWeb will handle the type encoding.

<PRE>
    public static void insert_person(mnesia._corba_sessionRef mcsRef,
                                     String name,
                                     int sex,
                                     int age,
                                     int phone,
                                     String mt,
                                     _sequence_String children)
    {
        persons.data data;
        data = new persons.data(sex, age, phone);
        persons.person person = new persons.person();
        person.name = name;
        person.personData = data;
        person.married_to = mt;
        person.children = children;
        
        try
        {
            StringHolder reason = new StringHolder();       
            mnesia.Record object = new mnesia.Record();
            object.insert(person);

            if(mnesia.Status.ok != mcsRef.dirty_write(&#34;persons&#34;, object, reason))
                System.out.println(&#34;Insert person Error &#34; + reason.value);
        }
        catch(SystemException se)
        {
            System.out.println(&#34;Unexpected exception: &#34; + se.toString());
            return;
        }
    }

</PRE>

<P>A read operation. Observe the extraction of person from the received 
<CODE>mnesia.Recordlist</CODE> and the <CODE>key</CODE> object handling.

<PRE>
    public static persons.person 
        get_person(mnesia._corba_sessionRef mcsRef, String name)
    {   
        try
        {
            StringHolder reason = new StringHolder();       
            mnesia.Key key = new mnesia.Key();
            mnesia.Recordlist res = new mnesia.Recordlist();
            key.insertString(name);
                
            if(mnesia.Status.ok == mcsRef.dirty_read(&#34;persons&#34;, 
                                                     key, res, reason))
            {
                if(res.length &#62; 0) 
                {
                    persons.person rec1 = new persons.person();
                    res.buffer[0].extract(rec1);
                    return rec1;
                }
                else
                    return null;                            
            }
            else
            {
                System.out.println(&#34;Insert person Error &#34; + reason.value);
                return null;
            }
        }
        catch(SystemException se)
        {
            System.out.println(&#34;Unexpected exception: &#34; + se.toString());
            return null;
        }       
    }

</PRE>
<CENTER>
<HR>
<SMALL>
Copyright &copy; 1991-2004
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>