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.Atomic;
31  import jgroup.core.registry.RegistryService;
32  import jgroup.util.Network;
33  
34  /**
35   *  Simple Hello server aimed at demonstrating the capabilities of atomic
36   *  external method invocations.
37   *
38   *  @author Hein Meling
39   *  @since Jgroup 1.0
40   */
41  public class AtomicHelloServer
42    implements Hello, 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    /** The binding identifier for the servers stub in the dependable registry */
59    private IID bindId;
60  
61  
62    ////////////////////////////////////////////////////////////////////////////////////////////
63    // Main method (initialize the HelloServer object)
64    ////////////////////////////////////////////////////////////////////////////////////////////
65  
66    public static void main(String argv[])
67      throws Exception
68    {
69      AtomicHelloServer obj = new AtomicHelloServer();
70    }
71  
72  
73    ////////////////////////////////////////////////////////////////////////////////////////////
74    // Constructor for the HelloServer
75    ////////////////////////////////////////////////////////////////////////////////////////////
76  
77    public AtomicHelloServer()
78      throws Exception
79    {
80      System.out.println("Server starting...");
81      /* Prepare the answer for replying to external invocations. */
82      answer = new Answer("Hello from " + Network.getLocalHostName());
83      System.out.println("Server is connected to a network...");
84  
85      /* Obtain the group manager */
86      GroupManager gm = GroupManager.getGroupManager(this);
87      System.out.println("Server obtained group manager");
88  
89      /* Obtain proxies for the services required by the HelloServer */
90      membershipService = (MembershipService) gm.getService(MembershipService.class);
91      externalGMIService = (ExternalGMIService) gm.getService(ExternalGMIService.class);
92      RegistryService registryService = (RegistryService) gm.getService(RegistryService.class);
93  
94      /* Join the group and bind the server in the dependable registry */
95      membershipService.join(11);
96      bindId = registryService.bind(this);
97      System.out.println("Server joined group 11");
98      MemberId thisMember = membershipService.getMyIdentifier();
99      System.out.println("My memberId: " + thisMember + ", " + thisMember.getLocalId().getMemberNo());
100     System.out.println("Server ready and bound to the dependable registry");
101   }
102 
103 
104   ////////////////////////////////////////////////////////////////////////////////////////////
105   // Methods from the Hello interface (External Group Method Invocation)
106   ////////////////////////////////////////////////////////////////////////////////////////////
107 
108   @Atomic public Answer sayHello()
109     throws RemoteException
110   {
111     System.out.println("sayHello: returning " + answer);
112     System.out.println("sayHello identifier: " + externalGMIService.getIdentifier());
113     return answer;
114   }
115 
116   @Atomic public double doCompute()
117     throws RemoteException
118   {
119     System.out.println("Starting computatations ...");
120     long start = System.currentTimeMillis();
121     long limit = 100000l;
122     double dd = 0.0d;
123     double ret = 0.0d;
124     for (long l = -limit; l < limit; l++) {
125       dd = Math.sin(3.141576d*l)*l;
126       ret += dd*2.00d;
127     }
128     long end = System.currentTimeMillis();
129     System.out.println("Computations took: " + (end-start) + " msec");
130     return ret;
131   }
132 
133 
134   ////////////////////////////////////////////////////////////////////////////////////////////
135   // Methods from MembershipListener
136   ////////////////////////////////////////////////////////////////////////////////////////////
137 
138   @AllowDuplicateViews public void viewChange(View view)
139   {
140     System.out.println("  ** ATOMIC HelloServer **" + view);
141   }
142 
143   public void prepareChange()
144   {
145     System.out.println("The current view is invalid; please await a new view...");
146   }
147 
148   public void hasLeft()
149   {
150     System.out.println("I have left the group");
151   }
152 
153 } // END AtomicHelloServer