KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > utobcast > basic > Main


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Vivien Quema
22  * Contributor(s):
23  */

24
25 package utobcast.basic;
26
27 import java.net.InetAddress JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.objectweb.dream.adl.FactoryFactory;
32 import org.objectweb.dream.protocol.utobcast.ProcessMembership;
33 import org.objectweb.dream.util.Util;
34 import org.objectweb.fractal.adl.Factory;
35 import org.objectweb.fractal.api.Component;
36 import org.objectweb.fractal.api.control.ContentController;
37 import org.objectweb.fractal.rmi.registry.Registry;
38 import org.objectweb.fractal.util.Fractal;
39
40 import utobcast.TCPProcess;
41
42 /**
43  * A class that defines a main method to launch the example.
44  */

45 public class Main
46 {
47
48   /**
49    * Starts the example.
50    *
51    * @param args command line arguments
52    * @throws Exception if an error occurs.
53    */

54   public static void main(String JavaDoc[] args) throws Exception JavaDoc
55   {
56     Factory f = FactoryFactory.getFactory();
57     Map JavaDoc ctx = new HashMap JavaDoc();
58
59     // Create the composite
60
ctx.put("naming-service", Registry.getRegistry("localhost"));
61     Component composite = (Component) f.newComponent(
62         "utobcast.basic.UTOBcastExample(33333)", ctx);
63
64     // Retrieve the nodes
65
ContentController contentController = Fractal
66         .getContentController(composite);
67
68     //Create processes
69
Component[] nodes = contentController.getFcSubComponents();
70     TCPProcess[] tcpProcesses = new TCPProcess[nodes.length];
71     for (int i = 0; i < nodes.length; i++)
72     {
73       tcpProcesses[i] = new TCPProcess((short) i, InetAddress.getLocalHost(),
74           30000 + i);
75     }
76
77     // Configure each node
78
for (int i = 0; i < nodes.length; i++)
79     {
80       // Start the UTOBcastProtocol component
81
ContentController cc = Fractal.getContentController(nodes[i]);
82       Component utoBcastProtocol = Util.getComponentByName(cc,
83           "UTOBcastProtocol");
84       Fractal.getLifeCycleController(utoBcastProtocol).startFc();
85       // Configure membership
86
ProcessMembership processMembershipItf = (ProcessMembership) Util
87           .getComponentByName(cc, "UTOBcastProtocol/ProcessMembership")
88           .getFcInterface(ProcessMembership.ITF_NAME);
89       processMembershipItf.setLeader(tcpProcesses[0]);
90       processMembershipItf.setBackup(tcpProcesses[1]);
91       processMembershipItf.setMyself(tcpProcesses[i]);
92     }
93
94     // Retrieve the process membership component of the leader
95
ProcessMembership leaderProcessMembershipItf = (ProcessMembership) Util
96         .getComponentByName(contentController,
97             "0/UTOBcastProtocol/ProcessMembership").getFcInterface(
98             ProcessMembership.ITF_NAME);
99
100     // Retrieve the process membership component of the backup
101
ProcessMembership backupMembershipItf = (ProcessMembership) Util
102         .getComponentByName(contentController,
103             "1/UTOBcastProtocol/ProcessMembership").getFcInterface(
104             ProcessMembership.ITF_NAME);
105
106     for (int i = 0; i < tcpProcesses.length; i++)
107     {
108       leaderProcessMembershipItf.addProcess(tcpProcesses[i]);
109       backupMembershipItf.addProcess(tcpProcesses[i]);
110     }
111
112     // Start the test (note that some components have already been started)
113
Fractal.getLifeCycleController(composite).startFc();
114
115   }
116 }
Popular Tags