Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.

1 /*2 * Copyright 2002 by3 * <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 Version7 * 1.1 (the "License"); you may not use this file except in compliance with the8 * License. You may obtain a copy of the License at9 * 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 License13 * for the specific language governing rights and limitations under the14 * 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) 200620 * Coridan Inc. All Rights Reserved.21 *22 * Contributor(s): all the names of the contributors are added in the source23 * code where applicable.24 *25 * Alternatively, the contents of this file may be used under the terms of the26 * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the27 * provisions of LGPL are applicable instead of those above. If you wish to28 * allow use of your version of this file only under the terms of the LGPL29 * License and not to allow others to use your version of this file under30 * the MPL, indicate your decision by deleting the provisions above and31 * 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 version33 * 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 it37 * under the terms of the MPL as stated above or under the terms of the GNU38 * 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 WITHOUT42 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or43 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public44 * License for more details.45 */46 /*47 * Created on 16/06/200448 *49 * Manta LTD50 */51 package org.mr.kernel.delivery;52 53 importjava.io.IOException;54 55 importorg.apache.commons.logging.LogFactory;56 importorg.mr.MantaAgent;57 importorg.mr.core.protocol.MantaBusMessage;58 importorg.mr.core.protocol.PayloadContainer;59 importorg.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 unBufferedMessages64 * by making sure that only N message to each recipient will be sent to be prossed65 * @author Amir Shevat66 *67 */68 publicclass NetworkModerator {69 // here we keep the messge that ware waiting to be sent to the network70 privateSynchronizedQueue unBufferedMessages = newSynchronizedQueue();71 // here we keep the messge that ware waiting to be sent to the network72 // the payload of messages here has been turned to buffer73 privateSynchronizedQueue bufferedMessages = newSynchronizedQueue();74 75 // the name of the ricipient of this message76 privateString recipient;77 // the max nubmer of message that were sent to the network but not sent on the wire78 privateint maxNumberOfMessages;79 // the nubmer of message that were sent to the network but not sent on the wire80 //private int numberOfMessageInNetBuffer = 0;81 // if there is a message in the network layer82 privateMantaBusMessage messageInPipe = null;83 84 85 86 public NetworkModerator(String 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 sent93 * @param msg the message to be sent94 */95 publicfinalsynchronizedvoid 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 e) {105 LogFactory.getLog("NetworkModerator").error(e);106 }107 }108 }else{109 messageInPipe = msg;110 MantaAgent.getInstance().getSingletonRepository().getNetworkManager().sendBuffer(msg);111 }112 113 }//sendToNetWork114 115 /**116 * after a message was sent on the wire we push more unBufferedMessages to the network117 * if needed118 *119 */120 publicfinalsynchronizedvoid 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 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 }//messageSentByNetwork152 153 publicfinalsynchronizedvoid 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 state166 */167 publicsynchronizedvoid 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