KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > channel > SocketManagerIPStaticImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.channel;
26
27 import java.io.IOException JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.net.UnknownHostException JavaDoc;
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 /**
38  * Socket manager managing only one open socket at a time. Socket is retrieved
39  * using a {@link IPSocketManager }, the IP and port value are set with the
40  * {@link SocketManagerIPStaticAttributeController }attribute controller
41  * interface.
42  */

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   // ---------------------------------------------------------------------------
54
// Attribute fields
55
// ---------------------------------------------------------------------------
56

57   protected InetAddress JavaDoc destinationAddress;
58   protected int destinationPort;
59
60   // ---------------------------------------------------------------------------
61
// Client interfaces
62
// ---------------------------------------------------------------------------
63

64   protected IPSocketManager delegateSocketManagerItf;
65
66   // ---------------------------------------------------------------------------
67
// Implementation of the SocketManager interface
68
// ---------------------------------------------------------------------------
69

70   /**
71    * @see SocketManager#getSocket(Message)
72    */

73   public synchronized SocketState getSocket(Message message)
74       throws IOException JavaDoc, InterruptedException JavaDoc
75   {
76     if (inUse)
77     {
78       throw new IOException JavaDoc("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   /**
95    * @see SocketManager#releaseSocket(SocketState, boolean)
96    */

97   public synchronized void releaseSocket(SocketState socketState, boolean error)
98   {
99     if (socketState != currentSocket)
100     {
101       throw new IllegalArgumentException JavaDoc(
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   // ---------------------------------------------------------------------------
114
// Implementation of the AttributeController interface
115
// ---------------------------------------------------------------------------
116

117   /**
118    * @see SocketManagerIPStaticAttributeController#getDestinationHostname()
119    */

120   public String JavaDoc getDestinationHostname()
121   {
122     return destinationAddress.getCanonicalHostName();
123   }
124
125   /**
126    * @see SocketManagerIPStaticAttributeController#setDestinationHostname(String)
127    */

128   public void setDestinationHostname(String JavaDoc hostname)
129       throws UnknownHostException JavaDoc
130   {
131     destinationAddress = InetAddress.getByName(hostname);
132     destinationChanged = true;
133   }
134
135   /**
136    * @see SocketManagerIPStaticAttributeController#getDestinationPort()
137    */

138   public int getDestinationPort()
139   {
140     return destinationPort;
141   }
142
143   /**
144    * @see SocketManagerIPStaticAttributeController#setDestinationPort(int)
145    */

146   public void setDestinationPort(int port)
147   {
148     if (destinationPort != port)
149     {
150       destinationPort = port;
151       destinationChanged = true;
152     }
153   }
154
155   // ---------------------------------------------------------------------------
156
// Overriden method of the BindingController interface
157
// ---------------------------------------------------------------------------
158

159   /**
160    * @see org.objectweb.fractal.api.control.LifeCycleController#stopFc()
161    */

162   public void stopFc() throws IllegalLifeCycleException
163   {
164     synchronized (this)
165     {
166       currentSocket.close();
167       currentSocket = null;
168     }
169     super.stopFc();
170   }
171
172   // ---------------------------------------------------------------------------
173
// Implementation of the BindingController interface
174
// ---------------------------------------------------------------------------
175

176   /**
177    * @see org.objectweb.fractal.api.control.BindingController#listFc()
178    */

179   public String JavaDoc[] listFc()
180   {
181     return new String JavaDoc[]{IPSocketManager.ITF_NAME};
182   }
183
184   /**
185    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
186    * Object)
187    */

188   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc 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