Browse Source

More httprequest tweaks - added cancel for ios, removed non-default ctor.

Mark Sibly 7 năm trước cách đây
mục cha
commit
0a4ab9dc1b

+ 1 - 1
bananas/httprequest/httprequest_test.monkey2

@@ -57,7 +57,7 @@ Class MyWindow Extends Window
 		
 		req.Send()
 	End
-
+	
 	Method OnRender( canvas:Canvas ) Override
 	
 		App.RequestRender()	'need this for ios?

+ 7 - 16
modules/httprequest/httprequest.monkey2

@@ -38,22 +38,6 @@ Class HttpRequestBase
 	
 	Field ReadyStateChanged:Void()
 	
-	Method New()
-		
-		_readyState=ReadyState.Unsent
-		_timeout=10
-		_status=-1
-	End
-	
-	Method New( req:String,url:String,readyStateChanged:Void()=Null )
-		
-		Self.New()
-		
-		Open( req,url )
-		
-		ReadyStateChanged=readyStateChanged
-	End
-	
 	Property Timeout:Float()
 		
 		Return _timeout
@@ -107,6 +91,13 @@ Class HttpRequestBase
 	Field _req:String
 	Field _url:String
 	
+	Method New()
+		
+		_readyState=ReadyState.Unsent
+		_timeout=10
+		_status=-1
+	End
+	
 	Method OnOpen( req:String,url:String ) Virtual
 		_req=req
 		_url=url

+ 16 - 239
modules/httprequest/httprequest_android.monkey2

@@ -21,13 +21,6 @@ Global onReadyStateChanged:Void(jobject,Int)="bbHttpRequest::onReadyStateChanged
 
 Global onResponseReceived:Void(jobject,String,Int,Int)="bbHttpRequest::onResponseReceived"
 
-private
-
-Function OnMainFiber( func:Void() )
-	
-	If Fiber.Current()=Fiber.Main() func() Else App.Idle+=func
-End
-
 Public
 
 Class HttpRequest Extends HttpRequestBase
@@ -49,43 +42,31 @@ Class HttpRequest Extends HttpRequestBase
 	
 	Method OnOpen( req:String,url:String ) Override
 		
-		OnMainFiber( Lambda()
-		
-			If Not _obj Return
-		
-			Local env:=Android_JNI_GetEnv()
-		
-			env.CallVoidMethod( _obj,_open,New Variant[]( req,url ) )
-			
-		End )
+		If Not _obj Return
+	
+		Local env:=Android_JNI_GetEnv()
+	
+		env.CallVoidMethod( _obj,_open,New Variant[]( req,url ) )
 	End
 	
 	Method OnSetHeader( header:String,value:String ) Override
 		
-		OnMainFiber( Lambda()
-
-			If Not _obj Return
-		
-			Local env:=Android_JNI_GetEnv()
-		
-			env.CallVoidMethod( _obj,_setHeader,New Variant[]( header,value ) )
-			
-		End )
+		If Not _obj Return
+	
+		Local env:=Android_JNI_GetEnv()
+	
+		env.CallVoidMethod( _obj,_setHeader,New Variant[]( header,value ) )
 	End
 	
 	Method OnSend( text:String ) Override
 		
-		OnMainFiber( Lambda()
-
-			If Not _obj Return
-		
-			Local env:=Android_JNI_GetEnv()
-			
-			Local timeout:=Int( _timeout * 1000 )
-		
-			env.CallVoidMethod( _obj,_send,New Variant[]( text,timeout ) )
+		If Not _obj Return
+	
+		Local env:=Android_JNI_GetEnv()
 		
-		End )
+		Local timeout:=Int( _timeout * 1000 )
+	
+		env.CallVoidMethod( _obj,_send,New Variant[]( text,timeout ) )
 	End
 	
 	Method OnCancel() Override
@@ -179,207 +160,3 @@ Class HttpRequest Extends HttpRequestBase
 		onResponseReceived=OnResponseReceived
 	End
 End
