Author: remi
Date: 2009-03-31 12:13:17 +0200 (Tue, 31 Mar 2009)
New Revision: 4277

Added:
   
software_suite_v2/software/development_tools/tuxdroid-gadget-java-kit/branches/0.0.2/tuxdroid-gadget-java-kit/sources/com/kysoh/tuxdroid/gadget/framework/gadget/StdInCom.java
Modified:
   
software_suite_v2/software/development_tools/tuxdroid-gadget-java-kit/branches/0.0.2/tuxdroid-gadget-java-kit/sources/com/kysoh/tuxdroid/gadget/framework/gadget/SimpleGadget.java
Log:
* SimpleGadget class from java gadget kit can now handling the "stop" request 
from the framework.
  - Override the "onGadgetStop" method to interact with the "stop" request (to 
properly quit the gadget context ... )

Modified: 
software_suite_v2/software/development_tools/tuxdroid-gadget-java-kit/branches/0.0.2/tuxdroid-gadget-java-kit/sources/com/kysoh/tuxdroid/gadget/framework/gadget/SimpleGadget.java
===================================================================
--- 
software_suite_v2/software/development_tools/tuxdroid-gadget-java-kit/branches/0.0.2/tuxdroid-gadget-java-kit/sources/com/kysoh/tuxdroid/gadget/framework/gadget/SimpleGadget.java
  2009-03-31 10:13:12 UTC (rev 4276)
+++ 
software_suite_v2/software/development_tools/tuxdroid-gadget-java-kit/branches/0.0.2/tuxdroid-gadget-java-kit/sources/com/kysoh/tuxdroid/gadget/framework/gadget/SimpleGadget.java
  2009-03-31 10:13:17 UTC (rev 4277)
@@ -25,6 +25,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.PrintWriter;
@@ -44,6 +45,7 @@
        public static final String ENVIRONEMENT_PREFIX = "tgp_";
        /** configuration. */
        private CONFIGURATION configuration;
+       private StdInCom stdInCom = null;
 
        /**
         * @return the configuration
@@ -165,7 +167,16 @@
                        }
                        this.configuration = configuration;
                        loadEnvironementData();
+                       if (this.configuration.isDaemon())
+                       {
+                               stdInCom = new StdInCom();
+                               stdInCom.start();
+                       }
                        start();
+                       if (!this.configuration.isDaemon())
+                       {
+                               onGadgetStop();
+                       }
                } catch (Throwable e) {
                        throwError(e);
                }
@@ -198,6 +209,32 @@
         *             when something go wrong...
         */
        protected abstract void start() throws Throwable;
+       
+       /**
+        * On gadget stop event.
+        * this method should be defined in your gadget class.
+        * @throws Throwable
+        *                              when something go wrong...
+        */
+       protected abstract void onGadgetStop() throws Throwable;
+       
+       /**
+        * Stop the gadget.
+        */
+       public void stop()
+       {
+               if (this.stdInCom != null)
+               {
+                       this.stdInCom.stopPipe();
+               }
+               
+               try {
+                       this.onGadgetStop();
+               } catch (Throwable e) {
+                       this.throwError(e);
+               }
+               this.throwNotification("gadget", "stop");
+       }
 
        /**
         * This function throw a debug trace to the server. The server should 
have

Added: 
software_suite_v2/software/development_tools/tuxdroid-gadget-java-kit/branches/0.0.2/tuxdroid-gadget-java-kit/sources/com/kysoh/tuxdroid/gadget/framework/gadget/StdInCom.java
===================================================================
--- 
software_suite_v2/software/development_tools/tuxdroid-gadget-java-kit/branches/0.0.2/tuxdroid-gadget-java-kit/sources/com/kysoh/tuxdroid/gadget/framework/gadget/StdInCom.java
                              (rev 0)
+++ 
software_suite_v2/software/development_tools/tuxdroid-gadget-java-kit/branches/0.0.2/tuxdroid-gadget-java-kit/sources/com/kysoh/tuxdroid/gadget/framework/gadget/StdInCom.java
      2009-03-31 10:13:17 UTC (rev 4277)
@@ -0,0 +1,119 @@
+/* This file is part of "TuxDroid Gadget Java Kit" library.
+ *    Copyright 2009, kysoh
+ *    Author : Remi Jocaille
+ *    eMail  : [email protected]
+ *    Site   : http://www.kysoh.com/
+ *
+ * "TuxDroid Gadget Java Kit" is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * "TuxDroid Gadget Java Kit" is distributed in the hope that it will be 
useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with "TuxDroid Gadget Java Kit"; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package com.kysoh.tuxdroid.gadget.framework.gadget;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+public class StdInCom extends Thread
+{
+       private boolean isRun = false;
+       private Object runMutex = new Object();
+       
+       /*
+        * Set the run state of the communication handling.
+        */
+       private void setRun(boolean value)
+       {
+               synchronized (runMutex)
+               {
+                       isRun = value;
+               }
+       }
+       
+       /*
+        * Get the run state of the communication handling.
+        */
+       private boolean getRun()
+       {
+               boolean result;
+               synchronized (runMutex)
+               {
+                       result = isRun;
+               }
+               return result;
+       }
+       
+       /**
+        * Stop the communication handling.
+        */
+       @SuppressWarnings("deprecation")
+       public void stopPipe()
+       {
+               if (!getRun())
+               {
+                       return;
+               }
+               setRun(false);
+               try
+               {
+                       join(500);
+               }
+               catch (InterruptedException e) {}
+               if (isAlive())
+               {
+                       stop();
+               }
+       }
+       
+       /**
+        * Loop to listening the commands from the host application.
+        */
+       @SuppressWarnings("static-access")
+       public void run()
+       {
+               if (getRun())
+               {
+                       return;
+               }
+               setRun(true);
+               BufferedReader stdin = new BufferedReader(new 
InputStreamReader(System.in));
+               while (getRun())
+               {
+                       // Read the next received command
+                       String rcvLine;
+                       try
+                       {
+                               while (!stdin.ready())
+                               {
+                                       try {
+                                               this.sleep(200);
+                                       } catch (InterruptedException e) {}
+                               }
+                               rcvLine = stdin.readLine();
+                       }
+                       catch (IOException e)
+                       {
+                               break;
+                       }
+                       if (rcvLine.toLowerCase().startsWith("stop"))
+                       {
+                               this.setRun(false);
+                       }
+                       try {
+                               this.sleep(100);
+                       } catch (InterruptedException e) {}
+               }
+       }
+}


------------------------------------------------------------------------------
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to