aboutsummaryrefslogtreecommitdiff
path: root/doc/html/guide/first.html
blob: 97ab74c7451db9c5bf7b90d7f632a8b4caa9a615 (plain)
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
<html>
<head>
  <title>your first Ivy java application </title>
</head>
<body>
  <table width=100% border=0 cellpadding=0 cellspacing=0><tr><th colspan=3 align=center>
  The Ivy java library guide</th></tr><tr>
  <td width=10% align=left valign=bottom><a href="javalib.html">Prev</a></td>
  <td width=80% align=center valign=bottom>
  <td width=10% align=right valign=bottom><a href="basic.html">Next</a></td>
  </tr></table><hr>

  <h1>Your first Ivy java application</h1>

  <p>We are going to write a "Hello world translater" for an Ivy bus. The
  application will subscribe to all messages starting
  with "Hello", and re-emit them after translating "Hello" into "Bonjour". In
  addition, the application will quit when it receives any message containing
  exactly "Bye".

  <h2>The code</h2>
  There is the code of <a href="ivyTranslater.java">ivyTranslater.java</a>:

<TABLE cellpadding=0 cellspacing=0 width=100%>
<tr BGcolor=#e0e0e0 TEXT=#000000><td>
<PRE>
<FONT color=#c000c0>import</FONT> fr.dgac.ivy.* ;

<FONT color=#008000>class</FONT> ivyTranslater <FONT color=#008000>implements</FONT> IvyMessageListener {

  <FONT color=#008000>private</FONT> Ivy bus;

  ivyTranslater() {
    <FONT color=#0000c0>// initialization</FONT>
    bus = <FONT color=#804000>new</FONT> Ivy(<FONT color=#c00000>&quot;IvyTranslater&quot;</FONT>,<FONT color=#c00000>&quot;Hello le monde&quot;</FONT>,<FONT color=#c00000>null</FONT>);
    bus.bindMsg(<FONT color=#c00000>&quot;^Hello(.*)&quot;</FONT>,<FONT color=#008000>this</FONT>);
    bus.bindMsg(<FONT color=#c00000>&quot;^Bye$&quot;</FONT>,<FONT color=#804000>new</FONT> IvyMessageListener() {
      <FONT color=#0000c0>// callback for </FONT><FONT color=#c00000>&quot;Bye&quot;</FONT><FONT color=#0000c0> message</FONT>
      <FONT color=#008000>public</FONT> <FONT color=#008000>void</FONT> receive(IvyClient client, String[] args) {System.exit(<FONT color=#c00000>0</FONT>);}
    });
    <FONT color=#804000>try</FONT> {
       <FONT color=#0000c0>// starts the bus on the default domain or IVY_DOMAIN property</FONT>
       bus.start(<FONT color=#c00000>null</FONT>);
    } <FONT color=#804000>catch</FONT> (IvyException ie) {
      System.err.println(<FONT color=#c00000>&quot;can't run the Ivy bus&quot;</FONT> + ie.getMessage());
    }
  }

  <FONT color=#0000c0>// callback associated to the </FONT><FONT color=#c00000>&quot;Hello&quot;</FONT><FONT color=#0000c0> messages</FONT><FONT color=#c00000>&quot;</FONT>
  <FONT color=#008000>public</FONT> <FONT color=#008000>void</FONT> receive(IvyClient client, String[] args) {
    bus.sendMsg(<FONT color=#c00000>&quot;Bonjour&quot;</FONT>+((args.length&gt;<FONT color=#c00000>0</FONT>)?args[<FONT color=#c00000>0</FONT>]:<FONT color=#c00000>&quot;&quot;</FONT>));
  }

  <FONT color=#008000>public</FONT> <FONT color=#008000>static</FONT> <FONT color=#008000>void</FONT> main(String args[]) { <FONT color=#804000>new</FONT> ivyTranslater(); }
}
</PRE>
</td></tr></table>
 
 <h2>Compiling</h2>
 <p>On a Unix computer, you should be able to compile the application with the
 following command :

<table cellpadding=0 cellspacing=0 width=100%>
<tr bgcolor=#e0e0e0 text=#000000><td>
$ javac ivyTranslater.java
<br>$
</td></tr></table>
 
 <h2>Testing</h2>
 <p>We are going to test our application with Probe. In a terminal window,
 launch ivyTranslater:

<table cellpadding=0 cellspacing=0 width=100%>
<tr bgcolor=#e0e0e0 text=#000000><td>
$ java ivyTranslater
<br>&nbsp;
</td></tr></table>

 <p>Then in another terminal window, launch Probe. You are then ready to
 start, Type "Hello Paul", and you should get "Bonjour Paul". Then Type "Bye",
 and your application should quit :

<table cellpadding=0 cellspacing=0 width=100%>
<tr bgcolor=#e0e0e0 text=#000000><td>
$ java fr.dgac.ivy.Probe '(.*)'
<br>you want to subscribe to (.*)
<br>broadcasting on 127.255.255.255:2010
<br>IvyTranslater connected 
<br>IvyTranslater subscribes to ^Bye$
<br>IvyTranslater subscribes to ^Hello(.*)
<br>IvyTranslater sent 'Hello le monde'
<br><b>Hello Paul</b>
<br>-&gt; Sent to 1 peers
<br>IvyTranslater sent 'Bonjour Paul'
<br><b>Bye</b>
<br>-&gt; Sent to 1 peers
<br>IvyTranslater disconnected 
<br>&lt;Ctrl-D&gt;
<br>$
</td></tr></table>

  <p><hr><table width=100% border=0 cellpadding=0 cellspacing=0><tr>
  <td width=33% align=left valign=top><a href="javalib.html">Prev</a></td>
  <td width=34% align=center valign=top><a href="index.html">Home</a></td>
  <td width=33% align=right valign=top><a href="basic.html">Next</a></td>
  </tr><tr>
  <td width=33% align=left valign=top>The Ivy java library</td>
  <td width=34% align=center valign=top>&nbsp;</td>
  <td width=33% align=right valign=top>Basic functions</a></td>
  </tr></table>
</body>
</html>