-
-#rem
-
-Class HttpRequest
-	
-	Field ReadyStateChanged:Void()
-	
-	Method New()
-
-		Local env:=Android_JNI_GetEnv()
-		
-		Init( env )
-		
-		Local obj:=env.AllocObject( _class )
-		
-		_obj=env.NewGlobalRef( obj )
-		
-		Insert()
-	End
-	
-	Method New( req:String,url:String,readyStateChanged:Void()=Null )
-		Self.New()
-		
-		ReadyStateChanged=readyStateChanged
-		
-		Open( req,url )
-	End
-	
-	Property ReadyState:ReadyState()
-		
-		Return Cast<ReadyState>( _readyState )
-
-	End
-	
-	Property ResponseText:String()
-		
-		Return _response
-	End
-	
-	Property Status:Int()
-		
-		Return _status
-	End
-	
-	Property Timeout:Float()
-		
-		Return _timeout
-		
-	Setter( timeout:Float )
-		
-		_timeout=timeout
-	End
-	
-	Method Discard()
-		
-		Remove()
-		
-		Local env:=Android_JNI_GetEnv()
-		
-		env.DeleteGlobalRef( _obj )
-		
-	End
-	
-	Method Open( req:String,url:String )
-		
-		OnMainFiber( Lambda()
-		
-			Local env:=Android_JNI_GetEnv()
-		
-			env.CallVoidMethod( _obj,_open,New Variant[]( req,url ) )
-		End )
-	End
-	
-	Method SetHeader( header:String,value:String )
-		
-		OnMainFiber( Lambda()
-
-			Local env:=Android_JNI_GetEnv()
-		
-			env.CallVoidMethod( _obj,_setHeader,New Variant[]( header,value ) )
-		End )
-	End
-	
-	Method Send()
-		
-		Send( "" )
-	End
-	
-	Method Send( text:String )
-		
-		OnMainFiber( Lambda()
-
-			Local env:=Android_JNI_GetEnv()
-			
-			Local timeout:=Int( _timeout * 1000 )
-		
-			env.CallVoidMethod( _obj,_send,New Variant[]( text,timeout ) )
-		
-		End )
-		
-	End
-	
-	Private
-	
-	Global _sending:=New Stack<HttpRequest>
-	
-	
-	Global _list:HttpRequest
-	
-	Field _succ:HttpRequest
-	Field _obj:jobject
-	Field _readyState:Int
-	Field _response:String
-	Field _status:int
-	Field _timeout:Float=10.0
-	
-	Method New( peer:jobject )
-		_obj=peer
-	End
-	
-	Method Insert()
-		_succ=_list
-		_list=Self
-	End
-	
-	Method Remove()
-		local inst:=_list,pred:HttpRequest=Null
-		While inst
-			If inst=Self
-				If pred
-					pred._succ=_succ
-				Else
-					_list=_succ
-				Endif
-				Return
-			Endif
-			pred=inst
-			inst=inst._succ
-		Wend
-	End
-	
-	Global _class:jclass
-	Global _open:jmethodID
-	Global _setHeader:jmethodID
-	Global _send:jmethodID
-	
-	Function OnReadyStateChanged( obj:jobject,state:Int )
-		
-		Local env:=Android_JNI_GetEnv()
-		
-		For Local request:=Eachin _requests
-			
-			If Not env.IsSameObject( obj,request._obj ) Continue
-				
-			request.SetReadyState( state )
-				
-			Return
-		Next
-		
-	End
-	
-	Function OnResponseReceived( obj:jobject,response:String,status:Int,state:Int )
-
-		Local env:=Android_JNI_GetEnv()
-		
-		For Local request:=Eachin _requests
-			
-			If Not env.IsSameObject( obj,request._obj ) Continue
-				
-			inst._response=response
-			
-			inst._status=status
-			
-			inst._readyState=state
-				
-			inst.ReadyStateChanged()
-			
-			Return
-		Next
-	End
-	
-	Function Init()
-		
-		If _class Return
-
-		Local env:=Android_JNI_GetEnv()
-	
-		_class=env.FindClass( "com/monkey2/lib/Monkey2HttpRequest" )
-		If Not _class RuntimeError( "Can't find com.monkey2.lib.Monkey2HttpRequest class" )
-		
-		_open=env.GetMethodID( _class,"open","(Ljava/lang/String;Ljava/lang/String;)V" )
-		
-		_setHeader=env.GetMethodID( _class,"setHeader","(Ljava/lang/String;Ljava/lang/String;)V" )
-		
-		_send=env.GetMethodID( _class,"send","(Ljava/lang/String;I)V" )
-		
-		onReadyStateChanged=OnReadyStateChanged
-		
-		onResponseReceived=OnResponseReceived
-	End
-	
-End
-
-		

+ 3 - 0
modules/httprequest/httprequest_desktop.monkey2

