From ad86e94b3d69b0f9d525758a7e00dab86e11970f Mon Sep 17 00:00:00 2001
From: Adam Frisby
Date: Thu, 5 Jul 2007 04:23:34 +0000
Subject: * Tweaks to Java engine (uses less threads). Added support for
 OnFrame and OnNewPresence events.

---
 .../scripting/Engines/JVMEngine/OpenSimJVM.cs      | 148 +++++++++++----------
 1 file changed, 78 insertions(+), 70 deletions(-)

diff --git a/OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs b/OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs
index aaa1609..3e083cc 100644
--- a/OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs
+++ b/OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs
@@ -47,11 +47,6 @@ namespace OpenSim.Scripting.EmbeddedJVM
 
         ScriptInfo scriptInfo;
 
-        public JVMScript()
-        {
-
-        }
-
         public void Initialise(ScriptInfo info)
         {
             scriptInfo = info;
@@ -59,81 +54,29 @@ namespace OpenSim.Scripting.EmbeddedJVM
             _mainMemory = new MainMemory();
             Thread.GlobalMemory = this._mainMemory;
             Thread.World = info.world;
-            compileThread = new System.Threading.Thread(new ThreadStart(CompileScript));
-            compileThread.IsBackground = true;
-            compileThread.Start();
+            CompileScript();
 
+            scriptInfo.events.OnFrame += new EventManager.OnFrameDelegate(events_OnFrame);
+            scriptInfo.events.OnNewPresence += new EventManager.OnNewPresenceDelegate(events_OnNewPresence);
         }
 
-        public string getName()
+        void events_OnNewPresence(ScenePresence presence)
         {
-            return "JVM Scripting Engine";
-        }
-
-        public void LoadScript(string script)
-        {
-            Console.WriteLine("OpenSimJVM - loading new script: " + script);
-            CompileInfo comp = new CompileInfo();
-            comp.script = script;
-            comp.scriptName = script;
-            this.CompileScripts.Enqueue(comp);
-        }
-
-        public void CompileScript()
-        {
-            while (true)
+            for (int i = 0; i < this._threads.Count; i++)
             {
-                CompileInfo comp = this.CompileScripts.Dequeue();
-                string script = comp.script;
-                string scriptName = comp.scriptName;
-                try
-                {
-                    //need to compile the script into a java class file
-
-                    //first save it to a java source file
-                    TextWriter tw = new StreamWriter(scriptName + ".java");
-                    tw.WriteLine(script);
-                    tw.Close();
-
-                    //now compile
-                    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("javac.exe", "*.java");
-                   // psi.RedirectStandardOutput = true;
-                    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
-                    psi.UseShellExecute = false;
-
-                    System.Diagnostics.Process javacomp;
-                    javacomp = System.Diagnostics.Process.Start(psi);
-                    javacomp.WaitForExit();
-
-                    
-                    //now load in class file
-                    ClassRecord class1 = new ClassRecord();
-                    class1.LoadClassFromFile(scriptName + ".class");
-                    class1.PrintToConsole();
-                    //Console.WriteLine();
-                    this._mainMemory.MethodArea.Classes.Add(class1);
-                    class1.AddMethodsToMemory(this._mainMemory.MethodArea);
-
-                    Thread newThread = new Thread();
-                    this._threads.Add(newThread);
-                    newThread.currentClass = class1;
-                    newThread.scriptInfo = scriptInfo;
-
-                    //now delete the created files
-                    System.IO.File.Delete(scriptName + ".java");
-                    System.IO.File.Delete(scriptName + ".class");
-                    //this.OnFrame();
-                }
-                catch (Exception e)
+                if (!this._threads[i].running)
                 {
-                    Console.WriteLine("exception");
-                    Console.WriteLine(e.StackTrace);
-                    Console.WriteLine(e.Message);
+                    this._threads[i].StartMethod("OnNewPresence");
+                    bool run = true;
+                    while (run)
+                    {
+                        run = this._threads[i].Excute();
+                    }
                 }
             }
         }
 
-        public void OnFrame()
+        void events_OnFrame()
         {
             for (int i = 0; i < this._threads.Count; i++)
             {
@@ -149,6 +92,71 @@ namespace OpenSim.Scripting.EmbeddedJVM
             }
         }
 
+        public string getName()
+        {
+            return "JVM Scripting Engine";
+        }
+
+        public void LoadScript(string script)
+        {
+            Console.WriteLine("OpenSimJVM - loading new script: " + script);
+            CompileInfo comp = new CompileInfo();
+            comp.script = script;
+            comp.scriptName = script;
+            this.CompileScripts.Enqueue(comp);
+        }
+
+        public void CompileScript()
+        {
+            CompileInfo comp = this.CompileScripts.Dequeue();
+            string script = comp.script;
+            string scriptName = comp.scriptName;
+            try
+            {
+                //need to compile the script into a java class file
+
+                //first save it to a java source file
+                TextWriter tw = new StreamWriter(scriptName + ".java");
+                tw.WriteLine(script);
+                tw.Close();
+
+                //now compile
+                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("javac.exe", "*.java");
+                // psi.RedirectStandardOutput = true;
+                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
+                psi.UseShellExecute = false;
+
+                System.Diagnostics.Process javacomp;
+                javacomp = System.Diagnostics.Process.Start(psi);
+                javacomp.WaitForExit();
+
+
+                //now load in class file
+                ClassRecord class1 = new ClassRecord();
+                class1.LoadClassFromFile(scriptName + ".class");
+                class1.PrintToConsole();
+                //Console.WriteLine();
+                this._mainMemory.MethodArea.Classes.Add(class1);
+                class1.AddMethodsToMemory(this._mainMemory.MethodArea);
+
+                Thread newThread = new Thread();
+                this._threads.Add(newThread);
+                newThread.currentClass = class1;
+                newThread.scriptInfo = scriptInfo;
+
+                //now delete the created files
+                System.IO.File.Delete(scriptName + ".java");
+                System.IO.File.Delete(scriptName + ".class");
+                //this.OnFrame();
+            }
+            catch (Exception e)
+            {
+                Console.WriteLine("exception");
+                Console.WriteLine(e.StackTrace);
+                Console.WriteLine(e.Message);
+            }
+        }
+
         private class CompileInfo
         {
             public string script;
-- 
cgit v1.1