[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [orbot/master] improves in start/stop logic for background service



commit cf218090520fd8ebb362c78814592b75b9857015
Author: Nathan Freitas <nathan@xxxxxxxxxxx>
Date:   Mon Nov 17 23:28:33 2014 -0500

    improves in start/stop logic for background service
---
 src/org/torproject/android/Orbot.java              |   31 +++--
 src/org/torproject/android/service/TorService.java |  120 ++++++++++----------
 .../android/service/TorServiceConstants.java       |   11 ++
 .../android/service/TorServiceUtils.java           |    2 +-
 4 files changed, 92 insertions(+), 72 deletions(-)

diff --git a/src/org/torproject/android/Orbot.java b/src/org/torproject/android/Orbot.java
index d8e1f6b..d1c111a 100644
--- a/src/org/torproject/android/Orbot.java
+++ b/src/org/torproject/android/Orbot.java
@@ -106,7 +106,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
 		LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
 			      new IntentFilter("log"));
 
-		startService("init");
+		startService(TorServiceConstants.CMD_INIT);
 	}
 	
 	// Our handler for received Intents. This will be called whenever an Intent
@@ -154,7 +154,14 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
 		torService.setAction(action);
 		startService(torService);
 		
-        
+	}
+	
+	private void stopService ()
+	{
+		
+		Intent torService = new Intent(this, TorService.class);
+		stopService(torService);
+		
 	}
 	
 	private void doLayout ()
@@ -412,7 +419,9 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
                                 {
                                     if (mItemOnOff != null)
                                             mItemOnOff.setTitle(R.string.menu_start);
+                                    
                                         stopTor();
+                                        stopService ();
                                         
                                 }
                                 
@@ -472,7 +481,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
                         //not something to tackle in your first iteration, but i thin we can talk about fixing
                         //terminology but also making sure there are clear distinctions in control
                         stopTor();
-                        
+                        stopService ();
                         //onDestroy();
                         
                         
@@ -855,7 +864,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
 		}
 		else if (request == REQUEST_VPN && response == RESULT_OK)
 		{
-			startService("vpn");
+			startService(TorServiceConstants.CMD_VPN);
 		}
 		
 	}
@@ -871,21 +880,21 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
 	        } 
 	        else
 	        {
-				startService("vpn");
+				startService(TorServiceConstants.CMD_VPN);
 
 	        }
     }
 
     private boolean flushTransProxy ()
     {
-    	startService("flush");
+    	startService(TorServiceConstants.CMD_FLUSH);
     	return true;
     }
     
     private boolean updateSettings ()
     {
     	//todo send service command
-    	startService("update");
+    	startService(TorServiceConstants.CMD_UPDATE);
     	return true;
     }
 
