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.registry.RegistryService;
31  import jgroup.util.Network;
32  
33  /**
34   *  Simple Hello server aimed at demonstrating the capabilities of both
35   *  internal and external invocations.
36   *
37   *  @author Alberto Montresor
38   *  @author Hein Meling
39   *  @since Jgroup 1.0
40   */
41  public class ExperimentalHelloServer
42    implements Hello, InternalHello, MembershipListener
43  {
44  
45    ////////////////////////////////////////////////////////////////////////////////////////////
46    // Fields
47    ////////////////////////////////////////////////////////////////////////////////////////////
48  
49    /** Stores the answer for replying to external invocations */
50    private Answer answer;
51  
52    /** Reference to the partitionable group membership service */
53    private MembershipService membershipService;
54  
55    /** Reference to the external group method invocation service */
56    private ExternalGMIService externalGMIService;
57  
58    /** Reference to the internal hello group proxy (for group internal invocations) */
59    private InternalHello internalHello;
60  
61    /** The binding identifier for the servers stub in the dependable registry */
62    private IID bindId;
63  
64    /** This member's member identifier */
65    private MemberId thisMember;
66  
67  
68    ////////////////////////////////////////////////////////////////////////////////////////////
69    // Main method (initialize the HelloServer object)
70    ////////////////////////////////////////////////////////////////////////////////////////////
71  
72    public static void main(String argv[])
73      throws Exception
74    {
75      ExperimentalHelloServer obj = new ExperimentalHelloServer();
76      /*
77       * Note that, at this point we do not exit since the dependable
78       * registry reference holds a strong reference to this
79       * <code>HelloServer</code> object, and thus prevents it from being
80       * garbage collected.  This again prevents the server from exiting
81       * at this point.  So, to stop the server from running we must
82       * either, use CTRL-C or unbind the helloserver reference in the
83       * dependable registry.
84       */
85    }
86  
87  
88    ////////////////////////////////////////////////////////////////////////////////////////////
89    // Constructor for the HelloServer
90    ////////////////////////////////////////////////////////////////////////////////////////////
91  
92    public ExperimentalHelloServer()
93      throws Exception
94    {
95      System.out.println("Server starting...");
96      /* Prepare the answer for replying to external invocations. */
97      answer = new Answer("Hello from " + Network.getLocalHostName());
98      System.out.println("Server is connected to a network...");
99  
100     /* Obtain the group manager */
101     GroupManager gm = GroupManager.getGroupManager(this);
102     System.out.println("Server obtained group manager");
103 
104     /* Obtain proxies for the services required by the HelloServer */
105     membershipService = (MembershipService) gm.getService(MembershipService.class);
106     externalGMIService = (ExternalGMIService) gm.getService(ExternalGMIService.class);
107     RegistryService registryService = (RegistryService) gm.getService(RegistryService.class);
108     internalHello = (InternalHello) gm.getService(InternalHello.class);
109 
110     /* Join the group and bind the server in the dependable registry */
111     membershipService.join(12);
112     System.out.println("Server joined group 12");
113     thisMember = membershipService.getMyIdentifier();
114     System.out.println("My memberId: " + thisMember + ", " + thisMember.getLocalId().getMemberNo());
115     bindId = registryService.bind(this);
116     System.out.println("Server ready and bound to the dependable registry");
117 
118     /*
119      * Code to test the membership.leave() method; invoked after
120      * being member of the group for some 15 seconds.
121      */
122     Thread.sleep(15000);
123     System.out.println("I'm leaving the group");
124     membershipService.leave();
125   }
126 
127 
128   ////////////////////////////////////////////////////////////////////////////////////////////
129   // Methods from the Hello interface (External Group Method Invocation)
130   ////////////////////////////////////////////////////////////////////////////////////////////
131 
132   public Answer sayHello()
133     throws RemoteException
134   {
135     System.out.println("sayHello: returning " + answer);
136     System.out.println("sayHello indentifier: " + externalGMIService.getIdentifier());
137     return answer;
138   }
139 
140 
141   ////////////////////////////////////////////////////////////////////////////////////////////
142   // Methods from the InternalHello interface (Internal Group Method Invocation)
143   ////////////////////////////////////////////////////////////////////////////////////////////
144 
145   public Object time()
146     throws RemoteException
147   {
148     long tmp = System.currentTimeMillis();
149     System.out.println("Generating time: " + tmp);
150     return new Long(tmp);
151   }
152 
153 
154   ////////////////////////////////////////////////////////////////////////////////////////////
155   // Methods from MembershipListener
156   ////////////////////////////////////////////////////////////////////////////////////////////
157 
158   public void viewChange(View view)
159   {
160     System.out.println("--- HelloServer.viewChange: ---" + view);
161   }
162 
163   public void prepareChange()
164   {
165     System.out.println("The current view is invalid; please await a new view...");
166   }
167 
168   public void hasLeft()
169   {
170     System.out.println("I have left the group");
171   }
172 
173 
174   public double doCompute() throws RemoteException
175   {
176     return 0;
177   }
178 
179 } // END ExperimentalHelloServer