KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > delivery > NetworkModerator


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on 16/06/2004
48  *
49  * Manta LTD
50  */

51 package org.mr.kernel.delivery;
52
53 import java.io.IOException JavaDoc;
54
55 import org.apache.commons.logging.LogFactory;
56 import org.mr.MantaAgent;
57 import org.mr.core.protocol.MantaBusMessage;
58 import org.mr.core.protocol.PayloadContainer;
59 import org.mr.core.util.SynchronizedQueue;
60
61 /**
62  * NetworkModerator has a bombastic name but a simple task
63  * it makes sure that the network will not be fluded with unBufferedMessages
64  * by making sure that only N message to each recipient will be sent to be prossed
65  * @author Amir Shevat
66  *
67  */

68 public class NetworkModerator {
69     // here we keep the messge that ware waiting to be sent to the network
70
private SynchronizedQueue unBufferedMessages = new SynchronizedQueue();
71     // here we keep the messge that ware waiting to be sent to the network
72
// the payload of messages here has been turned to buffer
73
private SynchronizedQueue bufferedMessages = new SynchronizedQueue();
74     
75     // the name of the ricipient of this message
76
private String JavaDoc recipient;
77     // the max nubmer of message that were sent to the network but not sent on the wire
78
private int maxNumberOfMessages;
79     // the nubmer of message that were sent to the network but not sent on the wire
80
//private int numberOfMessageInNetBuffer = 0;
81
// if there is a message in the network layer
82
private MantaBusMessage messageInPipe = null;
83     
84     
85     
86     public NetworkModerator(String JavaDoc recipient , int maxNumberOfMessages ){
87         this.recipient = recipient;
88         this.maxNumberOfMessages =maxNumberOfMessages;
89     }
90     
91     /**
92      * send the message to the network or keeps it in safe place utill ready to be sent
93      * @param msg the message to be sent
94      */

95     public final synchronized void sendToNetwork(MantaBusMessage msg){
96         if(messageInPipe != null){
97             if(unBufferedMessages.isEmpty() == false || bufferedMessages.size() >=maxNumberOfMessages){
98                 unBufferedMessages.enqueue(msg);
99             }else{
100                 try {
101                     if(msg.getPayloadContainer()!= null)
102                         msg.getPayloadContainer().getSerializedPayload();
103                     bufferedMessages.enqueue(msg);
104                 } catch (IOException JavaDoc e) {
105                     LogFactory.getLog("NetworkModerator").error(e);
106                 }
107             }
108         }else{
109             messageInPipe = msg;
110             MantaAgent.getInstance().getSingletonRepository().getNetworkManager().sendBuffer(msg);
111         }
112         
113     }//sendToNetWork
114

115     /**
116      * after a message was sent on the wire we push more unBufferedMessages to the network
117      * if needed
118      *
119      */

120     public final synchronized void messageSentByNetwork( MantaBusMessage sent){
121         if(sent != messageInPipe){
122             return;
123         }
124         if(messageInPipe != null)
125             messageInPipe.releaseBuffers();
126         MantaBusMessage msg = null;
127         if(bufferedMessages.size() <maxNumberOfMessages){
128             msg = (MantaBusMessage) unBufferedMessages.dequeueNoBlock();
129             if(msg!= null){
130                 try {
131                     PayloadContainer con = msg.getPayloadContainer();
132                     if(con != null)
133                         con.getSerializedPayload();
134                     bufferedMessages.enqueue(msg);
135                 } catch (IOException JavaDoc e) {
136                     LogFactory.getLog("NetworkModerator").error(e);
137                 }
138             }
139             
140         }
141         if(!bufferedMessages.isEmpty()){
142             msg=(MantaBusMessage) bufferedMessages.dequeueNoBlock();
143         }
144         if(msg!= null){
145             messageInPipe=msg;
146             MantaAgent.getInstance().getSingletonRepository().getNetworkManager().sendBuffer(msg);
147         }else{
148             messageInPipe = null;
149         }
150         
151     }//messageSentByNetwork
152

153     public final synchronized void messageSendFailByNetwork(MantaBusMessage sent){
154         if(messageInPipe != null) {
155             if(sent != messageInPipe){
156                 return;
157             }
158             messageInPipe.releaseBuffers();
159             messageInPipe = null;
160         }
161     }
162
163
164     /**
165      * returns the NetworkModerator to the start state
166      */

167     public synchronized void clear() {
168         this.unBufferedMessages.clear();
169         MantaBusMessage msg = null;
170         while((msg=(MantaBusMessage) bufferedMessages.dequeueNoBlock())!= null){
171             msg.releaseBuffers();
172         }
173         bufferedMessages.clear();
174         
175     }
176     
177 }
178
Popular Tags