@@ -1022,7 +1031,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
     {
             
 
-		startService ("start");
+		startService (TorServiceConstants.CMD_START);
 		torStatus = TorServiceConstants.STATUS_CONNECTING;
 				
 		mTxtOrbotLog.setText("");
@@ -1050,7 +1059,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
     private void stopTor () throws RemoteException
     {
     	
-    	startService ("stop");
+    	startService (TorServiceConstants.CMD_STOP);
 		torStatus = TorServiceConstants.STATUS_OFF;
 
     	//	mService.setProfile(TorServiceConstants.STATUS_OFF);
@@ -1058,6 +1067,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
     		mHandler.sendMessage(msg);
     		
     
+    		
     }
     
         /*
@@ -1080,6 +1090,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
 	                {
 	                        
 	                        stopTor();
+	                        stopService ();
 	                        
 	                }
 	                
@@ -1303,7 +1314,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
 
 	public void spinOrbot (float direction)
 	{
-			startService ("newnym");
+			startService (TorServiceConstants.CMD_NEWNYM);
 		
 			//mService.newIdentity(); //request a new identity
 			//TODO trigger newnym
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index aeca556..adb6fcf 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -377,31 +377,31 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 					
 					if (action!=null) 
 					{
-						if(action.equals(Intent.ACTION_BOOT_COMPLETED)||action.equals("start"))
+						if(action.equals(Intent.ACTION_BOOT_COMPLETED)||action.equals(CMD_START))
 			 			{	     				
 			 				setTorProfile(STATUS_ON);	     			
 			 			}
-						else if (action.equals("stop"))
+						else if (action.equals(CMD_STOP))
 						{
 							setTorProfile(STATUS_OFF);
 						}
-						else if (action.equals("init"))
+						else if (action.equals(CMD_INIT))
 						{
 							sendCallbackStatus(mCurrentStatus);
 						}
-						else if (action.equals("newnym"))
+						else if (action.equals(CMD_NEWNYM))
 						{
 							newIdentity();
 						}
-						else if (action.equals("flush"))
+						else if (action.equals(CMD_FLUSH))
 						{
 							flushTransparentProxyRules();
 						}
-						else if (action.equals("update"))
+						else if (action.equals(CMD_UPDATE))
 						{
 							processSettings();
 						}
-						else if (action.equals("vpn"))
+						else if (action.equals(CMD_VPN))
 						{
 							startVpnService();
 						}
@@ -774,7 +774,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     	
     }
     
-    public void startTor () throws Exception
+    private void startTor () throws Exception
     {
     	
 		mCurrentStatus = STATUS_CONNECTING;
@@ -1074,6 +1074,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			
 			int attempt = 0;
 			
+
+			logNotice( "Waiting for control port...");
+			
 			while (conn == null && attempt++ < maxTries)
 			{
 				try
@@ -1098,13 +1101,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 				catch (Exception ce)
 				{
 					conn = null;
-					logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce);
+				//	logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce);
 					
 				}
 				
 				
 				try {
-					logNotice("waiting...");
+				//	logNotice("waiting...");
 					Thread.sleep(1000); }
 				catch (Exception e){}
 			
@@ -1264,7 +1267,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		{
 			if (fileControlPort.exists())
 			{
-				logNotice("Reading control port config file: " + fileControlPort.getCanonicalPath());
+				debug("Reading control port config file: " + fileControlPort.getCanonicalPath());
 				BufferedReader bufferedReader = new BufferedReader(new FileReader(fileControlPort));
 				String line = bufferedReader.readLine();
 				
@@ -1284,7 +1287,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			}
 			else
 			{
-				logNotice("Control Port config file does not yet exist (waiting for tor): " + fileControlPort.getCanonicalPath());
+				debug("Control Port config file does not yet exist (waiting for tor): " + fileControlPort.getCanonicalPath());
 				
 			}
 			
@@ -1292,11 +1295,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		}
 		catch (FileNotFoundException e)
 		{	
-			logNotice("unable to get control port; file not found");
+			debug("unable to get control port; file not found");
 		}
 		catch (Exception e)
 		{	
-			logNotice("unable to read control port config file");
+			debug("unable to read control port config file");
 		}
 
 		return result;
@@ -1376,37 +1379,53 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		}
 
 
-		public void setTorProfile(int profile)  {
+		public void setTorProfile(int newState)  {
 		
-			if (profile == STATUS_ON && mCurrentStatus != STATUS_ON)
+			if (newState == STATUS_ON)
         	{
         		
-				sendCallbackLogMessage (getString(R.string.status_starting_up));
-
-	            try
-	   		     {
-	   			   startTor();
-
-	   		     }
-	   		     catch (Exception e)
-	   		     {				
-	   		    	
-	   		    	logException("Unable to start Tor: " + e.toString(),e);	
-	   		    	 mCurrentStatus = STATUS_OFF;
-	   		    	sendCallbackStatus(mCurrentStatus);
-	   	    		
-	   		    	 showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
-	   		    	stopTor();
-	   		     }
+				if (mCurrentStatus == STATUS_OFF)
+				{
+					sendCallbackLogMessage (getString(R.string.status_starting_up));
+	
+				
+		            try
+		   		     {
+
+		            	boolean found = findExistingProc ();
+		            	
+		            	if (!found)
+		            	{
+			            	killProcess(fileTor);
+			            	killProcess(filePolipo);
+			            	
+			   			    startTor();
+		            	}
+		   		     }
+		   		     catch (Exception e)
+		   		     {				
+		   		    	
+		   		    	logException("Unable to start Tor: " + e.toString(),e);	
+		   		    	 mCurrentStatus = STATUS_OFF;
+		   		    	sendCallbackStatus(mCurrentStatus);
+		   	    		
+		   		    	 showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
+		   		    	stopTor();
+		   		     }
+			            
+				}
         	}
-        	else if (profile == STATUS_OFF && mCurrentStatus != STATUS_OFF)
+        	else if (newState == STATUS_OFF)
         	{
-        		sendCallbackLogMessage (getString(R.string.status_shutting_down));
-	          
-	            stopTor();
-
-        		mCurrentStatus = STATUS_OFF;  
-        		sendCallbackStatus(mCurrentStatus);
+        		if (mCurrentStatus == STATUS_ON)
+        		{
+	        		sendCallbackLogMessage (getString(R.string.status_shutting_down));
+		          
+		            stopTor();
+	
+	        		mCurrentStatus = STATUS_OFF;  
+	        		sendCallbackStatus(mCurrentStatus);
+        		}
         		
         	}
 		}
@@ -1733,27 +1752,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 	}
 	
 
-
-	public boolean checkAndInitImpl ()
-    {
-    	if (fileTor != null)
-    	{
-    		try {
-				if (TorServiceUtils.findProcessId(fileTor.getCanonicalPath()) != -1)
-				{
-					initialize();
-					return true;
-				}
-			} catch (IOException e) {
-				logException("error init Tor", e);
-			}
-    				
-    	}
-    	
-    	return false;
-    }
-    
-            
         
         public void processSettings ()
         {
diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java
index 0b64856..d979ac4 100644
--- a/src/org/torproject/android/service/TorServiceConstants.java
+++ b/src/org/torproject/android/service/TorServiceConstants.java
@@ -76,6 +76,17 @@ public interface TorServiceConstants {
     public static final int DISABLE_TOR_MSG = 3;
     public static final int LOG_MSG = 4;
     
+    public static final String CMD_START = "start";
+    public static final String CMD_STOP = "stop";
+    public static final String CMD_FLUSH = "flush";
+    public static final String CMD_NEWNYM = "newnym";
+    public static final String CMD_INIT = "init";
+    public static final String CMD_VPN = "vpn";
+    public static final String CMD_UPDATE = "update";
+     
+    
+    
+    
     public static final String BINARY_TOR_VERSION = "0.2.5.10-openssl1.0.1i-nonPIE-polipofix";
     public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED";
     
diff --git a/src/org/torproject/android/service/TorServiceUtils.java b/src/org/torproject/android/service/TorServiceUtils.java
index 0038a5c..a22b328 100644
--- a/src/org/torproject/android/service/TorServiceUtils.java
+++ b/src/org/torproject/android/service/TorServiceUtils.java
@@ -108,7 +108,7 @@ public class TorServiceUtils implements TorServiceConstants {
         	}
         }
         
-        procPs.destroy();
+        try { procPs.destroy(); } catch (Exception e) {} // try to destroy just to make sure we clean it up
        
         return procId;
 

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits