| 1 package com.ubermq.jms.common.datagram.impl; 2 3 import com.ubermq.kernel.*; 4 import com.ubermq.jms.common.datagram.*; 5 import com.ubermq.jms.common.datagram.control.*; 6 7 11 public class ControlDatagram 12 extends AbstractDatagram 13 implements IControlDatagram 14 { 15 private int controlCode; 16 private ICommandSubGram commandGram; 17 18 static final int CONTROL_NOOP = 0; 19 static final int CONTROL_SUB = 1; 20 static final int CONTROL_UNSUB = 2; 21 static final int CONTROL_STOP = 3; 22 static final int CONTROL_START = 4; 23 static final int CONTROL_CLUSTER = 5; 24 static final int CONTROL_CLUSTER_PEER_ID = 6; 25 static final int CONTROL_DURABLE_SUB = 7; 26 static final int CONTROL_DURABLE_UNSUB = 8; 27 static final int CONTROL_DURABLE_GOING_AWAY = 9; 28 static final int CONTROL_DURABLE_RECOVER = 10; 29 static final int CONTROL_QUEUE_START = 11; 30 static final int CONTROL_QUEUE_STOP = 12; 31 static final int CONTROL_QUEUE_DELETE = 13; 32 33 36 public ControlDatagram() 37 { 38 super(DatagramFactory.DGRAM_CONTROL, 0); 39 } 40 41 49 ControlDatagram(int controlCode, 50 ICommandSubGram subGram) 51 { 52 super(DatagramFactory.DGRAM_CONTROL, 0); 53 54 this.controlCode = controlCode; 55 this.commandGram = subGram; 56 } 57 58 public int getControlCode() { return controlCode; } 59 public ICommandSubGram getCommandSubGram() {return commandGram;} 60 61 71 public void incoming(java.nio.ByteBuffer bb) 72 throws java.io.IOException  73 { 74 super.incoming(bb); 75 76 controlCode = bb.getInt(); 77 78 ICommandSubGram subgram = createSubGram(controlCode); 79 subgram.incoming(bb); 80 this.commandGram = subgram; 81 } 82 83 86 public void outgoing(java.nio.ByteBuffer bb) 87 { 88 super.outgoing(bb); 89 90 bb.putInt(controlCode); 91 commandGram.outgoing(bb); 92 } 93 94 private ICommandSubGram createSubGram(int controlCode) 95 { 96 switch(controlCode) 97 { 98 case CONTROL_NOOP: 99 default: 100 return new NoopDatagramImpl(); 101 case CONTROL_START: 102 return new StartDatagramImpl(); 103 case CONTROL_STOP: 104 return new StopDatagramImpl(); 105 case CONTROL_SUB: 106 return new SubscribeDatagramImpl(); 107 case CONTROL_UNSUB: 108 return new UnsubscribeDatagramImpl(); 109 case CONTROL_CLUSTER: 110 return new ClusterDatagramImpl(); 111 case CONTROL_CLUSTER_PEER_ID: 112 return new ClusterPeerDatagramImpl(); 113 case CONTROL_DURABLE_SUB: 114 return new DurableSubscribeDatagramImpl(); 115 case CONTROL_DURABLE_UNSUB: 116 return new DurableUnSubDatagramImpl(); 117 case CONTROL_DURABLE_GOING_AWAY: 118 return new DurableGoingAwayDatagramImpl(); 119 case CONTROL_DURABLE_RECOVER: 120 return new DurableRecoverDatagramImpl(); 121 case CONTROL_QUEUE_START: 122 return new QueueStartDatagramImpl(); 123 case CONTROL_QUEUE_STOP: 124 return new QueueStopDatagramImpl(); 125 } 126 } 127 128 private abstract static class AbstractSubGramImpl 129 implements ICommandSubGram 130 { 131 public String toString() {return "";} 132 public void incoming(java.nio.ByteBuffer bb) 133 { 134 } 135 136 public void outgoing(java.nio.ByteBuffer bb) 137 { 138 } 139 } 140 141 static class NoopDatagramImpl 142 extends AbstractSubGramImpl 143 implements INoopDatagram 144 { 145 public String toString() {return "NOOP";} 146 } 147 148 static class ClusterDatagramImpl 149 extends AbstractSubGramImpl 150 implements IClusterDatagram 151 { 152 public String toString() {return "CLUSTER";} 153 } 154 155 static class ClusterPeerDatagramImpl 156 extends AbstractSubGramImpl 157 implements IClusterPeerDatagram 158 { 159 private String peerId; 160 161 ClusterPeerDatagramImpl(String sz) {this.peerId = sz;} 162 ClusterPeerDatagramImpl() {} 163 164 public String toString() {return "CLUSTER_PEER\npeer = " + getClusterPeerId();} 165 166 public String getClusterPeerId() 167 { 168 return peerId; 169 } 170 171 public void incoming(java.nio.ByteBuffer bb) 172 { 173 super.incoming(bb); 174 peerId = readPascalString(bb); 175 } 176 177 public void outgoing(java.nio.ByteBuffer bb) 178 { 179 super.outgoing(bb); 180 writePascalString(peerId, bb); 181 } 182 } 183 184 private abstract static class DurableDatagramImpl 185 extends AbstractSubGramImpl 186 { 187 private String sub; 188 189 DurableDatagramImpl(String sub) {this.sub = sub; } 190 DurableDatagramImpl() {} 191 192 public String getSubscriptionName() 193 { 194 return sub; 195 } 196 197 public void incoming(java.nio.ByteBuffer bb) 198 { 199 super.incoming(bb); 200 sub = readPascalString(bb); 201 } 202 203 public void outgoing(java.nio.ByteBuffer bb) 204 { 205 super.outgoing(bb); 206 writePascalString(sub, bb); 207 } 208 } 209 210 static class DurableSubscribeDatagramImpl 211 extends DurableDatagramImpl 212 implements IDurableSubscribeDatagram 213 { 214 private String topic, selector; 215 216 DurableSubscribeDatagramImpl(String sub, String topic, String selector) {super(sub); this.topic = topic; this.selector = selector;} 217 DurableSubscribeDatagramImpl() {} 218 219 public String getTopicSpecification() 220 { 221 return topic; 222 } 223 224 public String getSelector() 225 { 226 return selector; 227 } 228 229 public String toString() {return "DURABLE_SUB\ntopic = " + getTopicSpecification() + ", selector = " + getSelector() + " " + super.toString();} 230 231 public void incoming(java.nio.ByteBuffer bb) 232 { 233 super.incoming(bb); 234 topic = readPascalString(bb); 235 selector = readPascalString(bb); 236 } 237 238 public void outgoing(java.nio.ByteBuffer bb) 239 { 240 super.outgoing(bb); 241 writePascalString(topic, bb); 242 writePascalString(selector, bb); 243 } 244 245 } 246 247 static class DurableUnSubDatagramImpl 248 extends DurableDatagramImpl 249 implements IDurableUnsubscribeDatagram 250 { 251 DurableUnSubDatagramImpl(String sub) {super(sub);} 252 DurableUnSubDatagramImpl() {} 253 public String toString() {return "DURABLE_UNSUB\n" + super.toString();} 254 } 255 256 static class DurableGoingAwayDatagramImpl 257 extends DurableDatagramImpl 258 implements IDurableGoingAwayDatagram 259 { 260 DurableGoingAwayDatagramImpl(String sub) {super(sub);} 261 DurableGoingAwayDatagramImpl() {} 262 public String toString() {return "DURABLE_GOING_AWAY\n" + super.toString();} 263 } 264 265 static class DurableRecoverDatagramImpl 266 extends DurableDatagramImpl 267 implements IDurableRecoverDatagram 268 { 269 DurableRecoverDatagramImpl(String sub) {super(sub);} 270 DurableRecoverDatagramImpl() {} 271 public String toString() {return "DURABLE_RECOVER\n" + super.toString();} 272 } 273 274 static class StartDatagramImpl 275 extends AbstractSubGramImpl 276 implements IStartDatagram 277 { 278 StartDatagramImpl() {} 279 public String toString() {return "START\n" + super.toString();} 280 } 281 282 static class StopDatagramImpl 283 extends AbstractSubGramImpl 284 implements IStopDatagram 285 { 286 StopDatagramImpl() {} 287 public String toString() {return "START\n" + super.toString();} 288 } 289 290 private abstract static class TopicDatagramImpl 291 extends AbstractSubGramImpl 292 { 293 private String topic; 294 295 TopicDatagramImpl(String topic) {this.topic = topic; } 296 TopicDatagramImpl() {} 297 298 public String getTopicSpecification() 299 { 300 return topic; 301 } 302 303 public String toString() {return "topic = " + getTopicSpecification() + " " + super.toString();} 304 305 public void incoming(java.nio.ByteBuffer bb) 306 { 307 super.incoming(bb); 308 topic = readPascalString(bb); 309 } 310 311 public void outgoing(java.nio.ByteBuffer bb) 312 { 313 super.outgoing(bb); 314 writePascalString(topic, bb); 315 } 316 } 317 318 static class SubscribeDatagramImpl 319 extends TopicDatagramImpl 320 implements ISubscribeDatagram 321 { 322 private String selector; 323 324 SubscribeDatagramImpl(String topic, String selector) {super(topic); this.selector = selector;} 325 SubscribeDatagramImpl() {} 326 public String toString() {return "SUB\nselector = " + getSelector() + " " + super.toString();} 327 328 public String getSelector() 329 { 330 return selector; 331 } 332 333 public void incoming(java.nio.ByteBuffer bb) 334 { 335 super.incoming(bb); 336 selector = readPascalString(bb); 337 } 338 339 public void outgoing(java.nio.ByteBuffer bb) 340 { 341 super.outgoing(bb); 342 writePascalString(selector, bb); 343 } 344 } 345 346 static class UnsubscribeDatagramImpl 347 extends TopicDatagramImpl 348 implements IUnsubscribeDatagram 349 { 350 UnsubscribeDatagramImpl(String topic) {super(topic);} 351 UnsubscribeDatagramImpl() {} 352 public String toString() {return "UNSUB\n" + super.toString();} 353 } 354 355 private abstract static class QueueDatagramImpl 356 extends AbstractSubGramImpl 357 { 358 private String queue; 359 360 QueueDatagramImpl(String queue) {this.queue = queue; } 361 QueueDatagramImpl() {} 362 363 public String getQueueName() 364 { 365 return queue; 366 } 367 368 public void incoming(java.nio.ByteBuffer bb) 369 { 370 super.incoming(bb); 371 queue = readPascalString(bb); 372 } 373 374 public void outgoing(java.nio.ByteBuffer bb) 375 { 376 super.outgoing(bb); 377 writePascalString(queue, bb); 378 } 379 } 380 381 static class QueueStartDatagramImpl 382 extends QueueDatagramImpl 383 implements IQueueStartDatagram 384 { 385 private String selector; 386 387 QueueStartDatagramImpl(String queue, String selector) {super(queue); this.selector = selector;} 388 QueueStartDatagramImpl() {} 389 390 public String getSelector() 391 { 392 return selector; 393 } 394 395 public void outgoing(java.nio.ByteBuffer bb) 396 { 397 super.outgoing(bb); 398 writePascalString(selector, bb); 399 } 400 401 public void incoming(java.nio.ByteBuffer bb) 402 { 403 super.incoming(bb); 404 selector = readPascalString(bb); 405 } 406 407 public String toString() {return "QUEUE_START";} 408 } 409 410 static class QueueStopDatagramImpl 411 extends QueueDatagramImpl 412 implements IQueueStopDatagram 413 { 414 QueueStopDatagramImpl(String queue) {super(queue);} 415 QueueStopDatagramImpl() {} 416 417 public String toString() {return "QUEUE_STOP";} 418 } 419 420 static class QueueDeleteDatagramImpl 421 extends QueueDatagramImpl 422 implements IQueueDeleteDatagram 423 { 424 QueueDeleteDatagramImpl(String queue) {super(queue);} 425 QueueDeleteDatagramImpl() {} 426 427 public String toString() {return "QUEUE_DELETE";} 428 } 429 430 public String toString() { 431 return super.toString() + 432 "\nCode:\t\t" + controlCode + 433 "\nCmd :\t\t" + commandGram.toString(); 434 } 435 436 public boolean equals(Object o) 437 { 438 if (o instanceof IControlDatagram) { 439 IControlDatagram cd = (IControlDatagram)o; 440 return cd.getControlCode() == getControlCode(); 441 } 442 else { 443 return false; 444 } 445 } 446 447 public int hashCode() { 448 return getControlCode(); 449 } 450 } 451 452 453 | Popular Tags |