View Javadoc

1   /*
2    * Copyright (c) 1998-2004 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  import java.io.OutputStream;
24  
25  import jgroup.core.GroupManager;
26  import jgroup.core.JgroupException;
27  import jgroup.core.MemberId;
28  import jgroup.core.View;
29  import jgroup.core.multicast.MulticastListener;
30  import jgroup.core.multicast.MulticastService;
31  
32  /**
33   * Simple multicast/total order test server.
34   *
35   * @author Hein Meling
36   * @since Jgroup 2.2
37   */
38  public class MulticastTestServer
39    implements MulticastListener
40  {
41  
42    ////////////////////////////////////////////////////////////////////////////////////////////
43    // Static fields
44    ////////////////////////////////////////////////////////////////////////////////////////////
45  
46    /** Protocol name used to distinguish messages from other protocols. */
47    private static final String PROTOCOL_NAME = "McastTest";
48  
49  
50    ////////////////////////////////////////////////////////////////////////////////////////////
51    // Fields
52    ////////////////////////////////////////////////////////////////////////////////////////////
53  
54    private MulticastService multicastService;
55  
56  
57    ////////////////////////////////////////////////////////////////////////////////////////////
58    // Constructor
59    ////////////////////////////////////////////////////////////////////////////////////////////
60  
61    MulticastTestServer()
62      throws JgroupException, IOException
63    {
64      GroupManager gm = GroupManager.getGroupManager(this);
65      multicastService = (MulticastService) gm.getService(MulticastService.class);
66      Thread t = new Thread() {
67        public void run() {
68          byte[] b = new byte[100];
69          while (true) {
70            try {
71              Thread.sleep(1000);
72              System.out.println("multicasting");
73              OutputStream out = multicastService.getMessage(PROTOCOL_NAME);
74              out.write(b);
75              multicastService.mcast(out, null);
76            } catch (Exception e) {
77              e.printStackTrace();
78            }
79          }
80        }
81      };
82    }
83  
84  
85    ////////////////////////////////////////////////////////////////////////////////////////////
86    // Main method
87    ////////////////////////////////////////////////////////////////////////////////////////////
88  
89    public static void main(String[] argv)
90      throws Exception
91    {
92      MulticastTestServer mts = new MulticastTestServer();
93    }
94  
95    ////////////////////////////////////////////////////////////////////////////////////////////
96    // Methods from MembershipListener
97    ////////////////////////////////////////////////////////////////////////////////////////////
98  
99    public void viewChange(View view)
100   {
101     System.out.println("View: " + view);
102   }
103 
104   public void prepareChange() { }
105 
106   public void hasLeft() { }
107 
108 
109   ////////////////////////////////////////////////////////////////////////////////////////////
110   // Methods from MulticastListener
111   ////////////////////////////////////////////////////////////////////////////////////////////
112 
113   /**
114    *  Returns a string naming the protocol implemented by this multicast
115    *  listener.
116    */
117   public String getProtocolName() 
118   {
119     return PROTOCOL_NAME;
120   }
121 
122   public Object deliverStream(InputStream stream, MemberId sender, int seqNo)
123   {
124     System.out.print("MulticastTestServer: deliverStream: ");
125     byte[] b = new byte[100];
126     try {
127       stream.read(b);
128     } catch (IOException e) {
129       e.printStackTrace();
130     }
131     System.out.println(b);
132     return null;
133   }
134 
135 
136   public Object deliverObject(Object obj, MemberId sender, int seqNo)
137   {
138     System.out.println("MulticastTestServer: deliverObject: " + obj);
139     return null;
140   }
141 
142 } // END MulticastObjectServer