Skip to content

Commit

Permalink
添加同步方法.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjc133 committed May 20, 2016
1 parent a34d38c commit 5fe9fbd
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down Expand Up @@ -112,7 +113,6 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>
6 changes: 1 addition & 5 deletions soar-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

<artifactId>soar-core</artifactId>
<build>
Expand All @@ -29,11 +30,6 @@
<artifactId>httpclient</artifactId>
</dependency>

<dependency>
<groupId>com.mcxiaoke.volley</groupId>
<artifactId>library</artifactId>
<version>1.0.19</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.elite.tools.soar.toolbox;

import com.elite.tools.soar.Cache;
import com.elite.tools.soar.NetworkResponse;
import com.elite.tools.soar.Request;
import com.elite.tools.soar.Response;

/**
* A synthetic request used for clearing the cache.
*/
public class ClearCacheRequest extends Request<Object> {
private final Cache mCache;
private final Runnable mCallback;

/**
* Creates a synthetic request for clearing the cache.
*
* @param cache Cache to clear
* @param callback Callback to make on the main thread once the cache is clear,
* or null for none
*/
public ClearCacheRequest(Cache cache, Runnable callback) {
super(Method.GET, null, null);
mCache = cache;
mCallback = callback;
}

@Override
public boolean isCanceled() {
// This is a little bit of a hack, but hey, why not.
mCache.clear();
if (mCallback != null) {
mCallback.run();
}
return true;
}

@Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}

@Override
protected Response<Object> parseNetworkResponse(NetworkResponse response) {
return null;
}

@Override
protected void deliverResponse(Object response) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.elite.tools.soar.NetworkResponse;
import com.elite.tools.soar.ParseError;
import com.elite.tools.soar.Response;
import com.oracle.javafx.jmx.json.JSONException;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -45,8 +44,6 @@ protected Response<T> parseNetworkResponse(NetworkResponse response) {
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.elite.tools.soar.toolbox;

import com.elite.tools.soar.Request;
import com.elite.tools.soar.Response;
import com.elite.tools.soar.SoarError;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* A Future that represents a Soar request.
* <p>
* Used by providing as your response and error listeners. For example:
* <pre>
* RequestFuture&lt;JSONObject&gt; future = RequestFuture.newFuture();
* MyRequest request = new MyRequest(URL, future, future);
*
* // If you want to be able to cancel the request:
* future.setRequest(requestQueue.add(request));
*
* // Otherwise:
* requestQueue.add(request);
*
* try {
* JSONObject response = future.get();
* // do something with response
* } catch (InterruptedException e) {
* // handle the error
* } catch (ExecutionException e) {
* // handle the error
* }
* </pre>
*
* @param <T> The type of parsed response this future expects.
*/
public class RequestFuture<T> implements Future<T>, Response.Listener<T>,
Response.ErrorListener {
private Request<?> mRequest;
private boolean mResultReceived = false;
private T mResult;
private SoarError mException;

private RequestFuture() {
}

public static <E> RequestFuture<E> newFuture() {
return new RequestFuture<E>();
}

public void setRequest(Request<?> request) {
mRequest = request;
}

@Override
public synchronized boolean cancel(boolean mayInterruptIfRunning) {
if (mRequest == null) {
return false;
}

if (!isDone()) {
mRequest.cancel();
return true;
} else {
return false;
}
}

@Override
public T get() throws InterruptedException, ExecutionException {
try {
return doGet(null);
} catch (TimeoutException e) {
throw new AssertionError(e);
}
}

@Override
public T get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return doGet(TimeUnit.MILLISECONDS.convert(timeout, unit));
}

private synchronized T doGet(Long timeoutMs)
throws InterruptedException, ExecutionException, TimeoutException {
if (mException != null) {
throw new ExecutionException(mException);
}

if (mResultReceived) {
return mResult;
}

if (timeoutMs == null) {
wait(0);
} else if (timeoutMs > 0) {
wait(timeoutMs);
}

if (mException != null) {
throw new ExecutionException(mException);
}

if (!mResultReceived) {
throw new TimeoutException();
}

return mResult;
}

@Override
public boolean isCancelled() {
if (mRequest == null) {
return false;
}
return mRequest.isCanceled();
}

@Override
public synchronized boolean isDone() {
return mResultReceived || mException != null || isCancelled();
}

@Override
public synchronized void onResponse(T response) {
mResultReceived = true;
mResult = response;
notifyAll();
}

@Override
public synchronized void onErrorResponse(SoarError error) {
mException = error;
notifyAll();
}
}

26 changes: 22 additions & 4 deletions soar-core/src/test/java/RequestTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

import com.elite.tools.soar.Request;
import com.elite.tools.soar.RequestQueue;
import com.elite.tools.soar.Response;
import com.elite.tools.soar.SoarError;
import com.elite.tools.soar.*;
import com.elite.tools.soar.toolbox.GsonRequest;
import com.elite.tools.soar.toolbox.RequestFuture;
import com.elite.tools.soar.toolbox.Soar;
import com.elite.tools.soar.toolbox.StringRequest;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
* Created by wjc133
Expand All @@ -17,6 +18,7 @@
public class RequestTest {
private RequestQueue mQueue = Soar.newRequestQueue();

@Ignore
@Test
public void testRequest() {
String url = "http://www.baidu.com";
Expand All @@ -38,4 +40,20 @@ public void onErrorResponse(SoarError error) {
e.printStackTrace();
}
}

@Test
public void testGsonRequest() {
String url = "http://localhost:8081/soar/news";
RequestFuture<News> future = RequestFuture.newFuture();
GsonRequest<News> request = new GsonRequest<News>(Request.Method.GET, url, News.class, future, future);
mQueue.add(request);
try {
News news = future.get();
System.out.println(news);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 5fe9fbd

Please sign in to comment.