Eric Kok
10 years ago
58 changed files with 3287 additions and 3484 deletions
@ -1,181 +0,0 @@
@@ -1,181 +0,0 @@
|
||||
/* |
||||
* This file is part of Transdroid <http://www.transdroid.org>
|
||||
* |
||||
* Transdroid 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 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
* |
||||
*/ |
||||
package org.transdroid.daemon; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Iterator; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Queue; |
||||
|
||||
import org.transdroid.daemon.task.DaemonTask; |
||||
import org.transdroid.daemon.task.DaemonTaskFailureResult; |
||||
import org.transdroid.daemon.task.DaemonTaskResult; |
||||
import org.transdroid.daemon.task.DaemonTaskSuccessResult; |
||||
import org.transdroid.daemon.util.DLog; |
||||
|
||||
public class TaskQueue implements Runnable { |
||||
|
||||
private static final String LOG_NAME = "Queue"; |
||||
|
||||
private List<DaemonTask> queue; |
||||
private Thread worker; |
||||
private IDaemonCallback callback; |
||||
private boolean paused;; |
||||
|
||||
public TaskQueue(IDaemonCallback callback) { |
||||
queue = Collections.synchronizedList(new LinkedList<DaemonTask>()); |
||||
paused = true; |
||||
this.callback = callback; |
||||
worker = new Thread(this); |
||||
worker.start(); |
||||
} |
||||
|
||||
/** |
||||
* Enqueue a single new task to later perform. |
||||
* @param task The task to add to the queue |
||||
*/ |
||||
public synchronized void enqueue(DaemonTask task) { |
||||
queue.add(task); |
||||
notifyAll(); |
||||
} |
||||
|
||||
/** |
||||
* Queues an old set of tasks again in the queue. This for example can be |
||||
* used to restore on old queue after an Activity was destroyed and |
||||
* is restored again. |
||||
* @param tasks A list of daemon tasks to queue |
||||
*/ |
||||
public synchronized void requeue(List<DaemonTask> tasks) { |
||||
queue.addAll(tasks); |
||||
notifyAll(); |
||||
} |
||||
|
||||
/** |
||||
* Removes all remaining tasks from the queue. Existing operations are still |
||||
* continued and their results posted back. |
||||
*/ |
||||
public synchronized void clear() { |
||||
queue.clear(); |
||||
notifyAll(); |
||||
} |
||||
|
||||
/** |
||||
* Removes all remaining tasks from the queue that are of some specific type. |
||||
* Other remaining tasks will still be executed and running operations are |
||||
* still continued and their results posted back. |
||||
* @param class1 |
||||
*/ |
||||
public synchronized void clear(DaemonMethod ofType) { |
||||
Iterator<DaemonTask> task = queue.iterator(); |
||||
while (task.hasNext()) { |
||||
if (task.next().getMethod() == ofType) { |
||||
task.remove(); |
||||
} |
||||
} |
||||
notifyAll(); |
||||
} |
||||
|
||||
/** |
||||
* Returns a copy of the queue with all remaining tasks. This can be used |
||||
* to save them on an Activity destroy and restore them later using |
||||
* requeue(). |
||||
* @return A list containing all remaining tasks |
||||
*/ |
||||
public Queue<DaemonTask> getRemainingTasks() { |
||||
return new LinkedList<DaemonTask>(queue); |
||||
} |
||||
|
||||
/** |
||||
* Request the task perfoming thread to stop all activity |
||||
*/ |
||||
public synchronized void requestStop() { |
||||
paused = true; |
||||
} |
||||
|
||||
/** |
||||
* Request |
||||
*/ |
||||
public synchronized void start() { |
||||
paused = false; |
||||
notify(); |
||||
} |
||||
|
||||
@Override |
||||
public void run() { |
||||
|
||||
while (true) { |
||||
|
||||
if (this.paused) { |
||||
// We are going to pause
|
||||
DLog.d(LOG_NAME, "Task queue pausing"); |
||||
} |
||||
synchronized (this) { |
||||
while (this.paused || queue.isEmpty()) { |
||||
try { |
||||
// We are going to run again if wait() succeeded (on notify())
|
||||
wait(); |
||||
DLog.d(LOG_NAME, "Task queue resuming"); |
||||
} catch (Exception e) { |
||||
} |
||||
} |
||||
} |
||||
|
||||
processTask(); |
||||
|
||||
if (queue.isEmpty()) { |
||||
callback.onQueueEmpty(); |
||||
// We are going to pause
|
||||
DLog.d(LOG_NAME, "Task queue pausing (queue empty)"); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
private void processTask() { |
||||
|
||||
// Get the task to execute
|
||||
DaemonTask task = queue.remove(0); |
||||
if (task == null) { |
||||
return; |
||||
} |
||||
|
||||
if (callback.isAttached()) |
||||
callback.onQueuedTaskStarted(task); |
||||
|
||||
// Ask the daemon adapter to perform the task (which does it synchronously)
|
||||
DLog.d(LOG_NAME, "Starting task: " + task.toString()); |
||||
DaemonTaskResult result = task.execute(); |
||||
|
||||
if (callback.isAttached()) |
||||
callback.onQueuedTaskFinished(task); |
||||
|
||||
// Return the result (to the UI thread)
|
||||
DLog.d(LOG_NAME, "Task result: " + (result == null? "null": result.toString())); |
||||
if (result != null && !this.paused && callback.isAttached()) { |
||||
if (result.wasSuccessful()) { |
||||
callback.onTaskSuccess((DaemonTaskSuccessResult) result); |
||||
} else { |
||||
callback.onTaskFailure((DaemonTaskFailureResult) result); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,59 +0,0 @@
@@ -1,59 +0,0 @@
|
||||
/* |
||||
* This file is part of Transdroid <http://www.transdroid.org>
|
||||
* |
||||
* Transdroid 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 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
* |
||||
*/ |
||||
package org.transdroid.daemon.util; |
||||
|
||||
/** |
||||
* Universal logger; applications using this library should |
||||
* attach an ITLogger using <code>setLogger(ITLogger)</code> |
||||
* to receive any logging information from the daemons. |
||||
* |
||||
* @author erickok |
||||
*/ |
||||
public class DLog { |
||||
|
||||
private static final String LOG_TAG = "Transdroid"; |
||||
|
||||
private static ITLogger instance = null; |
||||
|
||||
public static void setLogger(ITLogger logger) { |
||||
instance = logger; |
||||
} |
||||
|
||||
/** |
||||
* Send a DEBUG log message. |
||||
* @param self Unique source tag, identifying the part of Transdroid it happens in |
||||
* @param msg The debug message to log |
||||
*/ |
||||
public static void d(String self, String msg) { |
||||
if (instance != null) { |
||||
instance.d(LOG_TAG, self + ": " + msg); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Send an ERROR log message. |
||||
* @param self Unique source tag, identifying the part of Transdroid it happens in |
||||
* @param msg The error message to log |
||||
*/ |
||||
public static void e(String self, String msg) { |
||||
if (instance != null) { |
||||
instance.e(LOG_TAG, self + ": " + msg); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,44 +0,0 @@
@@ -1,44 +0,0 @@
|
||||
/* |
||||
* This file is part of Transdroid <http://www.transdroid.org>
|
||||
* |
||||
* Transdroid 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 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
* |
||||
*/ |
||||
package org.transdroid.daemon.util; |
||||
|
||||
/** |
||||
* Interface that should be implemented for any logging |
||||
* information to get from the daemons. Applications using |
||||
* this library should attach an instance using |
||||
* <code>TLog.setLogger(ITLogger)</code> |
||||
* |
||||
* @author erickok |
||||
*/ |
||||
public interface ITLogger { |
||||
|
||||
/** |
||||
* Send a DEBUG log message. |
||||
* @param self Unique source tag, identifying the part of Transdroid it happens in |
||||
* @param msg The debug message to log |
||||
*/ |
||||
public abstract void d(String self, String msg); |
||||
|
||||
/** |
||||
* Send an ERROR log message. |
||||
* @param self Unique source tag, identifying the part of Transdroid it happens in |
||||
* @param msg The error message to log |
||||
*/ |
||||
public abstract void e(String self, String msg); |
||||
|
||||
} |
@ -1,77 +0,0 @@
@@ -1,77 +0,0 @@
|
||||
/* |
||||
* Copyright (C) 2009 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 org.transdroid.daemon.util; |
||||
|
||||
/** |
||||
* Container to ease passing around a tuple of two objects. This object provides a sensible |
||||
* implementation of equals(), returning true if equals() is true on each of the contained |
||||
* objects. |
||||
*/ |
||||
public class Pair<F, S> { |
||||
public final F first; |
||||
public final S second; |
||||
|
||||
/** |
||||
* Constructor for a Pair. If either are null then equals() and hashCode() will throw |
||||
* a NullPointerException. |
||||
* @param first the first object in the Pair |
||||
* @param second the second object in the pair |
||||
*/ |
||||
public Pair(F first, S second) { |
||||
this.first = first; |
||||
this.second = second; |
||||
} |
||||
|
||||
/** |
||||
* Checks the two objects for equality by delegating to their respective equals() methods. |
||||
* @param o the Pair to which this one is to be checked for equality |
||||
* @return true if the underlying objects of the Pair are both considered equals() |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
public boolean equals(Object o) { |
||||
if (o == this) return true; |
||||
if (!(o instanceof Pair)) return false; |
||||
final Pair<F, S> other; |
||||
try { |
||||
other = (Pair<F, S>) o; |
||||
} catch (ClassCastException e) { |
||||
return false; |
||||
} |
||||
return first.equals(other.first) && second.equals(other.second); |
||||
} |
||||
|
||||
/** |
||||
* Compute a hash code using the hash codes of the underlying objects |
||||
* @return a hashcode of the Pair |
||||
*/ |
||||
public int hashCode() { |
||||
int result = 17; |
||||
result = 31 * result + first.hashCode(); |
||||
result = 31 * result + second.hashCode(); |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* Convenience method for creating an appropriately typed pair. |
||||
* @param a the first object in the Pair |
||||
* @param b the second object in the pair |
||||
* @return a Pair that is templatized with the types of a and b |
||||
*/ |
||||
public static <A, B> Pair <A, B> create(A a, B b) { |
||||
return new Pair<A, B>(a, b); |
||||
} |
||||
} |
Loading…
Reference in new issue