View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.test.hello;
20  
21  import java.rmi.RemoteException;
22  
23  import jgroup.core.ExternalGMIService;
24  import jgroup.core.GroupManager;
25  import jgroup.core.IID;
26  import jgroup.core.MemberId;
27  import jgroup.core.MembershipListener;
28  import jgroup.core.MembershipService;
29  import jgroup.core.View;
30  import jgroup.core.protocols.Anycast;
31  import jgroup.core.protocols.Multicast;
32  import jgroup.core.registry.RegistryService;
33  import jgroup.util.Network;
34  
35  /**
36   *  Simple Hello server aimed at demonstrating the capabilities of both
37   *  internal and external invocations.
38   *
39   *  @author Alberto Montresor
40   *  @author Hein Meling
41   *  @since Jgroup 1.0
42   */
43  public class HelloServer
44    implements Hello, InternalHello, MembershipListener
45  {
46  
47    ////////////////////////////////////////////////////////////////////////////////////////////
48    // Fields
49    ////////////////////////////////////////////////////////////////////////////////////////////
50  
51    /** Stores the answer for replying to external invocations */
52    private Answer answer;
53  
54    /** Reference to the partitionable group membership service */
55    private MembershipService membershipService;
56  
57    /** Reference to the external group method invocation service */
58    private ExternalGMIService externalGMIService;
59  
60    /** Reference to the internal hello group proxy (for group internal invocations) */
61    private InternalHello internalHello;
62  
63    /** The binding identifier for the servers stub in the dependable registry */
64    private IID bindId;
65  
66  
67    ////////////////////////////////////////////////////////////////////////////////////////////
68    // Main method (initialize the HelloServer object)
69    ////////////////////////////////////////////////////////////////////////////////////////////
70  
71    public static void main(String argv[])
72      throws Exception
73    {
74      HelloServer obj = new HelloServer();
75      /*
76       * Note that, at this point we do not exit since the dependable
77       * registry reference holds a strong reference to this
78       * <code>HelloServer</code> object, and thus prevents it from being
79       * garbage collected.  This again prevents the server from exiting
80       * at this point.  So, to stop the server from running we must
81       * either, use CTRL-C or unbind the helloserver reference in the
82       * dependable registry.
83       */
84    }
85  
86  
87    ////////////////////////////////////////////////////////////////////////////////////////////
88    // Constructor for the HelloServer
89    ////////////////////////////////////////////////////////////////////////////////////////////
90  
91    public HelloServer()
92      throws Exception
93    {
94      System.out.println("Server starting...");
95      /* Prepare the answer for replying to external invocations. */
96      answer = new Answer("Hello from " + Network.getLocalHostName());
97      System.out.println("Server is connected to a network...");
98  
99      /* Obtain the group manager */
100     GroupManager gm = GroupManager.getGroupManager(this);
101     System.out.println("Server obtained group manager");
102 
103     /* Obtain proxies for the services required by the HelloServer */
104     membershipService = (MembershipService) gm.getService(MembershipService.class);
105     externalGMIService = (ExternalGMIService) gm.getService(ExternalGMIService.class);
106     RegistryService registryService = (RegistryService) gm.getService(RegistryService.class);
107     internalHello = (InternalHello) gm.getService(InternalHello.class);
108 
109     /* Join the group and bind the server in the dependable registry */
110     membershipService.join(10);
111     bindId = registryService.bind(this);
112     System.out.println("Server joined group 10");
113     MemberId thisMember = membershipService.getMyIdentifier();
114     System.out.println("My memberId: " + thisMember + ", " + thisMember.getLocalId().getMemberNo());
115     System.out.println("Server ready and bound to the dependable registry");
116   }
117 
118 
119   ////////////////////////////////////////////////////////////////////////////////////////////
120   // Methods from the Hello interface (External Group Method Invocation)
121   ////////////////////////////////////////////////////////////////////////////////////////////
122 
123   @Anycast public Answer sayHello()
124     throws RemoteException
125   {
126     System.out.println("sayHello: returning " + answer);
127     System.out.println("sayHello identifier: " + externalGMIService.getIdentifier());
128     return answer;
129   }
130 
131   @Multicast public double doCompute()
132     throws RemoteException
133   {
134     System.out.println("Starting computatations ...");
135     long start = System.currentTimeMillis();
136     long limit = 100000l;
137     double dd = 0.0d;
138     double ret = 0.0d;
139     for (long l = -limit; l < limit; l++) {
140       dd = Math.sin(3.141576d*l)*l;
141       ret += dd*2.00d;
142     }
143     long end = System.currentTimeMillis();
144     System.out.println("Computations took: " + (end-start) + " msec");
145     return ret;
146   }
147 
148 
149   ////////////////////////////////////////////////////////////////////////////////////////////
150   // Methods from the InternalHello interface (Internal Group Method Invocation)
151   ////////////////////////////////////////////////////////////////////////////////////////////
152 
153   public Object time()
154     throws RemoteException
155   {
156     long time = System.currentTimeMillis();
157     System.out.println("Generating time: " + time);
158     return new Long(time);
159   }
160 
161 
162   ////////////////////////////////////////////////////////////////////////////////////////////
163   // Methods from MembershipListener
164   ////////////////////////////////////////////////////////////////////////////////////////////
165 
166   @AllowDuplicateViews public void viewChange(View view)
167   {
168     System.out.println("  ** HelloServer **" + view);
169 
170     try {
171       /*
172        * The time() method is defined in the InternalHello interface and
173        * is marked as a group internal method.  By definition, all group
174        * internal methods will return an array of values instead of a
175        * single value as with standard remote (or external group) method
176        * calls.
177        */
178       Object[] objs = (Object[]) internalHello.time();
179       for (int i = 0; i < objs.length; i++)
180         System.out.println("Time: " + objs[i]);
181       answer.setTime(objs);
182     } catch (Exception e) {
183       e.printStackTrace();
184     }
185   }
186 
187   public void prepareChange()
188   {
189     System.out.println("The current view is invalid; please await a new view...");
190   }
191 
192   public void hasLeft()
193   {
194     System.out.println("I have left the group");
195   }
196 
197 } // END HelloServer