You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractSimulation.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. */
  13. package telecom;
  14. public abstract class AbstractSimulation {
  15. public static AbstractSimulation simulation;
  16. /**
  17. * Creates objects and puts them to work.
  18. */
  19. public void run() {
  20. Customer jim = new Customer("Jim", 650);
  21. Customer mik = new Customer("Mik", 650);
  22. Customer crista = new Customer("Crista", 415);
  23. say("jim calls mik...");
  24. Call c1 = jim.call(mik);
  25. wait(1.0);
  26. say("mik accepts...");
  27. mik.pickup(c1);
  28. wait(2.0);
  29. say("jim hangs up...");
  30. jim.hangup(c1);
  31. report(jim);
  32. report(mik);
  33. report(crista);
  34. say("mik calls crista...");
  35. Call c2 = mik.call(crista);
  36. say("crista accepts...");
  37. crista.pickup(c2);
  38. wait(1.5);
  39. say("crista hangs up...");
  40. crista.hangup(c2);
  41. report(jim);
  42. report(mik);
  43. report(crista);
  44. }
  45. /**
  46. * Print a report of the connection time for customer
  47. */
  48. abstract protected void report(Customer c);
  49. /**
  50. * Wait 0.1 seconds per "second" for simulation
  51. */
  52. protected static void wait(double seconds) {
  53. Object dummy = new Object();
  54. synchronized (dummy) {
  55. //XXX cheat and only wait 0.1 seconds per second
  56. try {dummy.wait((long)(seconds*100)); }
  57. catch (Exception e) {}
  58. }
  59. }
  60. /**
  61. * Put a message on standard output
  62. */
  63. protected static void say(String s){
  64. System.out.println(s);
  65. }
  66. }