| 1 31 package org.objectweb.proactive.examples.c3d; 32 33 import java.awt.Button ; 34 import java.awt.Canvas ; 35 import java.awt.Color ; 36 import java.awt.Dialog ; 37 import java.awt.Dimension ; 38 import java.awt.Font ; 39 import java.awt.Frame ; 40 import java.awt.Graphics ; 41 import java.awt.GridBagConstraints ; 42 import java.awt.GridBagLayout ; 43 import java.awt.GridLayout ; 44 import java.awt.Image ; 45 import java.awt.Insets ; 46 import java.awt.Label ; 47 import java.awt.List ; 48 import java.awt.Menu ; 49 import java.awt.MenuBar ; 50 import java.awt.MenuItem ; 51 import java.awt.Panel ; 52 import java.awt.TextArea ; 53 import java.awt.TextField ; 54 import java.awt.event.ActionEvent ; 55 import java.awt.event.ActionListener ; 56 import java.awt.event.ItemEvent ; 57 import java.awt.event.ItemListener ; 58 import java.awt.event.MouseEvent ; 59 import java.awt.event.MouseListener ; 60 import java.awt.event.WindowAdapter ; 61 import java.awt.event.WindowEvent ; 62 import java.awt.image.MemoryImageSource ; 63 import java.net.InetAddress ; 64 import java.net.UnknownHostException ; 65 import java.util.Hashtable ; 66 67 import org.apache.log4j.Logger; 68 import org.objectweb.proactive.ProActive; 69 import org.objectweb.proactive.core.config.ProActiveConfiguration; 70 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor; 71 import org.objectweb.proactive.core.descriptor.data.VirtualNode; 72 import org.objectweb.proactive.core.node.Node; 73 import org.objectweb.proactive.core.node.NodeException; 74 import org.objectweb.proactive.examples.c3d.geom.Vec; 75 import org.objectweb.proactive.examples.c3d.prim.Light; 76 import org.objectweb.proactive.examples.c3d.prim.Primitive; 77 import org.objectweb.proactive.examples.c3d.prim.Sphere; 78 import org.objectweb.proactive.ext.migration.MigrationStrategyManagerImpl; 79 80 public class C3DUser implements org.objectweb.proactive.RunActive, java.io.Serializable { 81 82 static Logger logger = Logger.getLogger(C3DUser.class.getName()); 83 84 private MigrationStrategyManagerImpl myStrategyManager; 85 private boolean onMigration; 86 private String dispatcher_host; 87 88 protected VirtualNode vnDispatcher; 89 92 private final boolean ACTIVE = true; 93 private C3DUser me; 94 97 private MyDialog about; 98 102 TextArea ta_log; 103 106 TextArea ta_mess; 107 110 Label l_user; 111 114 Label l_c3ddispatcher; 115 118 final int i_height = 270; 119 122 final int i_width = 270; 123 127 transient MemoryImageSource mis; 128 131 transient int pix[]; 132 137 C3DDispatcher c3ddispatcher; 138 143 144 149 private Hashtable h_users = new Hashtable (); 150 153 private transient List li_users; 154 157 private int talkId = -1; 158 161 private static Vec color[] = { 162 new Vec(1, 1, 1), new Vec(0, 0, 0.5), new Vec(0, 0.5, 0), new Vec(0.5, 0, 0), new Vec(1, 0.6, 0), new Vec(1, 0.7, 0.8), new Vec(0.8, 0.8, 1) }; 171 172 173 175 178 public int nRotate = 0; 179 public transient UserFrame userframe; 180 boolean b_isApplet; 181 boolean b_isBot; 182 String botUrl; 183 String s_username; 184 int i_user; 185 186 187 public C3DUser() { 188 } 189 190 191 public C3DUser(Boolean b_isApplet, Boolean b_isBot, String url) { 192 this.b_isApplet = b_isApplet.booleanValue(); 193 this.b_isBot = b_isBot.booleanValue(); 194 this.botUrl = url; 195 this.onMigration = false; 196 } 197 198 199 public void go(ProActiveDescriptor pad) { 200 201 202 me = (C3DUser)org.objectweb.proactive.ProActive.getStubOnThis(); 203 204 vnDispatcher = pad.getVirtualNode("Dispatcher"); 206 userframe = new UserFrame(me); 207 userframe.createWelcomePanel(); 208 209 setOnMigration(true); 210 } 211 212 public void rebuild(){ 213 me = (C3DUser)org.objectweb.proactive.ProActive.getStubOnThis(); 214 userframe = new UserFrame(me); 215 userframe.createPanelAfterMigration(dispatcher_host,s_username); 216 } 217 218 public void clean(){ 219 if (userframe != null){ 220 userframe.dispose(); 221 userframe = null; 222 } 223 } 224 225 public void setOnMigration(boolean value){ 226 this.onMigration = value; 227 } 228 229 public boolean getOnMigration(){ 230 return this.onMigration; 231 } 232 233 public void runActivity(org.objectweb.proactive.Body body) { 234 logger.info("Starting custom live in C3DUser"); 235 org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body); 237 myStrategyManager = new MigrationStrategyManagerImpl((org.objectweb.proactive.core.body.migration.Migratable) body); 238 myStrategyManager.onDeparture("clean"); 239 if(getOnMigration()) { 240 rebuild(); 244 245 } 246 247 248 service.fifoServing(); 252 253 254 if(getOnMigration()) clean(); 255 258 } 259 260 261 262 265 public static void main(String argv[]) throws NodeException{ 266 267 ProActiveDescriptor proActiveDescriptor = null; 268 269 ProActiveConfiguration.load(); 270 try 271 { 272 proActiveDescriptor = 273 ProActive.getProactiveDescriptor( 274 "file:"+argv[0]); 275 proActiveDescriptor.activateMappings(); 276 277 } 278 catch (Exception e) 279 { 280 e.printStackTrace(); 281 logger.fatal("Pb in main"); 282 } 283 284 VirtualNode user = 285 proActiveDescriptor.getVirtualNode("User"); 286 287 Node node = user.getNode(); 288 Object params[] = { new Boolean (false),new Boolean (false), ""}; 289 try { 290 291 C3DUser c3duser = (C3DUser)org.objectweb.proactive.ProActive.newActive("org.objectweb.proactive.examples.c3d.C3DUser", params, node.getNodeInformation().getURL()); 292 293 c3duser.go(proActiveDescriptor); 294 } catch (Exception e) { 295 e.printStackTrace(); 296 } 297 } 298 299 300 public void log(String s_message) { 301 ta_log.append(s_message + "\n"); 302 } 303 304 305 public void message(String s_message) { 306 ta_mess.append(s_message + "\n"); 307 } 308 309 310 public UserFrame getUserFrame() { 311 return userframe; 312 } 313 314 315 323 public void informNewUser(int nUser, String sName) { 324 li_users.add(sName); 325 h_users.put(sName, new Integer (nUser)); 326 } 327 328 329 335 public void informUserLeft(String sName) { 336 337 try { 339 li_users.remove(sName); 340 } catch (Exception e) { 341 e.printStackTrace(); 342 } 343 344 h_users.remove(sName); 346 347 if (h_users.isEmpty()) { 348 talkId = -1; 349 li_users.select(0); 350 } 351 } 352 354 358 public int ping() { 359 return 0; 360 } 361 362 363 367 private Scene createScene() { 368 int x = 0; 369 int y = 0; 370 371 Scene scene = new Scene(); 372 373 374 Primitive p = new Sphere(new Vec(10, -5.77, 0), 7); 375 p.setColor(1.0, 0.0, 0.0); 376 p.surf.shine = 14.0; 377 p.surf.kd = 0.7; 378 p.surf.ks = 0.3; 379 p.surf.ior = 0.3; 380 scene.addObject(p); 381 382 p = new Sphere(new Vec(0, 11.55, 0), 7); 383 p.setColor(0.0, 1.0, 0.0); 384 p.surf.shine = 14.0; 385 p.surf.kd = 0.7; 386 p.surf.ks = 0.3; 387 scene.addObject(p); 388 389 p = new Sphere(new Vec(-10, -5.77, 0), 7); 390 p.setColor(0.0, 0.0, 1.0); 391 p.surf.shine = 14.0; 392 p.surf.kd = 0.7; 393 p.surf.ks = 0.3; 394 scene.addObject(p); 395 396 433 434 scene.addLight(new Light(100, 100, -50, 1.0)); 435 scene.addLight(new Light(-100, 100, -50, 1.0)); 436 scene.addLight(new Light(100, -100, -50, 1.0)); 437 441 442 View v = new View(); 443 v.from = new Vec(x, y, -30); 445 v.at = new Vec(x, y, -15); 447 v.up = new Vec(0, 1, 0); 448 v.angle = 35.0 * 3.14159265 / 180.0; 449 v.aspect = 1.0; 450 v.dist = 1.0; 451 scene.addView(v); 452 453 return scene; 454 } 455 456 457 462 public void setPixels(int[] newpix, Interval interval) { 463 int from = interval.width * interval.yfrom; 464 465 466 System.arraycopy(newpix, 0, pix, from, newpix.length); 467 468 469 mis.newPixels(0, interval.yfrom, interval.width, interval.yto); 470 471 472 userframe.repaint(); 473 } 474 475 476 481 public void showMessage(String message) { 482 ta_log.append(message + "\n"); 483 if (b_isBot) { 484 if (message.substring(0, 3).compareTo("All") == 0) { 486 if (nRotate < 15) { 488 nRotate++; 489 c3ddispatcher.rotateLeft(i_user); 490 } else { 491 c3ddispatcher.doBenchmarks(); 492 exit(); 493 } 494 } 495 } 496 } 497 498 499 public void showUserMessage(String message) { 500 ta_mess.append(message + '\n'); 501 } 502 503 504 public void dialogMessage(String title, String content) { 505 MsgDialog md = new MsgDialog(userframe, title, content); 506 md.setVisible(true); 507 } 508 509 510 514 515 public Integer getWidth() { 516 return new Integer (i_width); 518 } 519 520 521 525 public Integer getHeight() { 526 return new Integer (i_height); 527 } 528 529 530 533 534 void exit() { 535 try { 536 c3ddispatcher.unregisterConsumer(i_user); 537 userframe.setVisible(false); 538 userframe.dispose(); 539 userframe = null; 540 me.terminate(); 541 542 } catch (NullPointerException ex) { 543 ex.printStackTrace(); 544 } 545 } 550 551 552 private void trace(String s_message) { 553 logger.info("C3DUser: " + s_message); 554 } 555 556 557 public void terminate(){ 558 org.objectweb.proactive.ProActive.getBodyOnThis().terminate(); 559 } 560 561 public class UserFrame extends Frame implements ActionListener , ItemListener { 562 563 566 567 private Button b_up = new EqualButton("Up"); 568 571 private Button b_down = new EqualButton("Down"); 572 575 private Button b_left = new EqualButton("Left"); 576 579 private Button b_right = new EqualButton("Right"); 580 583 private Button b_clock = new EqualButton("Spin right"); 584 587 private Button b_unclock = new EqualButton("Spin left"); 588 591 private Button b_exit = new EqualButton("Exit"); 592 595 private Button b_addSphere = new Button ("Add random sphere"); 596 599 private Button b_connect = new EqualButton("Connect"); 600 603 private Button b_reset = new EqualButton("Reset"); 604 607 private Button b_send = new Button ("Send"); 608 private TextField tf_mess, tf_name, tf_host; 609 private GridBagLayout gridbag = new GridBagLayout ();; 610 private GridBagConstraints c = new GridBagConstraints (); 611 private String s_localhost; 612 private C3DUser c3duser; 613 private MenuItem mi_exit, mi_clear, mi_list,mi_about; 614 private Font f_standard = new Font ("SansSerif", Font.PLAIN, 12); 615 private int i_top; 616 private MenuBar mb; 617 618 619 624 public UserFrame(C3DUser c3d) { 625 super("Collaborative 3D Environment - User Window"); 626 setBackground(Color.lightGray); 627 setLayout(gridbag); 628 setFont(f_standard); 629 addWindowListener(new MyWindowListener()); 630 c3duser = c3d; 631 632 s_localhost = ""; 633 try { 634 s_localhost = InetAddress.getLocalHost().getCanonicalHostName(); 635 } catch (UnknownHostException e) { 636 s_localhost = "unknown!"; 637 } 638 } 639 640 public UserFrame(C3DUser c3d, boolean value) { 641 super("Collaborative 3D Environment - User Window"); 642 setBackground(Color.lightGray); 643 setLayout(gridbag); 644 setFont(f_standard); 645 addWindowListener(new MyWindowListener()); 646 c3duser = c3d; 647 s_localhost = ""; 648 try { 649 s_localhost = InetAddress.getLocalHost().getCanonicalHostName(); 650 } catch (UnknownHostException e) { 651 s_localhost = "unknown!"; 652 } 653 654 } 655 656 public Button getB_up() { 657 return b_up; 658 } 659 660 661 public Button getB_down() { 662 return b_down; 663 } 664 665 666 public Button getB_left() { 667 return b_left; 668 } 669 670 671 public Button getB_right() { 672 return b_right; 673 } 674 675 676 public Button getB_clock() { 677 return b_clock; 678 } 679 680 681 public Button getB_unclock() { 682 return b_unclock; 683 } 684 685 686 689 public void createBot(String url) { 690 s_username = "Benchmarking bot"; 691 } 693 694 695 private void createWelcomePanel() { 696 Label l1 = new Label ("Welcome to the Collaborative 3D Environment -", Label.CENTER); 697 c.insets = new Insets (5, 5, 0, 5); 698 c.gridwidth = GridBagConstraints.REMAINDER; 699 c.weightx = 1.0; 700 gridbag.setConstraints(l1, c); 701 add(l1); 702 703 Label l2 = new Label ("a Java// test application", Label.CENTER); 704 c.insets = new Insets (0, 5, 0, 5); 705 gridbag.setConstraints(l2, c); 706 add(l2); 707 708 Label l3 = new Label ("Please enter your name and the host of the C3DDispatcher.", Label.CENTER); 709 c.insets = new Insets (15, 5, 10, 5); 710 gridbag.setConstraints(l3, c); 711 add(l3); 712 713 Label l4 = new Label ("Name:", Label.RIGHT); 714 Label l5 = new Label ("Host:", Label.RIGHT); 715 c.insets = new Insets (5, 5, 5, 0); 716 c.gridwidth = 1; 717 c.anchor = GridBagConstraints.EAST; 718 gridbag.setConstraints(l4, c); 719 gridbag.setConstraints(l5, c); 720 721 tf_name = new TextField (20); 722 tf_name.addActionListener(this); 723 724 tf_host = new TextField (s_localhost, 20); 725 tf_host.addActionListener(this); 726 c.insets = new Insets (5, 0, 5, 5); 727 c.anchor = GridBagConstraints.CENTER; 728 c.gridwidth = GridBagConstraints.REMAINDER; 729 gridbag.setConstraints(tf_name, c); 730 gridbag.setConstraints(tf_host, c); 731 732 add(l4); 733 add(tf_name); 734 add(l5); 735 add(tf_host); 736 737 c.gridwidth = GridBagConstraints.RELATIVE; 738 c.insets = new Insets (15, 5, 10, 5); 739 c.anchor = GridBagConstraints.EAST; 740 gridbag.setConstraints(b_connect, c); 741 b_connect.addActionListener(this); 742 add(b_connect); 743 c.gridwidth = GridBagConstraints.REMAINDER; 744 c.anchor = GridBagConstraints.CENTER; 745 gridbag.setConstraints(b_exit, c); 746 b_exit.addActionListener(this); 747 add(b_exit); 748 749 pack(); 750 setVisible(true); 751 toFront(); 752 } 753 754 public void createPanel(String s_host,String s_name){ 755 setVisible(false); 756 removeAll(); 757 758 mb = new MenuBar (); 759 Menu m_file = new Menu ("File", false); 760 m_file.setFont(f_standard); 761 mi_exit = new MenuItem ("Exit"); 762 mi_clear = new MenuItem ("Clear log"); 763 mi_list = new MenuItem ("List users"); 764 mi_clear.setFont(f_standard); 765 mi_exit.setFont(f_standard); 766 mi_list.setFont(f_standard); 767 mi_exit.addActionListener(this); 768 mi_clear.addActionListener(this); 769 mi_list.addActionListener(this); 770 m_file.add(mi_list); 771 m_file.add(mi_clear); 772 m_file.addSeparator(); 773 m_file.add(mi_exit); 774 mb.add(m_file); 775 776 Menu m_c3d = new Menu ("C3D ProActive PDC", false); 777 mi_about = new MenuItem ("About ProActive PDC"); 778 mi_about.setFont(f_standard); 779 mi_about.addActionListener(this); 780 m_c3d.add(mi_about); 781 782 mb.setHelpMenu(m_c3d); 783 setMenuBar(mb); 784 785 786 pix = new int[i_width * i_height]; 787 788 789 mis = new MemoryImageSource (i_width, i_height, pix, 0, i_width); 790 791 792 mis.setAnimated(true); 793 794 798 Panel p_image = new Panel (new GridLayout (1, 1)); 799 801 c.insets = new Insets (5, 5, 0, 5); 803 804 c.gridx = 0; 806 c.gridy = 0; 807 c.weightx = 1; 808 c.weighty = 1; 809 810 c.gridwidth = 1; 812 c.gridheight = 1; 813 814 c.fill = GridBagConstraints.BOTH; 817 gridbag.setConstraints(p_image, c); 818 p_image.add(new MyImageContainer(createImage(mis), i_width, i_height, p_image)); 820 add(p_image); 821 822 825 826 Panel p_log = new Panel (); 828 829 c.gridx = 1; 831 gridbag.setConstraints(p_log, c); 832 add(p_log); 833 834 GridBagLayout gb_panel = new GridBagLayout (); 836 p_log.setLayout(gb_panel); 837 838 l_user = new Label ("User " + s_name, Label.CENTER); 840 l_user.setFont(new Font ("SansSerif", Font.ITALIC + Font.BOLD, 18)); 841 842 GridBagConstraints pc = new GridBagConstraints (); 843 844 pc.fill = GridBagConstraints.BOTH; 845 pc.insets = new Insets (5, 5, 0, 5); 846 pc.gridx = 0; 847 pc.gridy = 0; 848 pc.weightx = 1; 849 pc.weighty = 1; 850 pc.gridwidth = GridBagConstraints.REMAINDER; 851 gb_panel.setConstraints(l_user, pc); 852 p_log.add(l_user); 853 854 Label l_machine = new Label ("Local host: " + s_localhost + " (" + System.getProperty("os.name") + " " + System.getProperty("os.version") + ")"); 856 857 pc.gridy = 1; 859 gb_panel.setConstraints(l_machine, pc); 860 p_log.add(l_machine); 861 862 863 l_c3ddispatcher = new Label ("C3DDispatcher host: " + s_host); 865 pc.gridy = 2; 866 gb_panel.setConstraints(l_c3ddispatcher, pc); 867 p_
|