1 3 package org.jgroups.protocols; 4 5 import org.jgroups.Event; 6 import org.jgroups.stack.Protocol; 7 8 import java.io.IOException ; 9 import java.net.DatagramPacket ; 10 import java.net.DatagramSocket ; 11 import java.net.InetAddress ; 12 import java.util.HashMap ; 13 import java.util.Properties ; 14 15 16 27 public class AUTOCONF extends Protocol { 28 final HashMap config=new HashMap (); 29 static int num_iterations=10; 31 33 static int frag_overhead=1000; 34 35 36 public String getName() { 37 return "AUTOCONF"; 38 } 39 40 41 public void init() throws Exception { 42 senseNetworkConfiguration(); 43 if(log.isDebugEnabled()) log.debug("configuration is\n" + config); 44 } 45 46 public void start() throws Exception { 47 if(config != null && config.size() > 0) { 48 Event config_evt=new Event(Event.CONFIG, config); 49 passDown(config_evt); 50 passUp(config_evt); 51 } 52 } 53 54 55 58 public boolean setProperties(Properties props) { 59 String str; 60 61 super.setProperties(props); 62 str=props.getProperty("num_iterations"); 63 if(str != null) { 64 num_iterations=Integer.parseInt(str); 65 props.remove("num_iterations"); 66 } 67 68 str=props.getProperty("frag_overhead"); 69 if(str != null) { 70 frag_overhead=Integer.parseInt(str); 71 props.remove("frag_overhead"); 72 } 73 74 if(props.size() > 0) { 75 System.err.println("AUTOCONF.setProperties(): the following properties are not recognized:"); 76 props.list(System.out); 77 return false; 78 } 79 return true; 80 } 81 82 83 86 public void startUpHandler() { 87 ; 88 } 89 90 93 public void startDownHandler() { 94 ; 95 } 96 97 98 99 void senseNetworkConfiguration() { 100 int max_frag_size=senseMaxFragSize(); 101 if(max_frag_size <= 0) { 102 if(log.isErrorEnabled()) log.error("max_frag_size is invalid: " + max_frag_size); 103 } 104 else 105 config.put("frag_size", new Integer (max_frag_size)); 106 senseMaxSendBufferSize(config); 107 senseMaxReceiveBufferSize(config); 108 } 109 110 public static int senseMaxFragSizeStatic() { 111 return new AUTOCONF().senseMaxFragSize(); 112 } 113 114 118 public int senseMaxFragSize() { 119 int max_send=32000; 120 int upper=8192; 121 int lower=0; 122 int highest_failed=-1; 123 DatagramSocket sock=null; 124 byte[] buf; 125 DatagramPacket packet; 126 InetAddress local_addr; 127 128 129 try { 130 sock=new DatagramSocket (); 131 local_addr=InetAddress.getLocalHost(); 132 } 133 catch(Exception ex) { 134 if(log.isWarnEnabled()) log.warn("failed creating DatagramSocket: " + ex); 135 return 0; 136 } 137 138 try { 139 upper=max_send; 140 for(int i=0; i < num_iterations && lower < upper; i++) { try { 142 buf=new byte[upper]; 143 packet=new DatagramPacket (buf, buf.length, local_addr, 9); 145 sock.send(packet); 146 lower=Math.max(lower, upper); 147 upper=upper * 2; 148 if(highest_failed > -1) 149 upper=Math.min(highest_failed, upper); 150 } 151 catch(IOException io_ex) { 152 if(highest_failed > -1) 153 highest_failed=Math.min(highest_failed, upper); else 155 highest_failed=upper; 156 upper=(upper + lower) / 2; 157 } 158 catch(Throwable ex) { 159 if(log.isWarnEnabled()) log.warn("exception=" + ex); 160 break; 161 } 162 } 163 164 165 lower-=frag_overhead; 166 if(log.isDebugEnabled()) log.debug("frag_size=" + lower); 167 return lower; 168 } 169 finally { 170 if(sock != null) 171 sock.close(); 172 } 173 } 174 175 176 void senseMaxSendBufferSize(HashMap map) { 177 DatagramSocket sock=null; 178 int max_size=4096, retval=max_size; 179 180 if(map != null && map.containsKey("frag_size)")) 181 max_size=((Integer )map.get("frag_size")).intValue(); 182 183 try { 184 sock=new DatagramSocket (); 185 while(max_size < 1000000) { 186 sock.setSendBufferSize(max_size); 187 if((retval=sock.getSendBufferSize()) < max_size) 188 return; 189 max_size*=2; 190 } 191 } 192 catch(Throwable ex) { 193 if(log.isErrorEnabled()) log.error("failed getting the max send buffer size: " + ex + 194 ". Defaulting to " + retval); 195 return; 196 } 197 finally { 198 map.put("send_buf_size", new Integer (retval)); 199 } 200 } 201 202 203 204 void senseMaxReceiveBufferSize(HashMap map) { 205 DatagramSocket sock=null; 206 int max_size=4096, retval=max_size; 207 208 try { 209 sock=new DatagramSocket (); 210 while(max_size < 1000000) { 211 sock.setReceiveBufferSize(max_size); 212 if((retval=sock.getReceiveBufferSize()) < max_size) 213 return; 214 max_size*=2; 215 } 216 } 217 catch(Throwable ex) { 218 if(log.isErrorEnabled()) log.error("failed getting the max send buffer size: " + ex + 219 ". Defaulting to " + retval); 220 return; 221 } 222 finally { 223 map.put("recv_buf_size", new Integer (retval)); 224 } 225 } 226 227 228 229 230 public static void main(String [] args) { 231 int frag_size=new AUTOCONF().senseMaxFragSize(); 232 System.out.println("frag_size: " + frag_size); 233 } 234 235 } 236 | Popular Tags |