001    package cnslab.gui;
002
003    import java.awt.*;
004    import java.util.Arrays;
005
006    import jaxe.*;
007    import jpvm.*;
008    import org.slf4j.Logger;
009    import org.slf4j.LoggerFactory;
010    import thinlet.*;
011
012    import cnslab.cnsnetwork.*;
013
014    /***********************************************************************
015    * Graphic interface for the simulator.
016    * 
017    * @version
018    *   $Date: 2011-09-10 20:21:56 -0500 (Sat, 10 Sep 2011) $
019    *   $Rev: 222 $
020    *   $Author: croft $
021    * @author
022    *   Yi Dong
023    * @author
024    *   David Wallace Croft
025    ***********************************************************************/
026    public final class  mainErnst
027      extends Thinlet
028    ////////////////////////////////////////////////////////////////////////
029    ////////////////////////////////////////////////////////////////////////
030    {
031
032    private static final long  serialVersionUID = 0L;
033    
034    private static final Class<mainErnst>
035      CLASS = mainErnst.class;
036    
037    private static final Logger
038      LOGGER = LoggerFactory.getLogger ( CLASS );    
039
040    /** Graphic console */
041    public GConsole  g;
042
043    public Object    console;
044    
045    public Object    status;
046    
047    public Object    start_button;
048
049    public Process   process;
050    
051    ////////////////////////////////////////////////////////////////////////
052    // static methods
053    ////////////////////////////////////////////////////////////////////////
054    
055    public static void  main ( final String [ ]  args )
056      throws jpvmException, Exception
057    ////////////////////////////////////////////////////////////////////////
058    {
059      final mainErnst  main = new mainErnst ( );
060      
061      new FrameLauncher (
062        "Ernst Simulator",
063        main,
064        350,
065        800 );
066      
067      new Thread ( new ContentProcessResult ( main ) ).start ( );
068      
069      new Thread ( new ContentProcess ( main ) ).start ( );
070      
071      // System.out.println("here");
072    }
073    
074    ////////////////////////////////////////////////////////////////////////
075    // constructor methods
076    ////////////////////////////////////////////////////////////////////////
077    
078    public  mainErnst ( )
079      throws Exception, jpvmException
080    ////////////////////////////////////////////////////////////////////////
081    {
082      add ( parse ( "ernstGui.xml" ) );
083      
084      console = find ( "console" );
085      
086      status = find ( "statusText" );
087      
088      start_button = find ( "daemonToggle" );
089      
090      g = new GConsole ( );
091      
092      initServer ( );
093    }
094
095    ////////////////////////////////////////////////////////////////////////
096    ////////////////////////////////////////////////////////////////////////
097    
098    /***********************************************************************
099    * Process the user command in the console 
100    * 
101    * @throws jpvmException
102    * @throws Exception
103    ***********************************************************************/
104    public void  userCommandProcess ( )
105      throws jpvmException, Exception
106    ////////////////////////////////////////////////////////////////////////
107    {
108      final String
109        comm = getString (
110          getSelectedItem ( find ( "commandGroup" ) ),
111          "text" );
112      
113      if ( comm.equals ( "Add a host" ) )
114      {
115        // setBoolean(find("addHost"),"visible",true);
116        
117        add ( parse ( "addDialog.xml" ) );
118
119        setString (
120          status,
121          "text",
122          g.jpvm.pvm_config ( ).numHosts
123            + " hosts, this daemon listening at:"
124            + g.jpvm.readDaemonFile ( ) );
125      }
126      else if ( comm.equals ( "Add hosts from file" ) )
127      {
128        setString (
129          console,
130          "text",
131          getString ( console, "text" )
132            + "jpvm>add hosts from myhosts\n"
133            + g.AddAll ( ) );
134        
135        setString (
136          status,
137          "text",
138          g.jpvm.pvm_config ( ).numHosts
139            + " hosts, this daemon listening at:"
140            + g.jpvm.readDaemonFile ( ) );
141      }
142      else if ( comm.equals ( "Clear console" ) )
143      {
144        setString ( console, "text", "" );
145        
146        // setScroll(console, (float)(-1), (float)0.99);
147      }
148      else if ( comm.equals ( "Delete a host" ) )
149      {
150        final Object  delDia = parse ( "delDialog.xml" );
151        
152        add ( delDia );
153
154        final jpvmConfiguration  conf = g.jpvm.pvm_config ( );
155        
156        for ( int  i = 0; i < conf.numHosts; i++ )
157        {
158          final Object  o = create ( "choice" );
159          
160          setString (
161            o,
162            "text",
163            conf.hostNames [ i ] );
164          
165          add (
166            find (
167              delDia,
168              "delHostList" ),
169            o );
170        }
171
172        setString (
173          status,
174          "text",
175          g.jpvm.pvm_config ( ).numHosts
176            + " hosts, this daemon listening at:"
177            + g.jpvm.readDaemonFile ( ) );
178      }
179      else if ( comm.equals ( "Delete all hosts" ) )
180      {
181        setString (
182          console,
183          "text",
184          getString (
185            console,
186            "text" )
187            + "jpvm>delete all hosts\n" + g.DelAllHosts ( ) );
188        
189        setString (
190          status,
191          "text",
192          g.jpvm.pvm_config ( ).numHosts
193            + " hosts, this daemon listening at:"
194            + g.jpvm.readDaemonFile ( ) );
195      }
196      else if ( comm.equals ( "Kill all the processes" ) )
197      {
198        setString (
199          console,
200          "text",
201          getString (
202            console,
203            "text" )
204            + "jpvm>kill all processes\n"
205            + g.Delete ( ) );
206      }
207      else if ( comm.equals ( "List hosts" ) )
208      {
209        setString (
210          console,
211          "text",
212          getString (
213            console,
214            "text" )
215            + "jpvm>list hosts\n"
216            + g.Conf ( ) );
217        
218        // setScroll(console, (float)(-1), (float)0.99);
219      }
220      else if ( comm.equals ( "Show processes" ) )
221      {
222        setString (
223          console,
224          "text",
225          getString (
226            console,
227            "text" )
228            + "jpvm>show processes\n"
229            + g.Ps ( ) );
230      }
231
232      new Thread (
233        new UpdateScroll (
234          this,
235          console,
236          -1,
237          0.99f ) ).start ( );
238    }
239
240    /***********************************************************************
241    * start the JPVM daemon  
242    * 
243    * @throws jpvmException
244    * @throws Exception
245    ***********************************************************************/
246    public void  startServer ( )
247      throws jpvmException,Exception
248    ////////////////////////////////////////////////////////////////////////
249    {
250      new Thread (
251        new UpdateMessage (
252          this,
253          find ( "serverStatus" ),
254          "Please wait 3 secs..." ) ).start ( );
255      
256      if ( getString ( start_button, "text" ).equals ( "Start" ) )
257      {
258        final Object  cus_checkbox = find ( "cusPort" );
259        
260        // TODO:  Why is this not used?
261        final Object  ran_checkbox = find ( "ranPort" );
262        
263        if ( getBoolean ( cus_checkbox, "selected" ) )
264        {
265          final String [ ]  args = new String [ 3 ];
266          
267          args [ 0 ] = "java";
268          
269          args [ 1 ] = "jpvm.jpvmDaemon";
270          
271          final Object  cus_text = find ( "customPort" );
272          
273          args [ 2 ] = getString ( cus_text, "text" );
274          
275          try
276          {
277            Integer.parseInt ( args [ 2 ] );
278          }
279          catch ( final NumberFormatException  nfe )
280          {
281            getToolkit ( ).beep ( );
282            
283            return;
284          }
285          
286          LOGGER.info ( Arrays.toString ( args ) );
287          
288          try
289          {
290// TODO:  Is this intentionally hiding the instance variable "process"?            
291            final Process  process = Runtime.getRuntime ( ).exec ( args );
292          }
293          catch ( final Exception  e )
294          {
295            e.printStackTrace ( );
296          }
297        }
298        else
299        {
300          final String [ ]  args = new String [ ] {
301            "java",
302            "jpvm.jpvmDaemon" };
303            
304          LOGGER.info ( Arrays.toString ( args ) );
305          
306          try
307          { 
308// TODO:  Is this intentionally hiding the instance variable "process"?
309            
310            final Process  process = Runtime.getRuntime ( ).exec ( args );
311          }
312          catch ( final Exception  e )
313          {
314            e.printStackTrace ( );
315          }
316        }
317      }
318      else if ( getString ( start_button, "text" ).equals ( "Stop" ) )
319      {
320        g.jpvm.pvm_exit ( );
321        
322        g.jpvm.pvm_halt ( );
323      }
324      
325      Thread.sleep ( 3000 );
326      
327      initServer ( );
328      
329      setString (
330        find ( "serverStatus" ),
331        "text",
332        "" );
333    }
334
335    /***********************************************************************
336    * added one host 
337    * 
338    * @param okay
339    * 
340    * @throws jpvmException
341    * @throws Exception
342    ***********************************************************************/
343    public void  addHostNow ( final Object  okay )
344      throws jpvmException, Exception
345    ////////////////////////////////////////////////////////////////////////
346    {
347      if ( getString ( okay, "text" ).equals ( "Add" ) )
348      {
349        int port;
350        
351        try
352        {
353          port = Integer.parseInt (
354            getString (
355              find ( "portNumInt" ),
356              "text" ) );
357        }
358        catch ( final NumberFormatException  nfe )
359        {
360          getToolkit ( ).beep ( );
361          
362          return;
363        }
364        
365        setString (
366          console,
367          "text",
368          getString (
369            console,
370            "text" )
371            + "jpvm>add a host\n"
372            + g.Add (
373              getString (
374                find ( "hostNameStr" ),
375                "text" ),
376              port ) );
377        
378        setString (
379          status,
380          "text",
381          g.jpvm.pvm_config ( ).numHosts
382            + " hosts, this daemon listening at:"
383            + g.jpvm.readDaemonFile ( ) );
384      }
385      else
386      {
387        // TODO:  Why is this block empty?
388      }
389      
390      remove ( find ( "addHost" ) );
391    }
392
393    /***********************************************************************
394    * Delete one host
395    *  
396    * @param okay
397    * 
398    * @throws jpvmException
399    * @throws Exception
400    ***********************************************************************/
401    public void  delHostNow ( final Object  okay )
402      throws jpvmException, Exception
403    ////////////////////////////////////////////////////////////////////////
404    {
405      if ( getString ( okay, "text" ).equals ( "Del" ) )
406      {
407        // setString (
408        //   console,
409        //    "text",
410        //    getString ( console, "text" )
411        //      + "jpvm>add a host\n"
412        //      + g.Add (getString(find("hostNameStr"),"text"),port));
413        
414        setString (
415          console,
416          "text",
417          getString ( console, "text" )
418            + "jpvm>del a host\n"
419            + g.DelHost (
420              getString (
421                getSelectedItem (
422                  find ( "delHostList" ) ),
423                "text" ) ) );
424        
425        setString (
426          status,
427          "text",
428          g.jpvm.pvm_config ( ).numHosts
429            + " hosts, this daemon listening at:"
430            + g.jpvm.readDaemonFile ( ) );
431      }
432      else
433      {
434        // TODO:  Why is this block empty?
435      }
436      
437      remove ( find ( "delHost" ) );
438    }
439
440    /***********************************************************************
441    * The part starts the main simulation 
442    * 
443    * @param button
444    * 
445    * @throws Exception
446    * @throws jpvmException
447    ***********************************************************************/
448    public void  startSimulation ( final Object  button )
449      throws Exception, jpvmException
450    ////////////////////////////////////////////////////////////////////////
451    {
452      if ( getString ( button, "text" ).equals ( "Start" ) )
453      {
454        // start the simulation
455        
456        setString (
457          find ( "showProgress" ),
458          "text",
459          "" );
460        
461        // avalanche or not
462        
463        if ( getBoolean ( find ( "avalanch" ), "selected" ) )
464        {
465          // yes
466          
467          final String [ ]  args = new String [ ] {
468            "java",
469            "-Xmx" + getString ( find ( "mHeap" ), "text" ) + "m",
470            "starter",
471            getString ( getSelectedItem ( find ( "xmlModles" ) ), "text" ),
472            getString ( find ( "seedInt" ), "text" ),
473            getString ( find ( "nHeap" ), "text" ),
474            "-a" };
475          
476          LOGGER.info ( Arrays.toString ( args ) );
477          
478          process = Runtime.getRuntime ( ).exec ( args );
479          
480          final OutputListen
481            output = new OutputListen ( process.getInputStream ( ), this );
482          
483          output.start ( );
484        }
485        else
486        {
487          // no
488          
489          final String mHeap = getString ( find ( "mHeap" ), "text" );
490          
491          final String  xmlModels = getString (
492            getSelectedItem ( find ( "xmlModles" ) ), "text" );
493          
494          final String  seedInt = getString ( find ( "seedInt" ), "text" );
495          
496          final String nHeap = getString ( find ( "nHeap" ), "text" );
497          
498          final String [ ]  args = new String [ ] {
499            "java",
500            "-Xmx" + mHeap + "m",
501            "starter",
502            xmlModels,
503            seedInt,
504            nHeap };
505          
506          LOGGER.info ( Arrays.toString ( args ) );
507            
508          process = Runtime.getRuntime ( ).exec ( args );
509          
510          final OutputListen
511            output = new OutputListen ( process.getInputStream ( ), this );
512          
513          output.start ( );
514        }
515        
516        setBoolean (
517          find ( "startSim" ),
518          "enabled",
519          false );
520        
521        setBoolean (
522          find ( "stopSim" ),
523          "enabled",
524          true );
525      }
526      else
527      {
528        process.destroy ( );
529        
530        g.Delete ( );
531        
532        setBoolean (
533          find ( "startSim" ),
534          "enabled",
535          true );
536        
537        setBoolean (
538          find ( "stopSim"),
539          "enabled",
540          false );
541        
542        setString (
543          find ( "simStatusDisplay" ),
544          "text",
545          getString (
546            find ( "simStatusDisplay" ),
547            "text" )
548            + "Simulation is killed\n" );
549        
550        new Thread (
551          new UpdateScroll (
552            this,
553            find ( "simStatusDisplay" ),
554            -1,
555            0.99f ) ).start ( );
556        
557        setString (
558          find ( "showProgress" ),
559          "text",
560          "" );
561      }
562      
563      /*
564      process = Runtime.getRuntime ( ).exec ( args );
565      
566      // if(process ==null) throw new RuntimeException("process null");
567
568      jpvmDaemon.int2P[jpvmDaemon.pointer]=process;
569      
570      // System.out.println(
571      // process+" id:"+jpvmDaemon.pointer
572      // +" pro:"+jpvmDaemon.int2P[jpvmDaemon.pointer]);
573
574      jpvmDaemon.pointer++;
575      
576      if(jpvmDaemon.pointer==jpvmDaemon.maxHosts)jpvmDaemon.pointer=0;
577      
578      jpvmOutputListen
579        output = new jpvmOutputListen(process.getInputStream());
580        
581      output.start();
582      */
583    }
584
585    public void  getContent ( )
586    ////////////////////////////////////////////////////////////////////////
587    {
588      setBoolean (
589        find ( "startSim" ),
590        "enabled",
591        false );
592      
593      setBoolean (
594        find ( "stopSim" ),
595        "enabled",
596        false );
597      
598      setBoolean (
599        find ( "showXML" ),
600        "enabled",
601        false );
602      
603      new Thread (
604        new ContentProcess ( this ) ).start ( );
605
606      //  System.out.println("getContent");
607      
608      /*
609      File dir = new File("model");
610      
611      FilenameFilter filter = new FilenameFilter() {
612        public boolean accept(File dir, String name) {
613          return name.endsWith(".xml") || name.endsWith(".XML");
614        }
615      };
616      
617      String[] children = dir.list(filter);
618
619      java.util.Arrays.sort(children);
620      
621      for( int i=0;i < children.length; i++)
622      {
623        // System.out.println("file:"+children[i]);
624 
625        Object o = create("choice");
626        
627        setString(o,"text",children[i]);
628        
629        add(find("xmlModles"),o);
630      }
631
632      if(getSelectedItem(find("xmlModles"))!=null)
633      {
634        System.out.println(
635          getString(getSelectedItem(find("xmlModles")),"text"));
636          
637        SimulatorParser sim = new SimulatorParser();
638        
639        String  out = sim.validate(
640          getString(getSelectedItem(find("xmlModles")),"text"));
641          
642        if(out.equals(""))
643        {
644          System.out.println("ok");
645        }
646        else
647        {
648          System.out.println("error format");
649          System.out.println(out);
650        }
651      }
652      */
653    }
654
655    public void  getResults ( )
656    ////////////////////////////////////////////////////////////////////////
657    {
658      new Thread ( new ContentProcessResult ( this ) ).start ( );
659    }
660
661    public void  showPic ( final Object  tree )
662      throws Exception
663    ////////////////////////////////////////////////////////////////////////
664    {
665      // System.out.println(getString(getSelectedItem(tree),"text"));
666      
667      // System.out.println(getString(getSelectedItem(tree),"name"));
668      
669      if ( getString ( getSelectedItem ( tree ), "name" ) != null )
670      {
671        final String [ ]
672          arg = getString (
673            getSelectedItem ( tree ),
674            "name" ).split ( "," );
675
676        final String
677          fileName = getString (
678            getSelectedItem ( find ( "resultsCombo" ) ),
679            "text" );
680        
681        final PlotResult  pr = new PlotResult ( "test" );
682        
683        PlotResult.init ( "results/" + fileName );
684
685        Container  frame = this;
686        
687        while ( !( frame instanceof Frame ) )
688        { 
689          frame = frame.getParent ( ); 
690        }
691        
692        if ( frame.getComponentCount ( ) == 2 )
693        {
694          frame.remove ( frame.getComponent ( 1 ) );
695        }
696        else
697        {
698          // System.out.println("Subcomponent number: "
699          // +((Container)frame.getComponent(0)).getComponentCount());
700          
701          // frame.add(pr.getFrame(Integer.parseInt(arg[0]),
702          // Integer.parseInt(arg[1]),Integer.parseInt(arg[2])),"East");
703          
704          pr.getFrame (
705            Integer.parseInt ( arg [ 0 ] ),
706            Integer.parseInt ( arg [ 1 ] ),
707            Integer.parseInt ( arg [ 2 ] ) );
708        }
709
710        // ((Frame)frame).pack();
711        
712        // frame.setVisible(true);
713
714        PlotResult.stop ( );
715      }
716      else
717      {
718        // setBoolean(
719        //   getSelectedItem(tree),
720        //   "expanded",
721        //   getBoolean(getSelectedItem(tree),"expanded")^true);
722      }
723    }
724
725    public void  showXMLFile ( )
726      throws Exception
727    ////////////////////////////////////////////////////////////////////////
728    {
729      // System.out.println(
730      //   getString(getSelectedItem(find("xmlModles")),"text"));
731      
732      final String [ ]  args = new String [ ] {
733        "model/" + getString (
734          getSelectedItem ( find ( "xmlModles" ) ),
735          "text" ) };
736      
737      Jaxe.main ( args, this );
738      
739      //System.out.println("Editor finished");
740      //new Thread(new contentProcess(main)).start();
741
742      // int nbconf = 0;
743
744      //  File dir = new File("config");
745      //     if (!dir.exists()) {
746      //              JOptionPane.showMessageDialog(
747      // null, JaxeResourceBundle.getRB().getString("erreur.DossierConfig"),
748      // JaxeResourceBundle.getRB().getString("config.ErreurLancement"),
749      // JOptionPane.ERROR_MESSAGE);
750      //            System.exit(1);
751      //            }
752      //
753      //             if (args.length > 0)
754      //              Jaxe.ouvrir(new File(args[0]), null);
755      //       else if (nbconf > 1) {
756      //       synchronized (lock) { // synchronisation avec MacJaxe.ouvrir
757      //              if (Jaxe.allFrames.size() == 0)
758      //                 Jaxe.dialogueDepart();
759      //     /          }
760      //    } else if (nbconf == 1)
761      //       Jaxe.nouveau(null);
762      
763      /*
764      try {
765      // workaround a bug in WebStart with file permissions on windows
766      try {
767      System.setSecurityManager(null);
768      } catch (Throwable t) {
769      t.printStackTrace();
770      }
771      String [] args = new String[1];
772      args[0]="model/"+getString(getSelectedItem(find("xmlModles")),"text");
773      XMLEditor editor = new XMLEditor(args);
774      editor.run();
775      } catch (MerlotException e) {
776      e.printStackTrace();
777      // MerlotDebug.exception(e);
778      }
779      */
780    }
781
782    ////////////////////////////////////////////////////////////////////////
783    // private methods
784    ////////////////////////////////////////////////////////////////////////
785    
786    /***********************************************************************
787    * Initialzed the server 
788    * 
789    * @throws Exception
790    ***********************************************************************/
791    private void  initServer ( )
792      throws Exception
793    ////////////////////////////////////////////////////////////////////////
794    {
795      try
796      {
797        if ( g.testDaemon ( ) )
798        {
799          setString (
800            status,
801            "text",
802            g.jpvm.pvm_config ( ).numHosts
803              + " hosts, this daemon listening at:"
804              + g.jpvm.readDaemonFile ( ) );
805          
806          g.Delete ( );
807          
808          setBoolean (
809            console,
810            "visible",
811            true );
812          
813          setBoolean (
814            find ( "Ernst" ),
815            "enabled",
816            true );
817          
818          setBoolean (
819            find ( "commandPanel" ),
820            "visible",
821            true );
822          
823          setBoolean (
824            find ( "portNamePanel" ),
825            "visible",
826            false );
827          
828          setString (
829            console,
830            "text",
831            g.Conf ( ) );
832          
833          setString (
834            start_button,
835            "text",
836            "Stop" );
837        }
838        else
839        {
840          setString (
841            status,
842            "text",
843            "warning, The jpvm daemon is not detected!" );
844          
845          setBoolean (
846            console,
847            "visible",
848            false );
849          
850          setBoolean (
851            find ( "Ernst" ),
852            "enabled",
853            false );
854          
855          setBoolean (
856            find ( "commandPanel" ),
857            "visible", false );
858          
859          setBoolean (
860            find ( "portNamePanel" ),
861            "visible",
862            true );
863          
864          setString (
865            start_button,
866            "text",
867            "Start" );
868        }
869      }
870      catch ( final jpvmException  e )
871      {
872        e.printStackTrace ( );
873      }
874    }
875
876    ////////////////////////////////////////////////////////////////////////
877    ////////////////////////////////////////////////////////////////////////
878    }