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.relacs.mss;
20  
21  import java.io.IOException;
22  
23  import jgroup.core.ConfigManager;
24  import jgroup.core.EndPoint;
25  import jgroup.relacs.config.DistributedSystemConfig;
26  import jgroup.util.Util;
27  
28  import org.apache.log4j.Logger;
29  
30  /**
31   *  The <code>NI</code> class contains the network interface used by the
32   *  <code>Mss</code>.
33   *
34   *  @author Salvatore Cammarata
35   *  @author Hein Meling
36   *  @since Jgroup 1.2
37   */
38  final class NI 
39  {
40  
41    ////////////////////////////////////////////////////////////////////////////////////////////
42    // Logger
43    ////////////////////////////////////////////////////////////////////////////////////////////
44  
45    /** Obtain logger for this class */
46    private static final Logger log = Logger.getLogger(NI.class);
47  
48  
49    ////////////////////////////////////////////////////////////////////////////////////////////
50    // Fields
51    ////////////////////////////////////////////////////////////////////////////////////////////
52  
53    /** Unicast network interface */
54    private NetworkInterface uni;
55  
56    /** Multicast network interface */
57    private NetworkInterface mni;
58  
59  
60    ////////////////////////////////////////////////////////////////////////////////////////////
61    // Constructor
62    ////////////////////////////////////////////////////////////////////////////////////////////
63  
64    /** 
65     *  Create one instance each of the unicast and multicast network
66     *  interfaces.
67     */
68    public NI(NIListener upperLayer, int bufferlen, int multicastTTL)
69      throws IOException
70    {
71      EndPoint localHost   = DistributedSystemConfig.getLocalHost();
72      EndPoint localDomain = DistributedSystemConfig.getLocalDomain();
73      uni = new UnicastNetworkInterface("UNI", upperLayer, localHost, bufferlen);
74      mni = new MulticastNetworkInterface("MNI", upperLayer, localDomain, bufferlen, multicastTTL);
75    }
76  
77  
78    ////////////////////////////////////////////////////////////////////////////////////////////
79    // Methods
80    ////////////////////////////////////////////////////////////////////////////////////////////
81  
82    /** 
83     *  Send a message to the local cluster.
84     */
85    public void send(byte[] buffer, int buflen)
86    {
87      if (log.isDebugEnabled()) {
88        StringBuilder sbuf = new StringBuilder("send to local cluster (LAN) multicast address: packet [");
89        sbuf.append(buflen);
90        sbuf.append(" bytes]");
91        if (ConfigManager.logMsgContent) {
92          for (int i = 0; i < buflen; i++) {
93            sbuf.append(" ");
94            sbuf.append(Util.byte2str(buffer[i]));
95          }
96        }
97        log.debug(sbuf.toString());
98      }
99  
100     mni.send(buffer, buflen);
101   }
102 
103 
104   /** 
105    *  Send a message to a remote host.
106    */
107   public void send(EndPoint dest, byte[] buffer, int buflen)
108   {
109     if (log.isDebugEnabled()) {
110       StringBuilder sbuf = new StringBuilder();
111       if (dest.isMulticastEndPoint())
112         sbuf.append("send to local cluster (LAN) multicast address: ");
113       else 
114         sbuf.append("send to remote cluster leader: ");
115       sbuf.append(dest);
116       sbuf.append(" packet [");
117       sbuf.append(buflen);
118       sbuf.append(" bytes]");
119       if (ConfigManager.logMsgContent) {
120         for (int i = 0; i < buflen; i++) {
121           sbuf.append(" ");
122           sbuf.append(Util.byte2str(buffer[i]));
123         }
124       }
125       log.debug(sbuf.toString());
126     }
127 
128     if (dest.isMulticastEndPoint())
129       mni.send(dest, buffer, buflen);
130     else
131       uni.send(dest, buffer, buflen);
132   } 
133 
134 
135   /**
136    *  Starts the unicast and multicast network interfaces.
137    */
138   public void doStart() 
139   {
140     uni.doStart();
141     mni.doStart();
142   }
143 
144 } // END NI