1 31 package org.objectweb.proactive.examples.c3d; 32 33 import java.awt.Button ; 34 import java.awt.Color ; 35 import java.awt.Font ; 36 import java.awt.Frame ; 37 import java.awt.Graphics ; 38 import java.awt.GridBagConstraints ; 39 import java.awt.GridBagLayout ; 40 import java.awt.Insets ; 41 import java.awt.Label ; 42 import java.awt.List ; 43 import java.awt.Menu ; 44 import java.awt.MenuBar ; 45 import java.awt.MenuItem ; 46 import java.awt.Panel ; 47 import java.awt.TextArea ; 48 import java.awt.event.ActionEvent ; 49 import java.awt.event.ActionListener ; 50 import java.awt.event.WindowAdapter ; 51 import java.awt.event.WindowEvent ; 52 import java.io.FileWriter ; 53 import java.io.IOException ; 54 import java.io.RandomAccessFile ; 55 import java.net.InetAddress ; 56 import java.net.UnknownHostException ; 57 import java.util.Date ; 58 import java.util.Enumeration ; 59 import java.util.Hashtable ; 60 import java.util.Stack ; 61 62 import org.apache.log4j.Logger; 63 import org.objectweb.proactive.ProActive; 64 import org.objectweb.proactive.core.body.request.Request; 65 import org.objectweb.proactive.core.config.ProActiveConfiguration; 66 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor; 67 import org.objectweb.proactive.core.descriptor.data.VirtualNode; 68 import org.objectweb.proactive.core.node.Node; 69 import org.objectweb.proactive.core.node.NodeException; 70 import org.objectweb.proactive.examples.c3d.geom.Vec; 71 import org.objectweb.proactive.examples.c3d.prim.Primitive; 72 import org.objectweb.proactive.examples.c3d.prim.Sphere; 73 74 77 public class C3DDispatcher implements org.objectweb.proactive.RunActive 78 { 79 static Logger logger = Logger.getLogger(C3DDispatcher.class.getName()); 80 81 protected VirtualNode vn; 82 85 private static final boolean ACTIVE = true; 86 89 private Hashtable h_users = new Hashtable (); 90 91 94 private long startTime; 95 98 private long avgRender = 0; 99 102 private long totalRender = 0; 103 107 private Stack int_stack = new Stack (); 108 112 private C3DRenderingEngine engine[]; 113 116 private Hashtable h_engines = new Hashtable (); 117 121 private Scene scene; 122 125 private int engines; 126 127 132 private int intervals; 133 136 private int width; 137 140 private int height; 141 145 private int intheight; 146 150 private int[] pixels; 151 156 private int i_left = 0; 157 private int i_lastuser = 0; 158 private TextArea ta_log; 159 private List li_users; 160 private List li_enginesUsed; 161 private List li_enginesAvailable; 162 private String s_access_url; 163 private String s_hosts; 164 private Button b_addEng = new Button ("Add engine"); 165 private Button b_rmEng = new Button ("Remove engine"); 166 private Election election = null; 167 170 private long userPing = 0; 171 174 private long userTotal = 0; 175 178 private long enginePing = 0; 179 183 private boolean b_render = false; 184 187 private ProActiveDescriptor proActiveDescriptor; 188 private String [] rendererNodes; 189 190 193 public C3DDispatcher() 194 { 195 } 196 197 200 206 209 public C3DDispatcher(String [] rendererNodes,VirtualNode vn, ProActiveDescriptor pad) 210 { 211 new C3DDispatcherFrame(); 212 this.rendererNodes=rendererNodes; 213 this.vn = vn; 214 this.proActiveDescriptor = pad; 215 } 216 222 public void init() 223 { 224 try 225 { 226 C3DDispatcher d = 227 (C3DDispatcher) org 228 .objectweb 229 .proactive 230 .ProActive 231 .getStubOnThis(); 232 233 238 Object [] param = { d }; 240 241 251 engines = rendererNodes.length; 253 engine = new C3DRenderingEngine[engines]; 254 for (int i = 0; i < rendererNodes.length; i++) 255 { 256 C3DRenderingEngine tmp = 257 ( 258 C3DRenderingEngine)ProActive 259 .newActive( 260 "org.objectweb.proactive.examples.c3d.C3DRenderingEngine", 261 param, 262 rendererNodes[i]); 263 log("New rendering engine " + i + " created at " + rendererNodes[i]); 265 266 if (i == 1) 268 li_enginesUsed.add(rendererNodes[i].toString()); 269 else 270 li_enginesAvailable.add(rendererNodes[i].toString()); 271 272 h_engines.put(rendererNodes[i], tmp); 274 } 275 } 295 catch (Exception e) 296 { 297 e.printStackTrace(); 298 throw new RuntimeException (e.toString()); 299 } 300 } 301 302 305 public void log(String s_message) 306 { 307 ta_log.append(s_message + "\n"); 308 } 309 310 315 private void render() 316 { 317 318 if (li_enginesUsed.getItems().length == 0) 320 { 321 showMessageAll("No engines ... contact the dispatcher"); 322 return; 323 } 324 325 b_render = true; 327 328 startTime = System.currentTimeMillis(); 330 331 Interval interval; 332 String keys[] = li_enginesUsed.getItems(); 333 engines = keys.length; 334 335 log("Creating " + intervals + " intervals"); 336 intervals = 3 * keys.length; 337 intheight = height / intervals; 338 339 340 i_left = intervals; 341 342 343 int_stack = new Stack (); 344 for (int i = 0; i < intervals; i++) 345 { 346 Interval newint = 348 new Interval( 349 i, 350 width, 351 height, 352 i * intheight, 353 (i + 1) * intheight, 354 intervals); 355 int_stack.push(newint); 356 } 357 358 engine = null; 359 engine = new C3DRenderingEngine[keys.length]; 360 361 C3DRenderingEngine tmp; 362 if (keys != null) 363 { 364 for (int e = 0; e < keys.length; e++) 365 { 366 367 368 if (!int_stack.empty()) 369 { 370 interval = (Interval) int_stack.pop(); 371 372 tmp = (C3DRenderingEngine) h_engines.get(keys[e]); 373 374 if (tmp != null) 375 { 376 377 tmp.setScene(scene); 378 379 engine[e] = tmp; 380 381 382 tmp.render(e, interval); 383 384 log( 385 "Interval " 386 + interval.number 387 + " assigned to engine " 388 + keys[e] 389 + "[" 390 + e 391 + "]"); 392 } 393 else 394 { 395 log( 396 "Failed to assign an interval to engine " 397 + keys[e]); 398 h_engines.get(keys[e]); 399 } 400 } 401 } 402 } 403 else 404 b_render = false; 405 } 406 407 long previousTime = -1; 408 409 420 public void setPixels(int[] newpix, Interval interval, int engine_number) 421 { 422 424 long elapsed; 425 426 427 for (Enumeration e = h_users.elements(); e.hasMoreElements();) 428 { 429 User user = (User) e.nextElement(); 430 431 user.getObject().setPixels(newpix, interval); 432 } 433 434 436 System.arraycopy( 437 newpix, 438 0, 439 pixels, 440 interval.width * interval.yfrom, 441 newpix.length); 442 443 i_left--; 444 445 446 Interval nextinterval; 447 if (!int_stack.empty()) 448 { 449 nextinterval = (Interval) int_stack.pop(); 450 log( 451 "Next interval [" 452 + nextinterval.number 453 + "] assigned to engine " 454 + engine_number); 455 engine[engine_number].render(engine_number, nextinterval); 457 459 } 460 else if (i_left == 0) 461 { 462 elapsed = System.currentTimeMillis() - startTime; 464 465 totalRender++; 466 avgRender += ((elapsed - avgRender) / totalRender); 467 468 showMessageAll("All intervals rendered in " + elapsed + " ms"); 469 if (previousTime != -1) 470 showMessageAll( 471 "Speed Up : " 472 + java.lang.Math.rint( 473 ((double) previousTime / elapsed) * 1000) 474 / 1000 475 + "\n"); 476 else 477 showMessageAll(""); 478 previousTime = elapsed; 479 480 b_render = false; 481 } 482 } 483 484 491 private void rotateSceneY(double angle) 492 { 493 int objects = scene.getObjects(); 494 Sphere o; 495 496 for (int i = 0; i < objects; i++) 497 { 498 o = (Sphere) scene.getObject(i); 499 Vec c = o.getCenter(); 500 double phi = Math.atan2(c.z, c.x); 501 double l = Math.sqrt(c.x * c.x + c.z * c.z); 502 503 c.x = l * Math.cos(phi + angle); 504 c.z = l * Math.sin(phi + angle); 505 o.setCenter(c); 506 scene.setObject(o, i); 507 } 508 509 render(); 510 } 511 512 519 private void rotateSceneX(double angle) 520 { 521 int objects = scene.getObjects(); 522 Sphere o; 523 524 for (int i = 0; i < objects; i++) 525 { 526 o = (Sphere) scene.getObject(i); 527 Vec c = o.getCenter(); 528 double phi = Math.atan2(c.z, c.y); 529 double l = Math.sqrt(c.y * c.y + c.z * c.z); 530 531 c.y = l * Math.cos(phi + angle); 532 c.z = l * Math.sin(phi + angle); 533 o.setCenter(c); 534 scene.setObject(o, i); 535 } 536 537 render(); 538 } 539 540 547 private void rotateSceneZ(double angle) 548 { 549 int objects = scene.getObjects(); 550 Sphere o; 551 552 for (int i = 0; i < objects; i++) 553 { 554 o = (Sphere) scene.getObject(i); 555 Vec c = o.getCenter(); 556 double phi = Math.atan2(c.x, c.y); 557 double l = Math.sqrt(c.y * c.y + c.x * c.x); 558 559 c.y = l * Math.cos(phi + angle); 560 c.x = l * Math.sin(phi + angle); 561 o.setCenter(c); 562 scene.setObject(o, i); 563 } 564 565 render(); 566 } 567 568 575 public void rotateLeft(int i_user) 576 { 577 rotateSceneY(-Math.PI / 4); 578 } 579 580 587 public void rotateRight(int i_user) 588 { 589 rotateSceneY(Math.PI / 4); 590 } 591 592 599 public void rotateUp(int i_user) 600 { 601 rotateSceneX(Math.PI / 4); 602 } 603 604 611 public void rotateDown(int i_user) 612 { 613 rotateSceneX(-Math.PI / 4); 614 } 615 616 623 public void spinClock(int i_user) 624 { 625 rotateSceneZ(Math.PI / 4); 626 } 627 628 635 public void spinUnclock(int i_user) 636 { 637 rotateSceneZ(-Math.PI / 4); 638 } 639 640 public void changeColorAll() 641 { 642 logger.info("changeColorAll()"); 643 for (Enumeration e = h_users.elements(); e.hasMoreElements();) 644 ((User) e.nextElement()) 645 .getObject() 646 .getUserFrame() 647 .getB_left() 648 .setBackground( 649 Color.yellow); 650 } 651 652 656 public void runActivity(org.objectweb.proactive.Body body) 657 { 658 660 init(); 661 org 662 .objectweb 663 .proactive 664 .core 665 .body 666 .request 667 .BlockingRequestQueue requestQueue = 668 body.getRequestQueue(); 669 670 while (body.isActive()) 671 { 672 673 674 Request r = requestQueue.blockingRemoveOldest(); 675 String methodName = r.getMethodName(); 676 if (methodName.startsWith("rotate")|| methodName.startsWith("spin")){ 677 processRotate(body, methodName, r); 678 } 679 else{ 680 if (!Election.isRunning()) 681 { 682 body.serve(r); 684 } 685 else if (methodName.equals("addSphere")) 686 { 687 showMessageAll("Cannot add spheres while an election is running"); 690 } 691 else 692 { 693 695 body.serve(r); 696 } 697 } 698 699 } 700 } 702 703 740 746 public void processRotate( 747 org.objectweb.proactive.Body body, 748 String methodName, 749 Request r) 750 { 751 int i_user = 0; 752 try 753 { 754 i_user = ((Integer ) r.getParameter(0)).intValue(); 755 } 756 catch (Exception e) 757 { 758 e.printStackTrace(); 759 } 760 if (methodName.equals("rotateLeft")) 761 { 762 if (Election.isRunning()) 764 { 765 int i_votes = Election.vote(i_user, new Integer (Election.LEFT)); 767 if (i_votes == h_users.size()) 768 { 769 election.finish(); 770 } 771 } 772 else 773 { 774 if (i_left > 0) 776 { 777 showMessage( 778 i_user, 779 "Rendering in progress, request invalid"); 780 } 781 else if (h_users.size() == 1) 782 { 783 showMessage(i_user, "Scene is being rotated left"); 784 body.serve(r); 785 } 786 else 787 { 788 election = 789 new Election(i_user, new Integer (Election.LEFT), this); 790 } 791 } 792 } 793 else if (methodName.equals("rotateRight")) 794 { 795 if (Election.isRunning()) 797 { 798 int i_votes = 800 Election.vote(i_user, new Integer (Election.RIGHT)); 801 if (i_votes == h_users.size()) 802 { 803 election.finish(); 804 } 805 } 806 else 807 { 808 if (i_left > 0) 810 { 811 showMessage( 812 i_user, 813 "Rendering in progress, request invalid"); 814 } 815 else if (h_users.size() == 1) 816 { 817 showMessage(i_user, "Scene is being rotated right"); 818 body.serve(r); 819 } 820 else 821 { 822 election = 823 new Election(i_user, new Integer (Election.RIGHT), this); 824 } 825 } 826 } 827 else if (methodName.equals("rotateUp")) 828 { 829 if (Election.isRunning()) 831 { 832 int i_votes = Election.vote(i_user, new Integer (Election.UP)); 834 if (i_votes == h_users.size()) 835 { 836 election.finish(); 837 } 838 } 839 else 840 { 841 if (i_left > 0) 843 { 844 showMessage( 845 i_user, 846 "Rendering in progress, request invalid"); 847 } 848 else if (h_users.size() == 1) 849 { 850 showMessage(i_user, "Scene is being rotated up"); 851 body.serve(r); 852 } 853 else 854 { 855 election = 856 new Election(i_user, new Integer (Election.UP), this); 857 } 858 } 859 } 860 else if (methodName.equals("rotateDown")) 861 { 862 if (Election.isRunning()) 864 { 865 int i_votes = Election.vote(i_user, new Integer (Election.DOWN)); 867 if (i_votes == h_users.size()) 868 { 869 election.finish(); 870 } 871 } 872 else 873 { 874 if (i_left > 0) 876 { 877 showMessage( 878 i_user, 879 "Rendering in progress, request invalid"); 880 } 881 else if (h_users.size() == 1) 882 { 883 showMessage(i_user, "Scene is being rotated down"); 884 body.serve(r); 885 } 886 else 887 { 888 election = 889 new Election(i_user, new Integer (Election.DOWN), this); 890 } 891 } 892 } 893 else if (methodName.equals("spinClock")) 894 { 895 if (Election.isRunning()) 897 { 898 int i_votes = 900 Election.vote(i_user, new Integer (Election.CLOCKWISE)); 901 if (i_votes == h_users.size()) 902 { 903 election.finish(); 904 } 905 } 906 else 907 { 908 if (i_left > 0) 910 { 911 showMessage( 912 i_user, 913 "Rendering in progress, request invalid"); 914 } 915 else if (h_users.size() == 1) 916 { 917 showMessage(i_user, "Scene is being spinned right"); 918 body.serve(r); 919 } 920 else 921 { 922 election = 923 new Election( 924 i_user, 925 new Integer (Election.CLOCKWISE), 926 this); 927 } 928 } 929 } 930 else if (methodName.equals("spinUnclock")) 931 { 932 if (Election.isRunning()) 934 { 935 int i_votes = 937 Election.vote(i_user, new Integer (Election.UNCLOCKWISE)); 938 if (i_votes == h_users.size()) 939 { 940 election.finish(); 941 } 942 } 943 else 944 { 945 if (i_left > 0) 947 { 948 showMessage( 949 i_user, 950 "Rendering in progress, request invalid"); 951 } 952 else if (h_users.size() == 1) 953 { 954 showMessage(i_user, "Scene is being spinned left"); 955 body.serve(r); 956 } 957 else 958 { 959 election = 960 new Election( 961 i_user, 962 new Integer (Election.UNCLOCKWISE), 963 this); 964 } 965 } 966 } 967 } 968 969 975 public void showMessage(int i_user, String s_message) 976 { 977 ((User) h_users.get(new Integer (i_user))).getObject().showMessage( 978 s_message); 979 } 980 981 987 public void showDialog(int i_user, String subject, String s_message) 988 { 989 ((User) h_users.get(new Integer (i_user))).getObject().dialogMessage( 990 subject, 991 s_message); 992 } 993 994 997 public void showUserMessage(int i_user, String s_message) 998 { 999 ((User) h_users.get(new Integer (i_user))).getObject().showUserMessage( 1000 s_message); 1001 } 1002 1003 1007 public void showMessageAll(String s_message) 1008 { 1009 log(s_message); 1010 for (Enumeration e = h_users.elements(); e.hasMoreElements();) 1011 { 1012 ((User) e.nextElement()).getObject().showMessage(s_message); 1013 } 1014 } 1015 1016 1021 1022 1027 public void showMessageExcept(int i_user, String s_message) 1028 { 1029 log(s_message); 1030 int i; 1031 for (Enumeration e = h_users.keys(); e.hasMoreElements();) 1032 { 1033 i = ((Integer ) e.nextElement()).intValue(); 1034 if (i != i_user) 1035 showMessage(i, s_message); 1036 } 1037 } 1038 1039 1044 public void showDialogExcept(int i_user, String subject, String s_message) 1045 { 1046 int i; 1047 for (Enumeration e = h_users.keys(); e.hasMoreElements();) 1048 { 1049 i = ((Integer ) e.nextElement()).intValue(); 1050 if (i != i_user) 1051 showDialog(i, subject, s_message); 1052 } 1053 } 1054 1055 1060 public void showUserMessageExcept(int i_user, String s_message) 1061 { 1062 log(s_message); 1063 int i; 1064 for (Enumeration e = h_users.keys(); e.hasMoreElements();) 1065 { 1066 i = ((Integer ) e.nextElement()).intValue(); 1067 if (i != i_user) 1068 showUserMessage(i, s_message); 1069 } 1070 } 1071 1072 1077 public static void main(String argv[]) throws NodeException 1078 { 1079 ProActiveDescriptor proActiveDescriptor = null; 1080 ProActiveConfiguration.load(); 1081 Node node = null; 1088 try 1094 { 1095 proActiveDescriptor = 1096 ProActive.getProactiveDescriptor( 1097 "file:"+argv[0]); 1098 proActiveDescriptor.activateMappings(); 1099 } 1101 catch (Exception e) 1102 { 1103 e.printStackTrace(); 1104 logger.error("Pb in main"); 1105 } 1106 1107 1108 1110 VirtualNode dispatcher = 1111 proActiveDescriptor.getVirtualNode("Dispatcher"); 1112 1113 VirtualNode renderer = proActiveDescriptor.getVirtualNode("Renderer"); 1114 String [] rendererNodes = renderer.getNodesURL(); 1115 Object param[] = new Object []{rendererNodes,dispatcher, proActiveDescriptor}; 1116 node = dispatcher.getNode(); 1123 try 1127 { 1128 ProActive.newActive( 1129 "org.objectweb.proactive.examples.c3d.C3DDispatcher", 1130 param, 1131 node); 1132 1133 } 1134 catch (Exception e) 1135 { 1136 e.printStackTrace(); 1137 } 1138 } 1140 1141 1145 public int ping() 1146 { 1147 return 0; 1148 } 1149 1150 1162 public int registerConsumer(C3DUser c3duser, Scene newscene, String s_name) 1163 { 1164 c3duser.log("-> Remote call-back: dispatcher found, user registered"); 1166 1167 log("New user " + s_name + "(" + i_lastuser + ") has joined"); 1168 1169 User newUser = new User(s_name, c3duser); 1170 1171 1172 h_users.put(new Integer (i_lastuser), newUser); 1173 li_users.add(s_name + " (" + i_lastuser + ")"); 1174 1175 1178 1179 for (Enumeration e = h_users.keys(); e.hasMoreElements();) 1181 { 1182 int i = ((Integer ) e.nextElement()).intValue(); 1183 User oldUser = ((User) h_users.get(new Integer (i))); 1184 if (i != i_lastuser) 1185 { 1186 oldUser.getObject().informNewUser(i_lastuser, s_name); 1188 newUser.getObject().informNewUser(i, oldUser.getName()); 1190 } 1191 } 1192 1193 1196 userTotal++; 1197 long elapsed; 1198 long start = System.currentTimeMillis(); 1199 1200 if (c3duser.ping() == 0) 1202 { 1203 elapsed = System.currentTimeMillis() - start; 1205 userPing += (elapsed - userPing) / userTotal; 1206 log( 1207 "Pinging user " 1208 + s_name 1209 + " in " 1210 + elapsed 1211 + " ms. Average: " 1212 + userPing); 1213 } 1214 1215 1218 if (h_users.size() == 1) 1219 { 1220 1221 scene = newscene; 1222 1223 width = c3duser.getWidth().intValue(); 1224 height = c3duser.getHeight().intValue(); 1225 pixels = new int[width * height]; 1226 intervals = 3 * engines; 1227 intheight = height / intervals; 1228 int max; 1229 1230 if (s_name.compareTo("Benchmarking bot") == 0) 1231 max = 50; 1232 else 1233 max = 1; 1234 1235 int engPingTotal = 0; 1237 for (int n = 0; n < engines; n++) 1238 { 1239 for (int i = 1; i < max; i++) 1240 { 1241 engPingTotal++; 1242 start = System.currentTimeMillis(); 1243 if (engine[n].ping() == 1) 1244 { 1245 elapsed = System.currentTimeMillis() - start; 1246 enginePing += (elapsed - enginePing) / engPingTotal; 1247 } 1248 } 1249 } 1250 1251 render(); 1252 } 1253 else 1254 { 1255 1256 Interval inter = new Interval(0, width, height, 0, height, 1); 1257 c3duser.setPixels(pixels, inter); 1258 } 1259 return i_lastuser++; 1260 } 1261 1262 public void registerMigratedUser(int userNumber){ 1263 1264 1265 User user = (User)h_users.get(new Integer (userNumber)); 1266 log("User " + user.getName() + "(" + userNumber + ") has migrated "); 1267 1268 for (Enumeration e = h_users.keys(); e.hasMoreElements();) 1269 { 1270 int i = ((Integer ) e.nextElement()).intValue(); 1271 User oldUser = ((User) h_users.get(new Integer (i))); 1272 if (i != userNumber) 1273 { 1274 user.getObject().informNewUser(i, oldUser.getName()); 1276 } 1277 } 1278 1279 1280 1281 Interval inter = new Interval(0, width, height, 0, height, 1); 1282 user.getObject().setPixels(pixels, inter); 1283 1284 1285 } 1286 1287 1290 public void resetScene(Scene s) 1291 { 1292 if (!int_stack.isEmpty()) 1293 { 1294 showMessageAll("Cannot reset scene while rendering"); 1295 return; 1296 } 1297 else 1298 { 1299 scene = s; 1300 showMessageAll("The scene has been reseted. Rendering..."); 1301 render(); 1302 } 1303 } 1304 1305 1310 public void unregisterConsumer(int number) 1311 { 1312 showMessageExcept(number, "User " + nameOfUser(number) + " left"); 1313 1314 for (Enumeration e = h_users.keys(); e.hasMoreElements();) 1315 { 1316 int i = ((Integer ) e.nextElement()).intValue(); 1317 1318 if (i != number) 1319 { 1320 User user = ((User) h_users.get(new Integer (i))); 1321 user.getObject().informUserLeft(nameOfUser(number)); 1323 } 1324 } 1325 1326 1327 try 1328 { 1329 li_users.remove(nameOfUser(number) + " (" + number + ")"); 1330 } 1331 catch (Exception ex) 1332 { 1333 ex.printStackTrace(); 1334 } 1335 h_users.remove(new Integer (number)); 1336 if (h_users.isEmpty()) 1337 { 1338 int_stack.removeAllElements(); 1342 i_lastuser = 0; 1343 } 1344 } 1345 1346 1351 public String getOSString() 1352 { 1353 return System.getProperty("os.name") 1354 + " " 1355 + System.getProperty("os.version"); 1356 } 1357 1358 public int getIntervals() 1359 { 1360 return intervals; 1361 } 1362 1363 public String getUserList() 1364 { 1365 StringBuffer s_list = new StringBuffer (); 1366 for (Enumeration e = h_users.elements(); e.hasMoreElements();) 1367 { 1368 s_list.append(" " + ((User) e.nextElement()).getName() + "\n"); 1369 } 1370 return s_list.toString(); 1371 } 1372 1373 String nameOfUser(int i_user) 1374 { 1375 return ((User) h_users.get(new Integer (i_user))).getName(); 1376 } 1377 1378 private void trace(String s_message) 1379 { 1380 logger.info("C3DDispatcher: " + s_message); 1381 } 1382 1383 public void addSphere(Sphere s) 1384 { 1385 if (!int_stack.isEmpty()) 1387 { 1388 showMessageAll("Cannot add spheres while rendering"); 1389 return; 1390 } 1391 else 1392 { 1393 scene.addObject((Primitive) s); 1394 showMessageAll( 1395 "A Sphere has been added\nX:" 1396 + Math.floor(s.getCenter().x) 1397 + " Y:" 1398 + Math.floor(s.getCenter().y) 1399 + " Z:" 1400 + Math.floor(s.getCenter().y)); 1401 render(); 1402 log("Scene now contains " + scene.getObjects() + " spheres"); 1403 } 1404 } 1405 1406 1411 public void doBenchmarks() 1412 { 1413 FileWriter bench; 1414 long start; 1415 long elapsed; 1416 1417 for (int n = 0; n < engines; n++) 1418 { 1419 start = System.currentTimeMillis(); 1421 if (engine[n].ping() == 0) 1422 { 1423 elapsed = System.currentTimeMillis() - start; 1424 enginePing += (elapsed - enginePing) / (n + 1); 1425 log( 1426 "Pinging engine " 1427 + n 1428 + "in " 1429 + elapsed 1430 + " ms. Average: " 1431 + enginePing); 1432 } 1433 } 1434 try 1435 { 1436 log("Creating file"); 1437 1438 bench = new FileWriter ("bench" + engines + ".txt"); 1439 1440 bench.write("#Test file for bench generation\n"); 1441 bench.write(new Date ().toString()); 1442 bench.write("#Total number of users:\n" + userTotal); 1443 bench.write("\n#Avg. rd-trip time:\n" + userPing); 1444 bench.write("\n#Number of engines:\n" + engine.length); 1445 bench.write("\n#Avg rd-trip time:\n" + enginePing); 1446 bench.write("\n#Number of renderings:\n" + totalRender); 1447 bench.write("\n#Avg rendering time:\n" + avgRender); 1448 bench.write('\n'); 1449 bench.close(); 1450 log("Closing"); 1451 } 1452 catch (IOException e) 1453 { 1454 log(e.getMessage()); 1455 } 1456 1457 try 1459 { 1460 RandomAccessFile plot = new RandomAccessFile ("papdc.plot", "rw"); 1461 1462 plot.seek(plot.length()); 1464 plot.writeChars("#" + new Date ().toString() + "\n"); 1465 plot.writeChars("" + engines + "\t" + avgRender + "\n"); 1466 plot.close(); 1467 } 1468 catch (Exception ex) 1469 { 1470 return; 1471 } 1472 } 1473 1474 class C3DDispatcherFrame extends Frame implements ActionListener 1475 { 1476 1477 private MenuItem mi_exit, mi_clear, mi_benchmark; 1478 private Font f_standard = new Font ("SansSerif", Font.PLAIN, 12); 1479 private int i_top; 1480 1481 public C3DDispatcherFrame() 1482 { 1483 super("Collaborative 3D Environment - Dispatcher"); 1484 1485 setBackground(Color.lightGray); 1486 addWindowListener(new MyWindowListener()); 1487 setFont(f_standard); 1488 1489 MenuBar mb = new MenuBar (); 1490 Menu m_file = new Menu ("File", false); 1491 m_file.setFont(f_standard); 1492 mi_exit = new MenuItem ("Exit"); 1493 mi_clear = new MenuItem ("Clear log"); 1494 mi_benchmark = new MenuItem ("Benchmarks results"); 1495 1496 mi_clear.setFont(f_standard); 1498 mi_exit.setFont(f_standard); 1499 mi_benchmark.setFont(f_standard); 1500 1501 mi_exit.addActionListener(this); 1503 mi_clear.addActionListener(this); 1504 mi_benchmark.addActionListener(this); 1505 1506 m_file.add(mi_clear); 1507 m_file.addSeparator(); 1508 m_file.add(mi_exit); 1509 m_file.addSeparator(); 1510 m_file.add(mi_benchmark); 1511 mb.add(m_file); 1512 setMenuBar(mb); 1513 1514 GridBagLayout gridbag = new GridBagLayout (); 1515 GridBagConstraints c = new GridBagConstraints (); 1516 setLayout(gridbag); 1517 1518 Label l_header = new Label ("C3D Dispatcher", Label.CENTER); 1519 l_header.setFont( 1520 new Font ("SansSerif", Font.ITALIC + Font.BOLD, 18)); 1521 c.insets = new Insets (12, 10, 0, 10); 1522 c.weightx = 1.0; 1523 c.gridx = 0; 1524 c.gridy = 0; 1525 c.weighty = 0.0; 1526 c.gridwidth = GridBagConstraints.REMAINDER; 1527 gridbag.setConstraints(l_header, c); 1528 add(l_header); 1529 1530 String s_localhost = ""; 1531 try 1532 { 1533 s_localhost = InetAddress.getLocalHost().getCanonicalHostName(); 1534 } 1535 catch (UnknownHostException e) 1536 { 1537 s_localhost = "unknown!"; 1538 } 1539 Label l_machine = 1540 new Label ( 1541 "on machine: " 1542 + s_localhost 1543 + " (" 1544 + System.getProperty("os.name") 1545 + " " 1546 + System.getProperty("os.version") 1547 + ")"); 1548 c.insets = new Insets (0, 10, 5, 10); 1549 c.gridy = 1; 1550 gridbag.setConstraints(l_machine, c); 1551 add(l_machine); 1552 1553 ta_log = new TextArea (10, 35); 1554 ta_log.setEditable(false); 1555 1556 c.anchor = GridBagConstraints.CENTER; 1557 c.weighty = 1.0; 1558 c.gridy++; 1559 c.fill = GridBagConstraints.BOTH; 1560 gridbag.setConstraints(ta_log, c); 1561 add(ta_log); 1562 1563 Label l_users = new Label ("List of current users:", Label.LEFT); 1564 c.gridy++; 1565 c.insets = new Insets (5, 10, 0, 10); 1566 c.anchor = GridBagConstraints.WEST; 1567 gridbag.setConstraints(l_users, c); 1568 add(l_users); 1569 1570 li_users = new List (5); 1571 c.gridy++; 1572 gridbag.setConstraints(li_users, c); 1573 add(li_users); 1574 1575 1578 Panel p_eng = new Panel (); 1579 GridBagLayout p_gl = new GridBagLayout (); 1580 GridBagConstraints pc = new GridBagConstraints (); 1581 1582 p_eng.setLayout(p_gl); 1583 c.gridy++; 1584 c.fill = GridBagConstraints.BOTH; 1585 c.gridwidth = GridBagConstraints.REMAINDER; 1586 gridbag.setConstraints(p_eng, c); 1589 add(p_eng); 1590 1591 Label l_engines = new Label ("Available engines:", Label.CENTER); 1592 pc.weightx = 1.0; 1595 pc.weighty = 1.0; 1596 pc.gridx = 0; 1597 pc.gridy = 0; 1598 pc.insets = new Insets (5, 5, 5, 5); 1599 pc.gridwidth = GridBagConstraints.RELATIVE; 1600 p_gl.setConstraints(l_engines, pc); 1601 p_eng.add(l_engines); 1602 1603 Label l_used = new Label ("Engines used:", Label.CENTER); 1604 pc.gridx = 1; 1605 pc.gridwidth = GridBagConstraints.REMAINDER; 1606 p_gl.setConstraints(l_used, pc); 1607 p_eng.add(l_used); 1608 1609 li_enginesAvailable = new List (5); 1610 li_enginesAvailable.setMultipleMode(true); 1611 pc.fill = GridBagConstraints.BOTH; 1612 pc.gridy = 1; 1613 pc.gridx = 0; 1614 pc.gridwidth = GridBagConstraints.RELATIVE; 1615 p_gl.setConstraints(li_enginesAvailable, pc); 1616 p_eng.add(li_enginesAvailable); 1617 1618 li_enginesUsed = new List (5); 1619 li_enginesUsed.setMultipleMode(true); 1620 pc.gridx = 1; 1621 pc.gridwidth = GridBagConstraints.REMAINDER; 1622 p_gl.setConstraints(li_enginesUsed, pc); 1623 p_eng.add(li_enginesUsed); 1624 1625 b_addEng.addActionListener(this); 1626 pc.fill = GridBagConstraints.NONE; 1627 pc.gridx = 0; 1628 pc.gridy = 2; 1629 pc.gridwidth = GridBagConstraints.RELATIVE; 1630 p_gl.setConstraints(b_addEng, pc); 1631 p_eng.add(b_addEng); 1632 1633 b_rmEng.addActionListener(this); 1634 pc.gridwidth = GridBagConstraints.REMAINDER; 1635 pc.gridx = 1; 1636 p_gl.setConstraints(b_rmEng, pc); 1637 p_eng.add(b_rmEng); 1638 1639 pack(); 1640 setVisible(true); 1641 toFront(); 1642 } 1643 1644 private void exit() 1645 { 1646 try 1647 { 1648 proActiveDescriptor.killall(false); 1651 1652 } 1653 catch (Exception e) 1654 { 1655 trace("WARNING occurs when killing the application!"); 1656 } 1658 try 1659 { 1660 setVisible(false); 1661 dispose(); 1662 System.exit(0); 1663 } 1664 catch (Exception e) 1665 { 1666 e.printStackTrace(); 1667 } 1668 } 1669 1670 public void paint(Graphics g) 1671 { 1672 update(g); 1673 } 1674 1675 public void update(Graphics g) 1676 { 1677 i_top = this.getInsets().top; 1678 g.setColor(Color.gray); 1679 g.drawLine(0, i_top - 1, 2000, i_top - 1); 1680 g.setColor(Color.white); 1681 g.drawLine(0, i_top, 2000, i_top); 1682 } 1683 1684 public void actionPerformed(ActionEvent e) 1685 { 1686 Object source = e.getSource(); 1687 if (source == mi_exit) 1688 exit(); 1689 else if (source == mi_clear) 1690 ta_log.setText(""); 1691 else if (source == mi_benchmark) 1692 doBenchmarks(); 1693 else if (source == b_addEng) 1694 { 1695 String sel[] = li_enginesAvailable.getSelectedItems(); 1696 if (sel != null) 1697 { 1698 for (int i = 0; i < sel.length; i++) 1699 { 1700 li_enginesUsed.add(sel[i]); 1701 try 1702 { 1703 li_enginesAvailable.remove(sel[i]); 1704 } 1705 catch (Exception l_ex) 1706 { 1707 l_ex.printStackTrace(); 1708 } 1709 } 1710 } 1711 } 1712 else if (source == b_rmEng) 1713 { 1714 String sel[] = li_enginesUsed.getSelectedItems(); 1715 if (sel != null) 1716 { 1717 for (int i = 0; i < sel.length; i++) 1718 { 1719 li_enginesAvailable.add(sel[i]); 1720 try 1721 { 1722 li_enginesUsed.remove(sel[i]); 1723 } 1724 catch (Exception l_ex) 1725 { 1726 l_ex.printStackTrace(); 1727 } 1728 } 1729 } 1730 } 1731 } 1732 1733 1736 class MyWindowListener extends WindowAdapter 1737 { 1738 1739 public void windowClosing(WindowEvent e) 1740 { 1741 exit(); 1742 } 1743 } 1744 } 1745 1746} 1747 1748class Election extends Thread 1749{ 1750 1751 public static final int UP = 0; 1752 public static final int DOWN = 1; 1753 public static final int LEFT = 2; 1754 public static final int RIGHT = 3; 1755 public static final int CLOCKWISE = 4; 1756 public static final int UNCLOCKWISE = 5; 1757 1760 private static final int WAITSECS = 4; 1761 private static boolean running = false; 1762 private static Hashtable wishes; 1763 private static C3DDispatcher c3ddispatcher; 1764 1765 public Election(int i_user, Integer wish, C3DDispatcher c3ddispatcher) 1766 { 1767 Election.c3ddispatcher = c3ddispatcher; 1768 Election.running = true; 1769 wishes = new Hashtable (); 1770 vote(i_user, wish); 1771 c3ddispatcher.showMessage( 1772 i_user, 1773 "Request 'rotate " 1774 + voteString(wish) 1775 + "' submitted, \nnew " 1776 + WAITSECS 1777 + " second election started ..."); 1778 c3ddispatcher.showMessageExcept( 1779 i_user, 1780 "New " + WAITSECS + " second election started:"); 1781 c3ddispatcher.showMessageExcept( 1782 i_user, 1783 " User " 1784 + c3ddispatcher.nameOfUser(i_user) 1785 + " wants to rotate " 1786 + voteString(wish)); 1787 1788 1790 this.start(); 1792 1793 } 1794 1795 public synchronized void run() 1796 { 1797 try 1798 { 1799 wait(WAITSECS * 1000); 1800 } 1801 catch (InterruptedException e) 1802 { 1803 e.printStackTrace(); 1804 } 1805 1806 int score[] = { 0, 0, 0, 0, 0, 0 }; 1807 1808 int i_user; 1809 1810 c3ddispatcher.showMessageAll("Election finished"); 1811 for (Enumeration e = wishes.keys(); e.hasMoreElements();) 1812 { 1813 i_user = ((Integer ) e.nextElement()).intValue(); 1814 Integer wish = (Integer ) wishes.get(new Integer (i_user)); 1815 c3ddispatcher.showMessageAll( 1816 " User " 1817 + c3ddispatcher.nameOfUser(i_user) 1818 + " voted '" 1819 + voteString(wish) 1820 + "'"); 1821 1822 score[wish.intValue()]++; 1824 } 1825 c3ddispatcher.showMessageAll( 1826 " Result:\n " 1827 + score[RIGHT] 1828 + " right, " 1829 + score[LEFT] 1830 + " left [rotate]\n " 1831 + score[UP] 1832 + " up ," 1833 + score[DOWN] 1834 + " down [vertical]\n " 1835 + score[CLOCKWISE] 1836 + " right," 1837 + score[UNCLOCKWISE] 1838 + " left [spin]"); 1839 1840 int winner = -1; 1842 for (int i = 0; i < score.length; i++) 1843 { 1844 if (score[i] == wishes.size()) 1846 winner = i; 1847 } 1848 1849 switch (winner) 1850 { 1851 case UP : 1852 c3ddispatcher.showMessageAll( 1853 " The scene will be rotated up."); 1854 Election.running = false; 1855 Election.wishes.clear(); 1856 c3ddispatcher.rotateUp(0); 1857 break; 1858 case DOWN : 1859 c3ddispatcher.showMessageAll( 1860 " The scene will be rotated down."); 1861 Election.running = false; 1862 Election.wishes.clear(); 1863 c3ddispatcher.rotateDown(0); 1864 break; 1865 case LEFT : 1866 c3ddispatcher.showMessageAll( 1867 " The scene will be rotated left."); 1868 Election.running = false; 1869 Election.wishes.clear(); 1870 c3ddispatcher.rotateLeft(0); 1871 break; 1872 case RIGHT : 1873 c3ddispatcher.showMessageAll( 1874 " The scene will be rotated right."); 1875 Election.running = false; 1876 Election.wishes.clear(); 1877 c3ddispatcher.rotateRight(0); 1878 break; 1879 case CLOCKWISE : 1880 c3ddispatcher.showMessageAll( 1881 " The scene will be spinned right"); 1882 Election.running = false; 1883 Election.wishes.clear(); 1884 c3ddispatcher.spinClock(0); 1885 break; 1886 case UNCLOCKWISE : 1887 c3ddispatcher.showMessageAll( 1888 " The scene will be spinned left"); 1889 Election.running = false; 1890 Election.wishes.clear(); 1891 c3ddispatcher.spinUnclock(0); 1892 break; 1893 default : 1894 c3ddispatcher.showMessageAll( 1895 " No consensus found, vote again please!"); 1896 } 1897 1898 Election.running = false; 1899 Election.wishes.clear(); 1900 } 1901 1902 public synchronized static int vote(int i_user, Integer wish) 1903 { 1904 if (wishes.containsKey(new Integer (i_user))) 1905 c3ddispatcher.showMessage( 1906 i_user, 1907 "You have already voted in this round"); 1908 else 1909 wishes.put(new Integer (i_user), wish); 1910 return wishes.size(); 1911 } 1912 1913 public synchronized static boolean isRunning() 1914 { 1915 return running; 1916 } 1917 1918 public String voteString(Integer wish) 1919 { 1920 String ret; 1921 switch (wish.intValue()) 1922 { 1923 case UP : 1924 ret = "up"; 1925 break; 1926 case DOWN : 1927 ret = "down"; 1928 break; 1929 case LEFT : 1930 ret = "left"; 1931 break; 1932 case RIGHT : 1933 ret = "right"; 1934 break; 1935 case CLOCKWISE : 1936 ret = "clockwise"; 1937 break; 1938 case UNCLOCKWISE : 1939 ret = "unclockwise"; 1940 break; 1941 default : 1942 ret = "error"; 1943 break; 1944 } 1945 return ret; 1946 } 1947 1948 public synchronized void finish() 1949 { 1950 c3ddispatcher.showMessageAll("Everybody voted"); 1951 this.notify(); 1952 } 1953} 1954 1955class User 1956{ 1957 1958 private String name; 1959 private C3DUser c3duser; 1960 1961 User(String name, C3DUser c3duser) 1962 { 1963 this.name = name; 1964 this.c3duser = c3duser; 1965 } 1966 1967 String getName() 1968 { 1969 return name; 1970 } 1971 1972 C3DUser getObject() 1973 { 1974 return c3duser; 1975 } 1976} 1977 | Popular Tags |