1 46 package org.mr.kernel.services; 47 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 52 import org.mr.MantaAgent; 53 import org.mr.MantaException; 54 import org.mr.core.protocol.RecipientAddress; 55 import org.mr.indexing.WBAdvertiser; 56 import org.mr.kernel.control.ControlSignalMessageConsumer; 57 import org.mr.kernel.control.ControlSignalMessageSender; 58 59 66 public class ServiceActorControlCenter { 67 ControlSignalMessageSender defaultSender; 68 WBAdvertiser wbSender; 69 70 static ArrayList consumerStatusListeners = new ArrayList (); 71 private static HashMap upConsumers = new HashMap (); 73 74 static synchronized void addUpConsumer(ServiceConsumer consumer ){ 75 upConsumers.put(consumer.getId(),consumer); 76 Iterator consumerListeners = consumerStatusListeners.iterator(); 77 while(consumerListeners.hasNext()){ 78 ServiceActorStatusListener listener = (ServiceActorStatusListener) consumerListeners.next(); 79 listener.handleConsumerUp(consumer); 80 } 81 } 82 83 public static synchronized void removeUpConsumer(ServiceConsumer consumer ){ 84 upConsumers.remove(consumer.getId()); 85 Iterator consumerListeners = consumerStatusListeners.iterator(); 86 while(consumerListeners.hasNext()){ 87 ServiceActorStatusListener listener = (ServiceActorStatusListener) consumerListeners.next(); 88 listener.handleConsumerDown(consumer); 89 } 90 } 91 92 public static synchronized void addConsumerStatusListeners(ServiceActorStatusListener listener){ 93 consumerStatusListeners.add(listener); 94 } 95 96 public static synchronized void removeConsumerStatusListeners(ServiceActorStatusListener listener){ 97 consumerStatusListeners.remove(listener); 98 } 99 100 public static boolean isConsumerUp(RecipientAddress recipient){ 101 if(recipient instanceof ServiceConsumer){ 102 if(((ServiceConsumer)recipient).getServiceName() 104 .equals(ControlSignalMessageConsumer 105 .CONTROL_PSEUDO_SERVICE_NAME)){ 106 return true; 107 }else{ 108 return upConsumers.containsKey(recipient.getId()); 109 } 110 111 112 } 113 114 return true; 115 } 116 117 118 public ServiceActorControlCenter() { 119 this.defaultSender = new ControlSignalMessageSender(); 120 this.wbSender = null; 121 } 122 123 public void setDefaultSender(ControlSignalMessageSender sender) { 124 this.defaultSender = sender; 125 } 126 127 public void setWBSender(WBAdvertiser wbSender) { 128 this.wbSender = wbSender; 129 } 130 131 152 public void advertiseService(ServiceActor serviceActor, MantaAgent agent) 153 throws MantaException 154 { 155 if (this.defaultSender != null) { 156 this.defaultSender.advertiseService(serviceActor, agent); 157 } 158 if (wbSender != null) { 159 this.wbSender.advertiseService(serviceActor, agent); 160 } 161 } 162 163 164 175 public void recallService(ServiceActor serviceActor, MantaAgent agent) 176 throws MantaException 177 { 178 if (this.defaultSender != null) { 179 this.defaultSender.recallService(serviceActor, agent); 180 } 181 if (wbSender != null) { 182 this.wbSender.recallService(serviceActor, agent); 183 } 184 } 185 186 public void recallDurableSubscription(ServiceActor serviceActor, MantaAgent agent) throws MantaException 187 { 188 if (this.defaultSender != null) { 189 this.defaultSender.recallDurableSubscription(serviceActor, agent); 190 } 191 if (wbSender != null) { 192 this.wbSender.recallDurableSubscription(serviceActor, agent); 193 } 194 } 195 196 199 public ControlSignalMessageSender getDefaultSender() { 200 return this.defaultSender; 201 } 202 } 203 | Popular Tags |