1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 02:39:23 +02:00

use server 'all.hashes' method

This commit is contained in:
Nikolay Pultsin 2014-06-29 16:54:51 +01:00
parent 307f6a9abd
commit a5c4efbcc0

View file

@ -42,6 +42,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
private final List<Book> myQueue = Collections.synchronizedList(new LinkedList<Book>()); private final List<Book> myQueue = Collections.synchronizedList(new LinkedList<Book>());
private final Set<Book> myProcessed = new HashSet<Book>(); private final Set<Book> myProcessed = new HashSet<Book>();
private final Set<String> myHashesFromServer = new HashSet<String>();
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
@ -63,32 +64,45 @@ public class SynchroniserService extends Service implements IBookCollection.List
if (ourSynchronizationThread == null) { if (ourSynchronizationThread == null) {
ourSynchronizationThread = new Thread() { ourSynchronizationThread = new Thread() {
public void run() { public void run() {
System.err.println("HELLO THREAD");
try { try {
ourSynchronizationThread.sleep(5000); System.err.println("HELLO THREAD");
} catch (InterruptedException e) { myHashesFromServer.clear();
} try {
System.err.println("START SYNCRONIZING"); myNetworkContext.perform(new JsonRequest("all.hashes", null) {
for (BookQuery q = new BookQuery(new Filter.Empty(), 20);; q = q.next()) { @Override
final List<Book> books = myCollection.books(q); public void processResponse(Object response) {
if (books.isEmpty()) { myHashesFromServer.addAll((List)response);
break; }
});
System.err.println("RECEIVED: " + myHashesFromServer.size() + " HASHES");
} catch (Exception e) {
e.printStackTrace();
System.err.println("DO NOT SYNCHRONIZE: ALL HASHES REQUEST FAILED");
return;
} }
for (Book b : books) { System.err.println("START SYNCRONIZATION");
addBook(b); for (BookQuery q = new BookQuery(new Filter.Empty(), 20);; q = q.next()) {
final List<Book> books = myCollection.books(q);
if (books.isEmpty()) {
break;
}
for (Book b : books) {
addBook(b);
}
} }
} while (!myQueue.isEmpty()) {
while (!myQueue.isEmpty()) { final Book book = myQueue.remove(0);
final Book book = myQueue.remove(0); if (myProcessed.contains(book)) {
if (myProcessed.contains(book)) { continue;
continue; }
myProcessed.add(book);
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
uploadBookToServer(book.File.getPhysicalFile());
} }
myProcessed.add(book); } finally {
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]"); System.err.println("BYE-BYE THREAD");
uploadBookToServer(book.File.getPhysicalFile()); ourSynchronizationThread = null;
} }
System.err.println("BYE-BYE THREAD");
ourSynchronizationThread = null;
} }
}; };
ourSynchronizationThread.setPriority(Thread.MIN_PRIORITY); ourSynchronizationThread.setPriority(Thread.MIN_PRIORITY);
@ -128,7 +142,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
protected abstract void processResponse(Object response); protected abstract void processResponse(Object response);
} }
private static final class UploadRequest extends ZLNetworkRequest.FileUpload { private final class UploadRequest extends ZLNetworkRequest.FileUpload {
UploadRequest(File file) { UploadRequest(File file) {
super(BASE_URL + "book.upload", file, false); super(BASE_URL + "book.upload", file, false);
} }
@ -143,10 +157,12 @@ public class SynchroniserService extends Service implements IBookCollection.List
} }
final Object response = JSONValue.parse(buffer.toString()); final Object response = JSONValue.parse(buffer.toString());
String id = null; String id = null;
List<String> hashes = null;
String error = null; String error = null;
try { try {
if (response instanceof Map) { if (response instanceof Map) {
id = (String)((Map)response).get("id"); id = (String)((Map)response).get("id");
hashes = (List<String>)((Map)response).get("hashes");
error = (String)((Map)response).get("error"); error = (String)((Map)response).get("error");
} }
} catch (Exception e) { } catch (Exception e) {
@ -154,8 +170,9 @@ public class SynchroniserService extends Service implements IBookCollection.List
} }
if (error != null) { if (error != null) {
System.err.println("UPLOAD FAILURE: " + error); System.err.println("UPLOAD FAILURE: " + error);
} else if (id != null) { } else if (id != null && hashes != null) {
System.err.println("UPLOADED SUCCESSFULLY: " + id); System.err.println("UPLOADED SUCCESSFULLY: " + id);
myHashesFromServer.addAll(hashes);
} else { } else {
System.err.println("UNEXPECED RESPONSE: " + response); System.err.println("UNEXPECED RESPONSE: " + response);
} }
@ -168,6 +185,10 @@ public class SynchroniserService extends Service implements IBookCollection.List
System.err.println("Failed: SHA-1 checksum not computed"); System.err.println("Failed: SHA-1 checksum not computed");
return; return;
} }
if (myHashesFromServer.contains(uid.Id.toLowerCase())) {
System.err.println("HASH ALREADY IN THE TABLE");
return;
}
final Map<String,Object> result = new HashMap<String,Object>(); final Map<String,Object> result = new HashMap<String,Object>();
final JsonRequest verificationRequest = final JsonRequest verificationRequest =
new JsonRequest("books.by.hash", Collections.singletonMap("sha1", uid.Id)) { new JsonRequest("books.by.hash", Collections.singletonMap("sha1", uid.Id)) {
@ -184,7 +205,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
final String csrfToken = myNetworkContext.getCookieValue(DOMAIN, "csrftoken"); final String csrfToken = myNetworkContext.getCookieValue(DOMAIN, "csrftoken");
final Object response = result.get("result"); final Object response = result.get("result");
try { try {
final List responseList = (List)response; final List<Map<String,Object>> responseList = (List<Map<String,Object>>)response;
if (responseList.isEmpty()) { if (responseList.isEmpty()) {
try { try {
final UploadRequest uploadRequest = new UploadRequest(file.javaFile()); final UploadRequest uploadRequest = new UploadRequest(file.javaFile());
@ -195,8 +216,10 @@ public class SynchroniserService extends Service implements IBookCollection.List
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
final Map<String,String> firstBookInfo = (Map<String,String>)responseList.get(0); for (Map<String,Object> bookInfo : responseList) {
System.err.println("BOOK ALREADY UPLOADED: " + firstBookInfo.get("id")); System.err.println("BOOK ALREADY UPLOADED: " + bookInfo.get("id"));
myHashesFromServer.addAll((List<String>)bookInfo.get("hashes"));
}
} }
} catch (Exception e) { } catch (Exception e) {
System.err.println("UNEXPECTED RESPONSE: " + response); System.err.println("UNEXPECTED RESPONSE: " + response);