KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.message.ExtensibleMessage;
31 import org.objectweb.dream.message.Message;
32 import org.objectweb.dream.message.manager.MessageManager;
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 that retrieves sockets using an {@link IPSocketManager }and
39  * getting IP and port in a {@link IPChannelDestinationChunk }in messages.
40  */

41 public class SocketManagerIPChunkBasedImpl extends AbstractComponent
42     implements
43       SocketManager,
44       SocketManagerIPChunkBasedAttributeController
45 {
46
47   // ---------------------------------------------------------------------------
48
// attribute fields
49
// ---------------------------------------------------------------------------
50

51   protected boolean deleteChunk = true;
52   protected String JavaDoc destinationChunkName;
53
54   // ---------------------------------------------------------------------------
55
// Client interfaces
56
// ---------------------------------------------------------------------------
57

58   protected MessageManager messageManagerItf;
59   protected IPSocketManager delegateSocketManagerItf;
60
61   // ---------------------------------------------------------------------------
62
// Implementation of SocketManager interface
63
// ---------------------------------------------------------------------------
64

65   /**
66    * @see SocketManager#getSocket(Message)
67    */

68   public SocketState getSocket(Message message) throws IOException JavaDoc,
69       InterruptedException JavaDoc
70   {
71     IPChannelDestinationChunk chunk = (IPChannelDestinationChunk) message
72         .getChunk(destinationChunkName);
73     if (chunk == null)
74     {
75       throw new IOException JavaDoc("Unable to find \"" + destinationChunkName
76           + "\" chunk in message");
77     }
78     SocketState socketState = delegateSocketManagerItf.getSocket(chunk
79         .getChannelDestinationAddr(), chunk.getChannelDestinationPort());
80     if (deleteChunk && (message instanceof ExtensibleMessage))
81     {
82       ((ExtensibleMessage) message).removeChunk(destinationChunkName);
83       messageManagerItf.deleteChunk(chunk);
84     }
85     return socketState;
86   }
87
88   /**
89    * @see SocketManager#releaseSocket(SocketState, boolean)
90    */

91   public void releaseSocket(SocketState socketState, boolean error)
92   {
93     delegateSocketManagerItf.releaseSocket(socketState, error);
94   }
95
96   // ---------------------------------------------------------------------------
97
// Implementation of AttributeController interface
98
// ---------------------------------------------------------------------------
99

100   /**
101    * @see SocketManagerIPChunkBasedAttributeController#getDeleteChunk()
102    */

103   public boolean getDeleteChunk()
104   {
105     return deleteChunk;
106   }
107
108   /**
109    * @see SocketManagerIPChunkBasedAttributeController#setDeleteChunk(boolean)
110    */

111   public void setDeleteChunk(boolean deleteChunk)
112   {
113     this.deleteChunk = deleteChunk;
114   }
115
116   /**
117    * @see SocketManagerIPChunkBasedAttributeController#getDestinationChunkName()
118    */

119   public String JavaDoc getDestinationChunkName()
120   {
121     return destinationChunkName;
122   }
123
124   /**
125    * @see SocketManagerIPChunkBasedAttributeController#setDestinationChunkName(String)
126    */

127   public void setDestinationChunkName(String JavaDoc destinationChunkName)
128   {
129     this.destinationChunkName = destinationChunkName;
130   }
131
132   // ---------------------------------------------------------------------------
133
// Implementation of BindingController interface
134
// ---------------------------------------------------------------------------
135

136   /**
137    * @see org.objectweb.fractal.api.control.BindingController#listFc()
138    */

139   public String JavaDoc[] listFc()
140   {
141     return new String JavaDoc[]{MessageManager.ITF_NAME, IPSocketManager.ITF_NAME};
142   }
143
144   /**
145    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
146    * Object)
147    */

148   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
149       throws NoSuchInterfaceException, IllegalBindingException,
150       IllegalLifeCycleException
151   {
152     super.bindFc(clientItfName, serverItf);
153     if (clientItfName.equals(MessageManager.ITF_NAME))
154     {
155       messageManagerItf = (MessageManager) serverItf;
156     }
157     else if (clientItfName.equals(IPSocketManager.ITF_NAME))
158     {
159       delegateSocketManagerItf = (IPSocketManager) serverItf;
160     }
161   }
162 }
Popular Tags