1 22 package org.objectweb.petals.kernel.registry.thread; 23 24 import java.util.Map ; 25 26 import javax.naming.NamingException ; 27 28 import org.objectweb.petals.kernel.registry.RegistryServer; 29 import org.objectweb.petals.kernel.registry.msg.request.NewMasterRequest; 30 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest; 31 import org.objectweb.petals.kernel.registry.msg.response.ExceptionResponse; 32 import org.objectweb.petals.kernel.registry.msg.response.FullUpdateResponse; 33 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse; 34 import org.objectweb.petals.util.LoggingUtil; 35 36 50 public class TribeThread extends Thread { 51 52 55 protected static long lastRequest; 56 57 60 protected static long lastResponse; 61 62 65 protected LoggingUtil log; 66 67 70 protected boolean run = true; 71 72 75 protected long sender; 76 77 80 protected RegistryServer server; 81 82 public TribeThread(RegistryServer server) { 83 super("TribeThread" + server.getStartTime()); 84 this.server = server; 85 this.log = server.getLog(); 86 this.sender = server.getStartTime(); 87 } 88 89 92 public void run() { 93 log.call(); 94 95 while (run) { 96 Object msg = null; 97 RegistryRequest request = null; 98 RegistryResponse response = null; 99 try { 100 msg = server.getChannel().receive(); 101 if (msg instanceof RegistryRequest) { 103 request = (RegistryRequest) msg; 104 if (request.getSender() != sender 105 && (request.getRecipient() == sender || request 106 .getRecipient() == 0) 107 && lastRequest != request.getRequestDate()) { 108 111 lastRequest = request.getRequestDate(); 112 processRequest(request); 113 } 114 } else if (msg instanceof RegistryResponse) { 115 response = (RegistryResponse) msg; 116 if (response.getSender() != sender 117 && (response.getRecipient() == sender || response 118 .getRecipient() == 0) 119 && lastResponse != response.getResponseDate()) { 120 123 lastResponse = response.getResponseDate(); 124 processResponse(response); 125 } 126 } else { 127 if (msg != null) { 128 log.warning("Message is not a registry message " 129 + msg.getClass()); 130 } else { 131 log.warning("Message is null"); 132 } 133 continue; 134 } 135 } catch (Exception e) { 136 log.error(e.getMessage(), e); 137 continue; 138 } 139 } 140 } 141 142 146 public void terminate() { 147 log.start(); 148 run = false; 149 log.end(); 150 } 151 152 158 protected void processRequest(RegistryRequest request) { 159 RegistryResponse response = null; 160 try { 161 Object object = server.processRequest(request); 162 switch (request.getType()) { 163 166 case bind: 167 break; 168 171 case createSubcontext: 172 break; 173 176 case destroySubcontext: 177 break; 178 181 case fullUpdate: 182 response = new FullUpdateResponse((Map <?, ?>) object, 183 sender, request.getSender()); 184 break; 185 188 case rebind: 189 break; 190 193 case rename: 194 break; 195 198 case unbind: 199 break; 200 default: 201 break; 202 } 203 } catch (NamingException e) { 204 response = new ExceptionResponse(e, sender, request.getSender()); 205 } 206 if (response != null) { 207 try { 209 server.getChannel().send(response); 210 } catch (Exception e) { 211 log.error("Problem while sending response", e); 212 } 213 } 214 } 215 216 222 protected void processResponse(RegistryResponse response) { 223 RegistryRequest request = null; 224 Object outObject = server.processResponse(response); 225 switch (response.getType()) { 226 229 case myStartTime: 230 if (outObject != null) { 231 request = new NewMasterRequest(sender, response.getSender()); 233 } 234 break; 235 default: 236 break; 237 } 238 if (request != null) { 239 try { 241 server.getChannel().send(request); 242 } catch (Exception e) { 243 log.error("Problem while sending request", e); 244 } 245 } 246 } 247 248 } 249 | Popular Tags |