1 24 25 package org.objectweb.dream.channel; 26 27 import java.io.IOException ; 28 import java.net.InetAddress ; 29 import java.net.UnknownHostException ; 30 31 import org.objectweb.dream.AbstractComponent; 32 import org.objectweb.dream.message.Message; 33 import org.objectweb.fractal.api.NoSuchInterfaceException; 34 import org.objectweb.fractal.api.control.IllegalBindingException; 35 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 36 37 43 public class SocketManagerIPStaticImpl extends AbstractComponent 44 implements 45 SocketManager, 46 SocketManagerIPStaticAttributeController 47 { 48 49 protected boolean destinationChanged = false; 50 protected boolean inUse = false; 51 protected SocketState currentSocket; 52 53 57 protected InetAddress destinationAddress; 58 protected int destinationPort; 59 60 64 protected IPSocketManager delegateSocketManagerItf; 65 66 70 73 public synchronized SocketState getSocket(Message message) 74 throws IOException , InterruptedException 75 { 76 if (inUse) 77 { 78 throw new IOException ("Socket is already in use."); 79 } 80 inUse = true; 81 if (currentSocket == null || currentSocket.isClosed() || destinationChanged) 82 { 83 if (currentSocket != null) 84 { 85 delegateSocketManagerItf.releaseSocket(currentSocket, false); 86 } 87 currentSocket = delegateSocketManagerItf.getSocket(destinationAddress, 88 destinationPort); 89 destinationChanged = false; 90 } 91 return currentSocket; 92 } 93 94 97 public synchronized void releaseSocket(SocketState socketState, boolean error) 98 { 99 if (socketState != currentSocket) 100 { 101 throw new IllegalArgumentException ( 102 "The given SocketState is not the last returned SocketState"); 103 } 104 if (destinationChanged) 105 { 106 delegateSocketManagerItf.releaseSocket(socketState, error); 107 currentSocket = null; 108 destinationChanged = false; 109 } 110 inUse = false; 111 } 112 113 117 120 public String getDestinationHostname() 121 { 122 return destinationAddress.getCanonicalHostName(); 123 } 124 125 128 public void setDestinationHostname(String hostname) 129 throws UnknownHostException 130 { 131 destinationAddress = InetAddress.getByName(hostname); 132 destinationChanged = true; 133 } 134 135 138 public int getDestinationPort() 139 { 140 return destinationPort; 141 } 142 143 146 public void setDestinationPort(int port) 147 { 148 if (destinationPort != port) 149 { 150 destinationPort = port; 151 destinationChanged = true; 152 } 153 } 154 155 159 162 public void stopFc() throws IllegalLifeCycleException 163 { 164 synchronized (this) 165 { 166 currentSocket.close(); 167 currentSocket = null; 168 } 169 super.stopFc(); 170 } 171 172 176 179 public String [] listFc() 180 { 181 return new String []{IPSocketManager.ITF_NAME}; 182 } 183 184 188 public synchronized void bindFc(String clientItfName, Object serverItf) 189 throws NoSuchInterfaceException, IllegalBindingException, 190 IllegalLifeCycleException 191 { 192 super.bindFc(clientItfName, serverItf); 193 if (clientItfName.equals(IPSocketManager.ITF_NAME)) 194 { 195 delegateSocketManagerItf = (IPSocketManager) serverItf; 196 } 197 } 198 } | Popular Tags |