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  import jgroup.util.Abort;
33  
34  import org.apache.log4j.Logger;
35  
36  public class Performer
37  implements MembershipListener, MulticastListener
38  {
39  
40    ////////////////////////////////////////////////////////////////////////////////////////////
41    // Logger
42    ////////////////////////////////////////////////////////////////////////////////////////////
43  
44    /** Obtain logger for this class */
45    private static Logger log = Logger.getLogger(Performer.class);
46  
47  
48    ////////////////////////////////////////////////////////////////////////////////////////////
49    // Static fields
50    ////////////////////////////////////////////////////////////////////////////////////////////
51  
52    /** Protocol name used to distinguish messages from other protocols. */
53    private static final String PROTOCOL_NAME = "Performer";
54  
55  
56    ////////////////////////////////////////////////////////////////////////////////////////////
57    // Exchanged message
58    ////////////////////////////////////////////////////////////////////////////////////////////
59  
60    static byte[] data = new byte[51200*1];
61  
62    ////////////////////////////////////////////////////////////////////////////////////////////
63    // Fields
64    ////////////////////////////////////////////////////////////////////////////////////////////
65  
66    GroupManager        gm;
67    MembershipService   membershipService;
68    MulticastService    multicastService;
69    View                view;
70    int                 counter;
71    long                time;
72    int                 me;
73  
74    ////////////////////////////////////////////////////////////////////////////////////////////
75    // Constructor
76    ////////////////////////////////////////////////////////////////////////////////////////////
77  
78    Performer()
79          throws JgroupException, IOException
80    {
81      GroupManager gm = GroupManager.getGroupManager(this);
82      membershipService = (MembershipService) gm.getService(MembershipService.class);
83      multicastService = (MulticastService) gm.getService(MulticastService.class);
84      membershipService.join(7);
85      view = null;
86      counter = 0;
87    }
88  
89  
90    ////////////////////////////////////////////////////////////////////////////////////////////
91    // Main method
92    ////////////////////////////////////////////////////////////////////////////////////////////
93  
94    public static void main(String[] argv)
95      throws Exception
96    {
97      Performer t = new Performer();
98    }
99  
100   ////////////////////////////////////////////////////////////////////////////////////////////
101   // Upcalls
102   ////////////////////////////////////////////////////////////////////////////////////////////
103 
104   public void viewChange(View view)
105   {
106     MemberId[] members = view.getMembers();
107 
108     time = System.currentTimeMillis();
109     counter = 0;
110     StringBuilder buf = new StringBuilder();
111     buf.append("View: Vid");
112     buf.append(view.getVid());
113     buf.append(" Comp: ");
114     for (int i=0; i < members.length; i++) {
115       buf.append(members[i]);
116       buf.append(", ");
117     }
118     log.debug(buf.toString());
119 
120     if (me == 0 && this.view == null) {
121       this.view = view;
122       try {
123         data[0] = (byte) (System.currentTimeMillis() & 0xFF);
124         data[data.length-1] = data[0];
125         multicastService.mcast(PROTOCOL_NAME, data, null);
126       } catch (Exception e) {
127         Abort.exit("Mcast exception", e, 1);
128       }
129     }
130   }
131 
132   /**
133    *  Returns a string naming the protocol implemented by this multicast
134    *  listener.
135    */
136   public String getProtocolName() 
137   {
138     return PROTOCOL_NAME;
139   }
140 
141   public Object deliverStream(InputStream stream, MemberId sender, int seqNo)
142   {
143     return null;
144   }
145 
146 
147   public Object deliverObject(Object msg, MemberId sender, int seqNo)
148   {
149     byte[] data = (byte[]) msg;
150 
151     log.debug("+");
152     if (me == 0) {
153       try {
154         data[0] = (byte) (System.currentTimeMillis() & 0xFF);
155         data[data.length-1] = data[0];
156         multicastService.mcast(PROTOCOL_NAME, data, null);
157       } catch (Exception e) {
158         Abort.exit("Mcast exception", e, 1);
159       }
160     }
161     counter++;
162     if (counter % 100 == 0)
163       log.debug("\nMessages: " + counter + " Msg/sec: " + (float) counter / ((System.currentTimeMillis()-time)/1000));
164     return null;
165   }
166 
167 
168   public void prepareChange()
169   {
170   }
171 
172 
173   public void hasLeft()
174   {
175   }
176 
177 }