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.multicast;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import jgroup.core.GroupManager;
25  import jgroup.core.JgroupException;
26  import jgroup.core.MemberId;
27  import jgroup.core.MembershipListener;
28  import jgroup.core.MembershipService;
29  import jgroup.core.View;
30  import jgroup.core.multicast.MulticastListener;
31  import jgroup.core.multicast.MulticastService;
32  
33  /**
34   * This is a very simple test showing the multicast capabilities
35   * of Jgroup (not RMI-based).
36   *
37   * @author  Alberto Montresor
38   * @since   Jgroup 1.0
39   */
40  public class MulticastObjectServer
41  implements MembershipListener, MulticastListener
42  {
43  
44    ////////////////////////////////////////////////////////////////////////////////////////////
45    // Static fields
46    ////////////////////////////////////////////////////////////////////////////////////////////
47  
48    /** Protocol name used to distinguish messages from other protocols. */
49    private static final String PROTOCOL_NAME = "Test";
50  
51  
52    ////////////////////////////////////////////////////////////////////////////////////////////
53    // Fields
54    ////////////////////////////////////////////////////////////////////////////////////////////
55  
56    MembershipService   membershipService;
57    MulticastService    multicastService;
58    MemberId            me;
59  
60    ////////////////////////////////////////////////////////////////////////////////////////////
61    // Constructor
62    ////////////////////////////////////////////////////////////////////////////////////////////
63  
64    MulticastObjectServer()
65      throws JgroupException, IOException
66    {
67      GroupManager gm = GroupManager.getGroupManager(this);
68      membershipService = (MembershipService) gm.getService(MembershipService.class);
69      multicastService = (MulticastService) gm.getService(MulticastService.class);
70      membershipService.join(7);
71      me = membershipService.getMyIdentifier();
72    }
73  
74  
75    ////////////////////////////////////////////////////////////////////////////////////////////
76    // Main method
77    ////////////////////////////////////////////////////////////////////////////////////////////
78  
79    public static void main(String[] argv)
80      throws Exception
81    {
82      MulticastObjectServer t = new MulticastObjectServer();
83    }
84  
85    ////////////////////////////////////////////////////////////////////////////////////////////
86    // Upcalls
87    ////////////////////////////////////////////////////////////////////////////////////////////
88  
89    public void viewChange(View view)
90    {
91      MemberId[] members = view.getMembers();
92  
93      System.out.println("New view identifier: " + view.getVid());
94      for (int i=0; i < members.length; i++)
95        System.out.println(members[i]);
96  
97      try {
98        if (me != null) {
99          System.out.println("Send msg: " + me);
100         multicastService.mcast(PROTOCOL_NAME, me, null);
101       }
102     } catch (Exception e) {
103       System.out.println("Mcast exception: " + e);
104       e.printStackTrace();
105     }
106   }
107 
108   /**
109    *  Returns a string naming the protocol implemented by this multicast
110    *  listener.
111    */
112   public String getProtocolName() 
113   {
114     return PROTOCOL_NAME;
115   }
116 
117   public Object deliverStream(InputStream stream, MemberId sender, int seqNo)
118   {
119     System.out.print("MulticastObjectServer: deliverStream: ");
120     return null;
121   }
122 
123 
124   public Object deliverObject(Object obj, MemberId sender, int seqNo)
125   {
126     System.out.println("MulticastObjectServer: deliverObject: " + obj);
127     return null;
128   }
129 
130 
131   public void prepareChange()
132   {
133   }
134 
135 
136   public void hasLeft()
137   {
138   }
139 
140 } // END MulticastObjectServer
141