1 26 27 package DiningPhilosophers.cif; 28 29 import DiningPhilosophers.*; 30 31 48 49 public class PhilosopherPdaImpl 50 extends DiningPhilosophers.PhilosopherSessionComposition.PhilosopherSession_impl 51 implements java.lang.Runnable 52 { 53 59 60 private String name_; 61 62 66 private int ticks_duration_ = 0; 67 68 69 private int hungryness_; 70 71 75 private int hungry_counter_; 76 77 78 private boolean left_fork_; 79 80 81 private boolean right_fork_; 82 83 84 private java.lang.Thread behaviour_; 85 86 87 private PhilosopherState state_; 88 89 90 private java.awt.Frame frame_; 91 92 93 private MyButton button_; 94 95 99 private int[] stateDuration = {-1, 3, 10, 40, -1}; 100 101 102 private java.awt.Image [] stateAsIcon; 103 104 110 115 public 116 PhilosopherPdaImpl() 117 { 118 this(null); 119 } 120 121 126 public 127 PhilosopherPdaImpl(String name) 128 { 129 name_ = name; 130 java.util.Random rand = 131 new java.util.Random (System.currentTimeMillis()); 132 ticks_duration_ = 1000 + rand.nextInt(2001); 133 left_fork_ = false; 134 right_fork_ = false; 135 state_ = null; 136 behaviour_ = null; 137 hungry_counter_=0; 138 hungryness_=0; 139 140 stateAsIcon = new java.awt.Image [5]; 142 java.net.URL url = PhilosopherPdaImpl.class.getResource("/images/eat.gif"); 143 stateAsIcon[0] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 144 url = PhilosopherPdaImpl.class.getResource("/images/think.gif"); 145 stateAsIcon[1] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 146 url = PhilosopherPdaImpl.class.getResource("/images/hun.gif"); 147 stateAsIcon[2] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 148 url = PhilosopherPdaImpl.class.getResource("/images/hun_right.gif"); 149 stateAsIcon[3] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 150 url = PhilosopherPdaImpl.class.getResource("/images/rip.jpg"); 151 stateAsIcon[4] = java.awt.Toolkit.getDefaultToolkit().getImage(url); 152 153 } 154 155 161 168 protected void 169 inThinkingState() 170 { 171 if (hungryness_ < stateDuration[PhilosopherState._THINKING]) 172 { 173 setStatus(PhilosopherState.THINKING); 174 } else { 175 setStatus(PhilosopherState.HUNGRY); 176 } 177 } 178 179 199 protected void 200 inHungryState() 201 { 202 if (! left_fork_) 204 { 205 try { 206 get_context().get_connection_left().get(); 207 left_fork_ = true; 208 hungry_counter_=0; 209 } catch(InUse exc) { 210 if (right_fork_ && hungry_counter_ >= 5) { 211 get_context().get_connection_right().release(); 213 right_fork_=false; 214 hungry_counter_=0; 215 } 216 } 217 } 218 219 if (! right_fork_) 221 { 222 try { 223 get_context().get_connection_right().get(); 224 right_fork_ = true; 225 hungry_counter_=0; 226 } catch(InUse exc) { 227 if (left_fork_ && hungry_counter_ >= 5) { 228 get_context().get_connection_left().release(); 230 left_fork_=false; 231 hungry_counter_=0; 232 } 233 } 234 } 235 236 if (left_fork_ && right_fork_) { 237 hungry_counter_=0; 238 setStatus(PhilosopherState.EATING); 240 } else if (hungryness_ >= stateDuration[PhilosopherState._HUNGRY]) { 241 hungry_counter_=0; 242 setStatus(PhilosopherState.STARVING); 244 } else { 245 hungry_counter_++; 246 setStatus(PhilosopherState.HUNGRY); 248 } 249 } 250 251 265 protected void 266 inStarvingState() 267 { 268 if (! left_fork_) 270 { 271 try { 272 get_context().get_connection_left().get(); 273 left_fork_ = true; 274 } catch(InUse exc) { 275 } 276 } 277 278 if (! right_fork_) 280 { 281 try { 282 get_context().get_connection_right().get(); 283 right_fork_ = true; 284 } catch(InUse exc) { 285 } 286 } 287 288 if (left_fork_ && right_fork_) { 289 setStatus(PhilosopherState.EATING); 291 } else if (hungryness_ < stateDuration[PhilosopherState._STARVING]) { 292 setStatus(PhilosopherState.STARVING); 294 } else { 295 setStatus(PhilosopherState.DEAD); 297 } 298 } 299 300 311 protected void 312 inEatingState() 313 { 314 if (hungryness_ > 0) 315 { 316 setStatus(PhilosopherState.EATING); 318 hungryness_-=3; 320 } else { 321 hungryness_ = 0; 322 if (left_fork_) 324 { 325 get_context().get_connection_left().release(); 326 left_fork_ = false; 327 } 328 if (right_fork_) 330 { 331 get_context().get_connection_right().release(); 332 right_fork_ = false; 333 } 334 setStatus(PhilosopherState.THINKING); 336 } 337 } 338 339 348 protected void 349 inDeadState() 350 { 351 if (left_fork_) 353 { 354 get_context().get_connection_left().release(); 355 left_fork_ = false; 356 } 357 if (right_fork_) 359 { 360 get_context().get_connection_right().release(); 361 right_fork_ = false; 362 } 363 setStatus(PhilosopherState.DEAD); 365 } 366 367 378 protected void 379 nextTick() 380 { 381 switch(state_.value()) 383 { 384 case PhilosopherState._EATING : 385 { 386 inEatingState(); 387 break; 388 } 389 case PhilosopherState._THINKING : 390 { 391 inThinkingState(); 392 break; 393 } 394 case PhilosopherState._HUNGRY : 395 { 396 inHungryState(); 397 break; 398 } 399 case PhilosopherState._STARVING : 400 { 401 inStarvingState(); 402 break; 403 } 404 case PhilosopherState._DEAD : 405 { 406 inDeadState(); 407 break; 408 } 409 } 410 411 try { 413 behaviour_.sleep(ticks_duration_); 414 } catch(java.lang.InterruptedException exc) { 415 exc.printStackTrace(); 416 } 417 418 switch (state_.value()) 420 { 421 case PhilosopherState._THINKING : 422 case PhilosopherState._HUNGRY : 423 case PhilosopherState._STARVING : 424 { 425 hungryness_++; 426 break; 427 } 428 } 429 430 nextTick(); 432 } 433 434 437 protected void 438 setStatus(PhilosopherState state) 439 { 440 state_ = state; 441 442 button_.setIcon(stateAsIcon[state_.value()]); 443 446 get_context().push_info(new StatusInfoImpl(state_, name_, 448 hungryness_, left_fork_, 449 right_fork_)); 450 } 451 452 458 464 475 public void 476 configuration_complete() 477 throws org.omg.Components.InvalidConfiguration 478 { 479 if(name_ == null) 481 throw new org.omg.Components.InvalidConfiguration(); 482 483 if(ticks_duration_ == 0) 485 throw new org.omg.Components.InvalidConfiguration(); 486 487 if(get_context().get_connection_left() == null) 489 throw new org.omg.Components.InvalidConfiguration(); 490 491 if(get_context().get_connection_right() == null) 493 throw new org.omg.Components.InvalidConfiguration(); 494 495 frame_ = new java.awt.Frame (name_ + " GUI"); 497 java.awt.Panel border = new java.awt.Panel (new java.awt.BorderLayout ()); 498 499 button_ = new MyButton(); 501 button_.setIcon(stateAsIcon[PhilosopherState._THINKING]); 502 503 java.awt.Label label = new java.awt.Label ("Hi ! I'm " + name_ + " !!"); 505 507 border.add(button_, java.awt.BorderLayout.CENTER); 509 border.add(label, java.awt.BorderLayout.SOUTH); 510 frame_.add(border); 511 512 frame_.pack(); 514 frame_.show(); 515 516 behaviour_ = new java.lang.Thread (this); 518 behaviour_.start(); 519 } 520 521 527 532 public void 533 ccm_remove() 534 throws org.omg.Components.CCMException 535 { 536 frame_.dispose(); 538 frame_ = null; 539 540 behaviour_.stop(); 542 behaviour_ = null; 543 } 544 545 551 556 public void 557 name(String n) 558 { 559 name_ = n; 560 } 561 562 567 public String 568 name() 569 { 570 return name_; 571 } 572 573 579 582 public void 583 run() 584 { 585 setStatus(PhilosopherState.THINKING); 587 nextTick(); 588 } 589 } 590 591 594 class MyButton extends java.awt.Panel 595 { 596 597 598 protected java.awt.Image icon_; 599 600 603 public MyButton() { 604 }; 605 606 609 public void setIcon(java.awt.Image icon) 610 { 611 icon_ = icon; 612 repaint(); 613 } 614 615 619 public java.awt.Dimension getPreferredSize() 620 { 621 return new java.awt.Dimension (100, 100); 622 } 623 624 628 public void paint(java.awt.Graphics g) 629 { 630 g.drawImage(icon_, 0, 0, this); 631 } 632 } 633 | Popular Tags |