Back to project page khandroid.
The source code is released under:
Apache License
If you think the Android project khandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2012-2014 Ognyan Bankov */*from w w w . ja v a 2s . c om*/ * 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.github.khandroid.rest; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; import org.slf4j.LoggerFactory; public class KhRestFunctionalityImpl implements KhRestFunctionality { private final RestFunctionality mRestFunc; private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(KhRestFunctionalityImpl.class .getSimpleName()); private AtomicLong mSequenceGenerator = new AtomicLong(0); private Listener mListener; private Map<Long, KhRestExchangeTtlHelper> mCompletedExchanges = new ConcurrentHashMap<Long, KhRestExchangeTtlHelper>(); private Thread mTimeouter; public KhRestFunctionalityImpl(RestFunctionality restFunc) { super(); mRestFunc = restFunc; } @Override public synchronized void setListener(Listener listener) { mListener = listener; } @Override public long executeKhRestExchange(final RestExchange<KhRestResult> x) { Long ret = mSequenceGenerator.incrementAndGet(); executeKhRestExchangeActual(x, ret); return ret; } private void executeKhRestExchangeActual(final RestExchange<KhRestResult> x, final Long idL) { Thread t = new Thread(new Runnable() { @Override public void run() { try { KhRestResult result = mRestFunc.execute(x); onExchangeResult(x, result, idL); } catch (RestExchangeFailedException e) { onExchangeError(x, idL); } } }); t.start(); } private void onExchangeResult(RestExchange<KhRestResult> x, KhRestResult result, Long idL) { RestExchangeOutcome<KhRestResult> outcome = new RestExchangeOutcome<KhRestResult>(x, result, false); onExchangeCompleted(outcome, idL); } private void onExchangeError(RestExchange<KhRestResult> x, Long idL) { RestExchangeOutcome<KhRestResult> outcome = new RestExchangeOutcome<KhRestResult>(x, null, true); onExchangeCompleted(outcome, idL); } private synchronized void onExchangeCompleted(RestExchangeOutcome<KhRestResult> outcome, Long idL) { KhRestExchangeTtlHelper cont = new KhRestExchangeTtlHelper(idL, getTime()); mCompletedExchanges.put(idL, cont); if (mListener != null) { mListener.onKhRestExchangeCompleted(outcome, idL); } } @Override public void consumeExchange(Long idL) { if (mCompletedExchanges.containsKey(idL)) { mCompletedExchanges.remove(idL); } } private long getTime() { return System.nanoTime(); } private class Timeouter implements Runnable { private static final long SLEEP = 10000; @Override public void run() { while (!Thread.currentThread().isInterrupted()) { checkAndRemoveTtled(); sleep(); } } private void checkAndRemoveTtled() { Iterator<KhRestExchangeTtlHelper> it = mCompletedExchanges.values() .iterator(); while (it.hasNext()) { KhRestExchangeTtlHelper x = it.next(); if (getTime() - x.getCompletedAt() > x.getTtl()) { Long idL = x.getId(); mCompletedExchanges.remove(idL); mLogger.debug("TTL of ServiceKhRestExchangeOutcomeContainer exceeded. Removing from mCompletedExchanges. Id: " + idL); } } } private void sleep() { try { Thread.sleep(SLEEP); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } @Override public void start() { mTimeouter = new Thread(new Timeouter()); mTimeouter.start(); } @Override public void shutdown() { if (mTimeouter != null) { mTimeouter.interrupt(); mTimeouter = null; } } private static class KhRestExchangeTtlHelper { /** * Default value for {@see #mTtl} */ private static final int DEFAUTL_TTL = 3000; final long mId; final long mCompletedAt; final long mTtl; public KhRestExchangeTtlHelper(long id, long completed, long ttl) { super(); mId = id; mCompletedAt = completed; mTtl = ttl * 1000000; // conver to nanos } public KhRestExchangeTtlHelper(long id, long completed) { this(id, completed, DEFAUTL_TTL); } public long getId() { return mId; } public long getCompletedAt() { return mCompletedAt; } public long getTtl() { return mTtl; } } }