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.GroupManager;
24  import jgroup.core.MemberId;
25  import jgroup.core.MembershipListener;
26  import jgroup.core.View;
27  import jgroup.core.protocols.Multicast;
28  import jgroup.util.Network;
29  
30  /**
31   *  Simple Hello server aimed at demonstrating the capabilities of both
32   *  internal and external invocations.  This server is modified to be used
33   *  with the automatic join and bind of the PGMS and RegistryLayer, and to
34   *  test things like the LeaseLayer and NotifyLayer.
35   *
36   *  @author Alberto Montresor
37   *  @author Hein Meling
38   *  @since Jgroup 1.0
39   */
40  public class LeaseServer
41    implements Hello, InternalHello, MembershipListener
42  {
43  
44    ////////////////////////////////////////////////////////////////////////////////////////////
45    // Fields
46    ////////////////////////////////////////////////////////////////////////////////////////////
47  
48    /** Stores the answer for replying to external invocations */
49    private Answer answer;
50  
51    /** The group manager reference */
52    private GroupManager gm = null;
53  
54    /** Reference to the internal hello group proxy (for group internal invocations) */
55    private InternalHello internalHello;
56  
57  
58    ////////////////////////////////////////////////////////////////////////////////////////////
59    // Main method (initialize the LeaseServer object)
60    ////////////////////////////////////////////////////////////////////////////////////////////
61  
62    public static void main(String argv[])
63      throws Exception
64    {
65      LeaseServer obj = new LeaseServer();
66      /*
67       * Note that, at this point we do not exit since the dependable
68       * registry reference holds a strong reference to this
69       * <code>LeaseServer</code> object, and thus prevents it from being
70       * garbage collected.  This again prevents the server from exiting
71       * at this point.  So, to stop the server from running we must
72       * either, use CTRL-C or unbind the helloserver reference in the
73       * dependable registry.
74       */
75    }
76  
77  
78    ////////////////////////////////////////////////////////////////////////////////////////////
79    // Constructor for the LeaseServer
80    ////////////////////////////////////////////////////////////////////////////////////////////
81  
82    public LeaseServer()
83      throws Exception
84    {
85      System.out.println("Server starting...");
86      /* Prepare the answer for replying to external invocations. */
87      answer = new Answer("Hello from " + Network.getLocalHostName());
88  
89      /* Obtain the group manager */
90      gm = GroupManager.getGroupManager(this);
91      System.out.println("Server obtained group manager");
92    }
93  
94  
95    ////////////////////////////////////////////////////////////////////////////////////////////
96    // Methods from the Hello interface (External Group Method Invocation)
97    ////////////////////////////////////////////////////////////////////////////////////////////
98  
99    public Answer sayHello()
100     throws RemoteException
101   {
102     System.out.println("sayHello: returning " + answer);
103     return answer;
104   }
105 
106   @Multicast public double doCompute() throws RemoteException
107   {
108     return 0;
109   }
110 
111 
112   ////////////////////////////////////////////////////////////////////////////////////////////
113   // Methods from the InternalHello interface (Internal Group Method Invocation)
114   ////////////////////////////////////////////////////////////////////////////////////////////
115 
116   public Object time()
117     throws RemoteException
118   {
119     long tmp = System.currentTimeMillis();
120     System.out.println("Generating time: " + tmp);
121     return new Long(tmp);
122   }
123 
124 
125   ////////////////////////////////////////////////////////////////////////////////////////////
126   // Methods from MembershipListener
127   ////////////////////////////////////////////////////////////////////////////////////////////
128 
129   public void viewChange(View view)
130   {
131     if (internalHello == null) {
132       /* Obtain the reference for this groups group manager */
133       gm = GroupManager.getGroupManager(view.getGid());
134       /* Obtain proxy for the internal hello service required by the LeaseServer */
135       internalHello = (InternalHello) gm.getService(InternalHello.class);
136     }
137 
138     System.out.println("View id: " + view.getVid());
139     MemberId[] members = view.getMembers();
140     for (int i = 0; i < members.length; i++)
141       System.out.println("Member[" + i + "]: " + members[i]);
142 
143     try {
144       /*
145        * The time() method is defined in the InternalHello interface and
146        * is marked as a group internal method.  By definition, all group
147        * internal methods will return an array of values instead of a
148        * single value as with standard remote (or external group) method
149        * calls.
150        */
151       Object[] objs = (Object[]) internalHello.time();
152       for (int i = 0; i < objs.length; i++)
153         System.out.println("Time: " + objs[i]);
154       answer.setTime(objs);
155     } catch (Exception e) {
156       e.printStackTrace();
157     }
158   }
159 
160   public void prepareChange()
161   {
162   }
163 
164   public void hasLeft()
165   {
166   }
167 
168 } // END LeaseServer