1 26 27 package DiningPhilosophers.monolithic; 28 29 import DiningPhilosophers.*; 30 31 48 49 public class PhilosopherPdaImpl 50 extends org.omg.CORBA.LocalObject 51 implements CCM_Philosopher, 52 java.lang.Runnable , 53 org.omg.Components.SessionComponent 54 { 55 61 62 private String name_; 63 64 68 private int ticks_duration_ = 0; 69 70 71 private int hungryness_; 72 73 77 private int hungry_counter_; 78 79 80 private boolean left_fork_; 81 82 83 private boolean right_fork_; 84 85 86 private java.lang.Thread behaviour_; 87 88 89 private PhilosopherState state_; 90 91 92 private java.awt.Frame frame_; 93 94 95 private MyButton button_; 96 97 98 private CCM_Philosopher_Context the_context_; 99 100 104 private int[] stateDuration = {-1, 3, 10, 40, -1}; 105 106 107 private java.awt.Image [] stateAsIcon; 108 109 115 120 public 121 PhilosopherPdaImpl() 122 { 123 this(null); 124 } 125 126 131 public 132 PhilosopherPdaImpl(String name) 133 { 134 name_ = name; 135 java.util.Random rand = 136 new java.util.Random (System.currentTimeMillis()); 137 ticks_duration_ = 1000 + rand.nextInt(2001); 138 left_fork_ = false; 139 right_fork_ = false; 140 state_ = null; 141 behaviour_ = null; 142 hungry_counter_=0; 143 hungryness_=0; 144 145 stateAsIcon = new java.awt.Image [5]; 147 java.net.URL url = PhilosopherPdaImpl.class.getResource("/images/eat.gif"); 148 stateAsIcon[0] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 149 url = PhilosopherPdaImpl.class.getResource("/images/think.gif"); 150 stateAsIcon[1] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 151 url = PhilosopherPdaImpl.class.getResource("/images/hun.gif"); 152 stateAsIcon[2] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 153 url = PhilosopherPdaImpl.class.getResource("/images/hun_right.gif"); 154 stateAsIcon[3] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 155 url = PhilosopherPdaImpl.class.getResource("/images/rip.jpg"); 156 stateAsIcon[4] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 157 158 } 159 160 166 173 protected void 174 inThinkingState() 175 { 176 if (hungryness_ < stateDuration[PhilosopherState._THINKING]) 177 { 178 setStatus(PhilosopherState.THINKING); 179 } else { 180 setStatus(PhilosopherState.HUNGRY); 181 } 182 } 183 184 204 protected void 205 inHungryState() 206 { 207 if (! left_fork_) 209 { 210 try { 211 the_context_.get_connection_left().get(); 212 left_fork_ = true; 213 hungry_counter_=0; 214 } catch(InUse exc) { 215 if (right_fork_ && hungry_counter_ >= 5) { 216 the_context_.get_connection_right().release(); 218 right_fork_=false; 219 hungry_counter_=0; 220 } 221 } 222 } 223 224 if (! right_fork_) 226 { 227 try { 228 the_context_.get_connection_right().get(); 229 right_fork_ = true; 230 hungry_counter_=0; 231 } catch(InUse exc) { 232 if (left_fork_ && hungry_counter_ >= 5) { 233 the_context_.get_connection_left().release(); 235 left_fork_=false; 236 hungry_counter_=0; 237 } 238 } 239 } 240 241 if (left_fork_ && right_fork_) { 242 hungry_counter_=0; 243 setStatus(PhilosopherState.EATING); 245 } else if (hungryness_ >= stateDuration[PhilosopherState._HUNGRY]) { 246 hungry_counter_=0; 247 setStatus(PhilosopherState.STARVING); 249 } else { 250 hungry_counter_++; 251 setStatus(PhilosopherState.HUNGRY); 253 } 254 } 255 256 270 protected void 271 inStarvingState() 272 { 273 if (! left_fork_) 275 { 276 try { 277 the_context_.get_connection_left().get(); 278 left_fork_ = true; 279 } catch(InUse exc) { 280 } 281 } 282 283 if (! right_fork_) 285 { 286 try { 287 the_context_.get_connection_right().get(); 288 right_fork_ = true; 289 } catch(InUse exc) { 290 } 291 } 292 293 if (left_fork_ && right_fork_) { 294 setStatus(PhilosopherState.EATING); 296 } else if (hungryness_ < stateDuration[PhilosopherState._STARVING]) { 297 setStatus(PhilosopherState.STARVING); 299 } else { 300 setStatus(PhilosopherState.DEAD); 302 } 303 } 304 305 316 protected void 317 inEatingState() 318 { 319 if (hungryness_ > 0) 320 { 321 setStatus(PhilosopherState.EATING); 323 hungryness_-=3; 325 } else { 326 hungryness_ = 0; 327 if (left_fork_) 329 { 330 the_context_.get_connection_left().release(); 331 left_fork_ = false; 332 } 333 if (right_fork_) 335 { 336 the_context_.get_connection_right().release(); 337 right_fork_ = false; 338 } 339 setStatus(PhilosopherState.THINKING); 341 } 342 } 343 344 353 protected void 354 inDeadState() 355 { 356 if (left_fork_) 358 { 359 the_context_.get_connection_left().release(); 360 left_fork_ = false; 361 } 362 if (right_fork_) 364 { 365 the_context_.get_connection_right().release(); 366 right_fork_ = false; 367 } 368 setStatus(PhilosopherState.DEAD); 370 } 371 372 383 protected void 384 nextTick() 385 { 386 switch(state_.value()) 388 { 389 case PhilosopherState._EATING : 390 { 391 inEatingState(); 392 break; 393 } 394 case PhilosopherState._THINKING : 395 { 396 inThinkingState(); 397 break; 398 } 399 case PhilosopherState._HUNGRY : 400 { 401 inHungryState(); 402 break; 403 } 404 case PhilosopherState._STARVING : 405 { 406 inStarvingState(); 407 break; 408 } 409 case PhilosopherState._DEAD : 410 { 411 inDeadState(); 412 break; 413 } 414 } 415 416 try { 418 behaviour_.sleep(ticks_duration_); 419 } catch(java.lang.InterruptedException exc) { 420 exc.printStackTrace(); 421 } 422 423 switch (state_.value()) 425 { 426 case PhilosopherState._THINKING : 427 case PhilosopherState._HUNGRY : 428 case PhilosopherState._STARVING : 429 { 430 hungryness_++; 431 break; 432 } 433 } 434 435 nextTick(); 437 } 438 439 442 protected void 443 setStatus(PhilosopherState state) 444 { 445 state_ = state; 446 447 button_.setIcon(stateAsIcon[state_.value()]); 448 451 the_context_.push_info(new StatusInfoImpl(state_, name_, 453 hungryness_, left_fork_, 454 right_fork_)); 455 } 456 457 463 469 480 public void 481 configuration_complete() 482 throws org.omg.Components.InvalidConfiguration 483 { 484 if(name_ == null) 486 throw new org.omg.Components.InvalidConfiguration(); 487 488 if(ticks_duration_ == 0) 490 throw new org.omg.Components.InvalidConfiguration(); 491 492 if(the_context_.get_connection_left() == null) 494 throw new org.omg.Components.InvalidConfiguration(); 495 496 if(the_context_.get_connection_right() == null) 498 throw new org.omg.Components.InvalidConfiguration(); 499 500 frame_ = new java.awt.Frame (name_ + " GUI"); 502 java.awt.Panel border = new java.awt.Panel (new java.awt.BorderLayout ()); 503 504 button_ = new MyButton(); 506 button_.setIcon(stateAsIcon[PhilosopherState._THINKING]); 507 508 java.awt.Label label = new java.awt.Label ("Hi ! I'm " + name_ + " !!"); 510 512 border.add(button_, java.awt.BorderLayout.CENTER); 514 border.add(label, java.awt.BorderLayout.SOUTH); 515 frame_.add(border); 516 517 frame_.pack(); 519 frame_.show(); 520 521 behaviour_ = new java.lang.Thread (this); 523 behaviour_.start(); 524 } 525 526 532 537 public void 538 set_session_context(org.omg.Components.SessionContext context) 539 throws org.omg.Components.CCMException 540 { 541 the_context_ = (CCM_Philosopher_Context)context; 542 } 543 544 549 public void 550 ccm_activate() 551 throws org.omg.Components.CCMException 552 { 553 } 555 556 561 public void 562 ccm_passivate() 563 throws org.omg.Components.CCMException 564 { 565 } 567 568 573 public void 574 ccm_remove() 575 throws org.omg.Components.CCMException 576 { 577 frame_.dispose(); 579 frame_ = null; 580 581 behaviour_.stop(); 583 behaviour_ = null; 584 } 585 586 592 597 public void 598 name(String n) 599 { 600 name_ = n; 601 } 602 603 608 public String 609 name() 610 { 611 return name_; 612 } 613 614 620 623 public void 624 run() 625 { 626 setStatus(PhilosopherState.THINKING); 628 nextTick(); 629 } 630 } 631 632 635 class MyButton extends java.awt.Panel 636 { 637 638 639 protected java.awt.Image icon_; 640 641 644 public MyButton() { 645 }; 646 647 650 public void setIcon(java.awt.Image icon) 651 { 652 icon_ = icon; 653 repaint(); 654 } 655 656 660 public java.awt.Dimension getPreferredSize() 661 { 662 return new java.awt.Dimension (100, 100); 663 } 664 665 669 public void paint(java.awt.Graphics g) 670 { 671 g.drawImage(icon_, 0, 0, this); 672 } 673 } 674 | Popular Tags |