KickJava   Java API By Example, From Geeks To Geeks.

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


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): Vivien Quema
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.Push;
31 import org.objectweb.dream.PushException;
32 import org.objectweb.dream.message.Message;
33 import org.objectweb.dream.message.codec.MessageCodec;
34 import org.objectweb.dream.message.manager.MessageManager;
35 import org.objectweb.fractal.api.NoSuchInterfaceException;
36 import org.objectweb.fractal.api.control.IllegalBindingException;
37 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 /**
41  * Generic push ChannelIn implementation.
42  */

43 public class GenericPushChannelInImpl extends AbstractComponent
44     implements
45       OpenedSocket
46 {
47
48   /**
49    * The name of the optional client interface used by this coponent to send an
50    * acknowlegdment
51    */

52   public static final String JavaDoc SEND_ACK_OPT_ITF_NAME = "send-ack-opt";
53
54   // ---------------------------------------------------------------------------
55
// Client interfaces
56
// ---------------------------------------------------------------------------
57

58   protected Push outPushItf;
59   protected MessageManager messageManagerItf;
60   protected MessageCodec messageCodecItf;
61   protected SendByte sendAckOptItf;
62
63   // ---------------------------------------------------------------------------
64
// Implementation of the OpenedSocket interface
65
// ---------------------------------------------------------------------------
66

67   /**
68    * @see OpenedSocket#openedSocket(SocketState)
69    */

70   public void openedSocket(SocketState socket) throws IOException JavaDoc
71   {
72     Message message = messageCodecItf.decode(socket);
73
74     logger.log(BasicLevel.DEBUG, "msg received");
75     try
76     {
77       outPushItf.push(message, null);
78     }
79     catch (PushException e)
80     {
81       if (sendAckOptItf != null)
82       {
83         logger.log(BasicLevel.ERROR, "A push exception occurs. Send a nack", e);
84         sendAckOptItf.sendByte(socket.getOutput(), 1);
85       }
86       else
87       {
88         logger.log(BasicLevel.ERROR, "A push exception occurs", e);
89       }
90       messageManagerItf.deleteMessage(message);
91       return;
92     }
93
94     // then send the acknowledge.
95
if (sendAckOptItf != null)
96     {
97       logger.log(BasicLevel.DEBUG, "send ack");
98       sendAckOptItf.sendByte(socket.getOutput(), 0);
99     }
100   }
101
102   // ---------------------------------------------------------------------------
103
// Implementation of BindingController interface
104
// ---------------------------------------------------------------------------
105

106   /**
107    * @see org.objectweb.fractal.api.control.BindingController#listFc()
108    */

109   public String JavaDoc[] listFc()
110   {
111     return new String JavaDoc[]{MessageManager.ITF_NAME, MessageCodec.ITF_NAME,
112         Push.OUT_PUSH_ITF_NAME, SEND_ACK_OPT_ITF_NAME};
113   }
114
115   /**
116    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
117    * Object)
118    */

119   public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
120       throws NoSuchInterfaceException, IllegalBindingException,
121       IllegalLifeCycleException
122   {
123     super.bindFc(clientItfName, serverItf);
124     if (clientItfName.equals(Push.OUT_PUSH_ITF_NAME))
125     {
126       outPushItf = (Push) serverItf;
127     }
128     else if (clientItfName.equals(MessageManager.ITF_NAME))
129     {
130       messageManagerItf = (MessageManager) serverItf;
131     }
132     else if (clientItfName.equals(MessageCodec.ITF_NAME))
133     {
134       messageCodecItf = (MessageCodec) serverItf;
135     }
136     else if (clientItfName.equals(SEND_ACK_OPT_ITF_NAME))
137     {
138       sendAckOptItf = (SendByte) serverItf;
139     }
140   }
141
142   /**
143    * @see org.objectweb.fractal.api.control.BindingController#unbindFc(String)
144    */

145   public void unbindFc(String JavaDoc clientItfName) throws NoSuchInterfaceException,
146       IllegalBindingException, IllegalLifeCycleException
147   {
148     super.unbindFc(clientItfName);
149     if (clientItfName.equals(SEND_ACK_OPT_ITF_NAME))
150     {
151       sendAckOptItf = null;
152     }
153   }
154 }
Popular Tags