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.jini;
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.relacs.gm.TimestampService;
28  import jgroup.util.Network;
29  
30  import org.apache.log4j.Logger;
31  
32  import com.sun.jini.start.LifeCycle;
33  
34  
35  /**
36   * Simple Hello server aimed at demonstrating the capabilities of
37   * Greg.
38   *
39   * @author  Alberto Montresor
40   * @since   Jgroup 1.0
41   */
42  public class HelloServer
43    implements Hello, MembershipListener
44  {
45    ////////////////////////////////////////////////////////////////////////////////////////////
46    // Logger
47    ////////////////////////////////////////////////////////////////////////////////////////////
48  
49    /** Obtain logger for this class */
50    private static final Logger log = Logger.getLogger(HelloServer.class);
51  
52  
53    ////////////////////////////////////////////////////////////////////////////////////////////
54    // Fields
55    ////////////////////////////////////////////////////////////////////////////////////////////
56  
57    /** Message returned by sayHello */
58    private String answer;
59  
60    /** Service proxies */
61    private TimestampService timestampService;
62    
63  
64    ////////////////////////////////////////////////////////////////////////////////////////////
65    // Constructor and main
66    ////////////////////////////////////////////////////////////////////////////////////////////
67  
68    /**
69     * Constructor compatible with the jini service starter. 
70     *
71     * @param configArgs <code>String</code> array whose elements are
72     *                   the arguments to use when creating the server.
73     * @param lifeCycle  instance of <code>LifeCycle</code> that, if
74     *                   non-<code>null</code>, will cause this object's
75     *                   <code>unregister</code> method to be invoked during
76     *                   shutdown to notify the service starter framework that
77     *                   the reference to this service's implementation can be
78     *                   'released' for garbage collection. A value of
79     *                   <code>null</code> for this argument is allowed.
80     *
81     * @throws Exception If there was a problem initializing the service.
82     */
83    public HelloServer(String[] configArgs, LifeCycle lifeCycle) 
84      throws Exception
85    {
86      /*
87       * Prepares answer message
88       */
89      answer = new String("Hello from " + Network.getLocalHostName());
90  
91      /*
92       * Obtains the group manager
93       */
94      GroupManager gm = GroupManager.getGroupManager(this);
95  
96      /*
97       * Obtains the service proxies
98       */
99  //    timestampService = (TimestampService) gm.getService(TimestampService.class);
100   }
101 
102 
103   ////////////////////////////////////////////////////////////////////////////////////////////
104   // Methods from Hello
105   ////////////////////////////////////////////////////////////////////////////////////////////
106 
107   /**
108    *  External method
109    */
110    public String sayHello()
111     throws RemoteException
112   {
113     log.debug("sayHello: returning " + answer);
114     return answer;
115   }
116 
117 
118   /**
119    * Returns the global timestamp
120    */
121   public long getGlobalTimestamp() 
122     throws RemoteException
123   {
124     return 0;
125 //    return timestampService.getTimestampCounter();   
126   }
127 
128 
129   ////////////////////////////////////////////////////////////////////////////////////////////
130   // Methods from MembershipListener
131   ////////////////////////////////////////////////////////////////////////////////////////////
132 
133   public void viewChange(View v)
134   {
135     log.debug("View id: " + v.getVid());
136     MemberId[] members = v.getMembers();
137     for (int i=0; i < members.length; i++)
138       log.debug("Member " + i + ": " + members[i]);
139   }
140 
141   public void prepareChange()
142   {
143   }
144 
145   public void hasLeft()
146   {
147   }
148 } // END HelloServer
149