@@ -6,6 +6,9 @@ End
 
 Class HttpRequest Extends HttpRequestBase
 	
+	Method New()
+	End
+	
 	Protected
 	
 	Method OnSend( text:String ) Override

+ 3 - 0
modules/httprequest/httprequest_emscripten.monkey2

@@ -7,6 +7,9 @@ Using emscripten..
 
 Class HttpRequest Extends HttpRequestBase
 	
+	Method New()
+	End
+	
 	Protected
 	
 	Method OnSend( text:String ) Override

+ 0 - 9
modules/httprequest/httprequest_ios.monkey2

@@ -31,15 +31,6 @@ Class HttpRequest Extends HttpRequestBase
 		_peer.readyStateChanged=OnReadyStateChanged
 	End
 	
-	Method New( req:String,url:String,readyStateChanged:Void()=Null )
-	
-		Self.New()
-		
-		Open( req,url )
-		
-		ReadyStateChanged=readyStateChanged
-	End
-	
 	Protected
 	
 	Method OnReadyStateChanged()

+ 12 - 77
modules/httprequest/native/Monkey2HttpRequest.java

@@ -21,7 +21,7 @@ public class Monkey2HttpRequest{
 	
 	int readyState;
 	
-	boolean busy;
+	boolean sending;
 
     native void onNativeReadyStateChanged( int state );
     
@@ -50,22 +50,24 @@ public class Monkey2HttpRequest{
 			
 		}catch( IOException ex ){
 		
+			Log.v( TAG,ex.toString() );
+				
 			setReadyState( 5 );
 		}		
 	}
 	
 	void setHeader( String name,String value ){
 	
-		if( readyState!=1 || busy ) return;
+		if( readyState!=1 || sending ) return;
 	
 		connection.setRequestProperty( name,value );
 	}
 	
 	void send( final String text,final int timeout ){
 	
-		if( readyState!=1 || busy ) return;
-		
-		ExecutorService executor=Executors.newSingleThreadExecutor();
+		if( readyState!=1 || sending ) return;
+				
+		sending=true;
 		
 		new Thread( new Runnable(){
 		
@@ -75,7 +77,7 @@ public class Monkey2HttpRequest{
 		
 				connection.setReadTimeout( timeout );
 				
-				try {
+				try{
 				
 					if( text!=null && text.length()!=0 ){
 			
@@ -110,89 +112,22 @@ public class Monkey2HttpRequest{
 					
 					readyState=4;
 					
-				} catch ( IOException ex) {
+				}catch( IOException ex ){
+				
+					Log.v( TAG,ex.toString() );
 				
 					setReadyState( 5 );
 				}
 
 				connection.disconnect();
 
-				busy=false;
+				sending=false;
 			}
-			
 		} ).start();
-		
 	}
 	
 	void cancel(){
 	
-		Log.v( TAG,"Cancelling httprequest" );
-	
 		connection.disconnect();
 	}
-	
-	/*
-	void send( final String text,final int timeout ){
-	
-		if( readyState!=1 || busy ) return;
-		
-		busy=true;
-		
-		new Thread( new Runnable() {
-
-			public void run() {
-			
-				connection.setConnectTimeout( timeout );
-		
-				connection.setReadTimeout( timeout );
-				
-				try {
-				
-					if( text!=null && text.length()!=0 ){
-			
-						byte[] bytes=text.getBytes( "UTF-8" );
-
-						connection.setDoOutput( true );
-						connection.setFixedLengthStreamingMode( bytes.length );
-				
-						OutputStream out=connection.getOutputStream();
-						out.write( bytes,0,bytes.length );
-						out.close();
-					}
-					
-					InputStream in=connection.getInputStream();
-					
-					setReadyState( 3 );
-
-					byte[] buf=new byte[4096];
-					ByteArrayOutputStream out=new ByteArrayOutputStream(1024);
-					for( ;; ){
-						int n=in.read( buf );
-						if( n<0 ) break;
-						out.write( buf,0,n );
-					}
-					in.close();
-
-					String response=new String( out.toByteArray(),"UTF-8" );
-
-					int status=connection.getResponseCode();
-					
-					onNativeResponseReceived( response,status,4 );
-					
-					readyState=4;
-					
-				} catch ( IOException ex) {
-				
-					setReadyState( 5 );
-				}
-
-				connection.disconnect();
-					
-				
-				busy=false;
-			}
-			
-		} ).start();
-	}
-	*/
 }