1 31 package org.objectweb.proactive; 32 33 import org.objectweb.proactive.core.body.request.BlockingRequestQueue; 34 import org.objectweb.proactive.core.body.request.Request; 35 import org.objectweb.proactive.core.body.request.RequestFilter; 36 import org.objectweb.proactive.core.body.request.RequestProcessor; 37 38 39 77 public class Service { 78 79 83 protected Body body; 84 protected BlockingRequestQueue requestQueue; 85 86 87 91 95 public Service(Body body) { 96 this.body = body; 97 this.requestQueue = body.getRequestQueue(); 98 } 99 100 101 105 106 110 public String toString() { 111 return "Service\n Body="+body.toString()+"\n RequestQueue="+requestQueue.toString(); 112 } 113 114 115 119 public void serve(Request request) { 120 body.serve(request); 121 } 122 123 124 129 public void fifoServing() { 130 while (body.isActive()) { 131 blockingServeOldest(); 132 } 133 } 134 135 136 141 public void lifoServing() { 142 while (body.isActive()) { 143 blockingServeYoungest(); 144 } 145 } 146 147 148 150 155 public void blockingServeOldest() { 156 body.serve(requestQueue.blockingRemoveOldest()); 157 } 158 159 160 167 public void blockingServeOldest(long timeout) { 168 body.serve(requestQueue.blockingRemoveOldest(timeout)); 169 } 170 171 172 178 public void blockingServeOldest(String methodName) { 179 body.serve(requestQueue.blockingRemoveOldest(methodName)); 180 } 181 182 183 189 public void blockingServeOldest(RequestFilter requestFilter) { 190 body.serve(requestQueue.blockingRemoveOldest(requestFilter)); 191 } 192 193 194 198 public void serveOldest() { 199 body.serve(requestQueue.removeOldest()); 200 } 201 202 203 208 public void serveOldest(String methodName) { 209 body.serve(requestQueue.removeOldest(methodName)); 210 } 211 212 213 218 public void serveOldest(RequestFilter requestFilter) { 219 body.serve(requestQueue.removeOldest(requestFilter)); 220 } 221 222 223 224 226 231 public void blockingServeYoungest() { 232 body.serve(requestQueue.blockingRemoveYoungest()); 233 } 234 235 236 243 public void blockingServeYoungest(long timeout) { 244 body.serve(requestQueue.blockingRemoveYoungest(timeout)); 245 } 246 247 248 254 public void blockingServeYoungest(String methodName) { 255 body.serve(requestQueue.blockingRemoveYoungest(methodName)); 256 } 257 258 259 265 public void blockingServeYoungest(RequestFilter requestFilter) { 266 body.serve(requestQueue.blockingRemoveYoungest(requestFilter)); 267 } 268 269 270 274 public void serveYoungest() { 275 body.serve(requestQueue.removeYoungest()); 276 } 277 278 279 284 public void serveYoungest(String methodName) { 285 body.serve(requestQueue.removeYoungest(methodName)); 286 } 287 288 289 294 public void serveYoungest(RequestFilter requestFilter) { 295 body.serve(requestQueue.removeYoungest(requestFilter)); 296 } 297 298 299 300 302 309 public void serveAll(String methodName) { 310 serveAll(new RequestFilterOnMethodName(methodName)); 311 } 312 313 314 319 public void serveAll(RequestFilter requestFilter) { 320 requestQueue.processRequests(new ServingRequestProcessor(requestFilter), body); 321 } 322 323 324 325 327 334 public void flushingServeYoungest() { 335 flushingServeYoungest(new AcceptAllRequestFilter()); 336 } 337 338 339 348 public void flushingServeYoungest(String methodName) { 349 flushingServeYoungest(new RequestFilterOnMethodName(methodName)); 350 } 351 352 353 361 public void flushingServeYoungest(RequestFilter requestFilter) { 362 requestQueue.processRequests(new FlushingServeYoungestRequestProcessor(requestFilter), body); 363 } 364 365 366 367 369 376 public void flushingServeOldest() { 377 flushingServeOldest(new AcceptAllRequestFilter()); 378 } 379 380 381 390 public void flushingServeOldest(String methodName) { 391 flushingServeOldest(new RequestFilterOnMethodName(methodName)); 392 } 393 394 395 403 public void flushingServeOldest(RequestFilter requestFilter) { 404 requestQueue.processRequests(new FlushingServeOldestRequestProcessor(requestFilter), body); 405 } 406 407 408 409 411 414 public void waitForRequest() { 415 requestQueue.waitForRequest(); 416 } 417 418 419 423 public boolean hasRequestToServe() { 424 return ! requestQueue.isEmpty(); 425 } 426 427 428 432 public int getRequestCount() { 433 return requestQueue.size(); 434 } 435 436 437 440 public void flushAll() { 441 requestQueue.clear(); 442 } 443 444 445 449 454 public Request getOldest() { 455 return requestQueue.getOldest(); 456 } 457 458 459 465 public Request getOldest(String methodName) { 466 return requestQueue.getOldest(methodName); 467 } 468 469 470 476 public Request getOldest(RequestFilter requestFilter) { 477 return requestQueue.getOldest(requestFilter); 478 } 479 480 481 482 488 public Request blockingGetOldest() { 489 Request request = null; 490 while (request == null && ! requestQueue.isDestroyed()) { 491 waitForRequest(); 492 request = requestQueue.getOldest(); 493 } 494 return request; 495 } 496 497 498 502 507 public Request getYoungest() { 508 return requestQueue.getYoungest(); 509 } 510 511 512 518 public Request getYoungest(String methodName) { 519 return requestQueue.getYoungest(methodName); 520 } 521 522 523 529 public Request getYoungest(RequestFilter requestFilter) { 530 return requestQueue.getYoungest(requestFilter); 531 } 532 533 534 540 public Request blockingGetYoungest() { 541 Request request = null; 542 while (request == null && ! requestQueue.isDestroyed()) { 543 waitForRequest(); 544 request = requestQueue.getYoungest(); 545 } 546 return request; 547 } 548 549 550 551 555 556 564 public Request blockingRemoveOldest(RequestFilter requestFilter) { 565 return requestQueue.blockingRemoveOldest(requestFilter); 566 } 567 568 575 public Request blockingRemoveOldest(String methodName) { 576 return requestQueue.blockingRemoveOldest(methodName); 577 } 578 579 585 public Request blockingRemoveOldest() { 586 return requestQueue.blockingRemoveOldest(); 587 } 588 589 596 public Request blockingRemoveOldest(long timeout) { 597 return requestQueue.blockingRemoveOldest(timeout); 598 } 599 600 601 602 606 614 public Request blockingRemoveYoungest(RequestFilter requestFilter) { 615 return requestQueue.blockingRemoveYoungest(requestFilter); 616 } 617 618 625 public Request blockingRemoveYoungest(String methodName) { 626 return requestQueue.blockingRemoveYoungest(methodName); 627 } 628 629 635 public Request blockingRemoveYoungest() { 636 return requestQueue.blockingRemoveYoungest(); 637 } 638 639 646 public Request blockingRemoveYoungest(long timeout) { 647 return requestQueue.blockingRemoveYoungest(timeout); 648 } 649 650 651 655 656 666 protected class ServingRequestProcessor implements RequestProcessor { 667 668 669 private RequestFilter selectorRequestFilter; 670 671 public ServingRequestProcessor(RequestFilter selectorRequestFilter) { 672 this.selectorRequestFilter = selectorRequestFilter; 673 } 674 675 682 public int processRequest(Request request) { 683 if (selectorRequestFilter.acceptRequest(request)) { 684 return REMOVE_AND_SERVE; 685 } else { 686 return KEEP; 687 } 688 } 689 } 691 692 693 694 695 706 protected class FlushingServeYoungestRequestProcessor implements RequestProcessor { 707 private RequestFilter selectorRequestFilter; 708 private Request requestToServe; 709 private int counter; 710 private int numberOfRequests; 711 712 public FlushingServeYoungestRequestProcessor(RequestFilter selectorRequestFilter) { 713 this.selectorRequestFilter = selectorRequestFilter; 714 } 715 716 723 public int processRequest(Request request) { 724 if (counter == 0) { 725 numberOfRequests = requestQueue.size(); 727 } 728 counter++; 729 int shouldRemove; 730 if (selectorRequestFilter.acceptRequest(request)) { 731 requestToServe = request; 732 shouldRemove = REMOVE; 733 } else { 734 shouldRemove = KEEP; 735 } 736 if (counter == numberOfRequests && requestToServe != null) { 737 if (request == requestToServe) { 738 return REMOVE_AND_SERVE; } else { 740 body.serve(requestToServe); } 742 } 743 return shouldRemove; 744 } 745 } 747 748 749 760 protected class FlushingServeOldestRequestProcessor implements RequestProcessor { 761 private RequestFilter selectorRequestFilter; 762 private boolean hasServed; 763 764 public FlushingServeOldestRequestProcessor(RequestFilter selectorRequestFilter) { 765 this.selectorRequestFilter = selectorRequestFilter; 766 } 767 768 775 public int processRequest(Request request) { 776 if (selectorRequestFilter.acceptRequest(request)) { 777 if (!hasServed) { 778 hasServed = true; 779 return REMOVE_AND_SERVE; 780 } 781 return REMOVE; 782 } else { 783 return KEEP; 784 } 785 } 786 } 788 789 790 800 protected class RequestFilterOnMethodName implements RequestFilter { 801 private String methodName; 802 public RequestFilterOnMethodName(String methodName) { 803 this.methodName = methodName; 804 } 805 810 public boolean acceptRequest(Request request) { 811 return methodName.equals(request.getMethodName()); 812 } 813 } 815 816 817 826 protected class AcceptAllRequestFilter implements RequestFilter { 827 832 public boolean acceptRequest(Request request) { 833 return true; 834 } 835 } 837 838 839 } 840 | Popular Tags |