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

code sync'd wit new server interface

This commit is contained in:
Nikolay Pultsin 2014-07-23 06:45:14 +01:00
parent cfe32e4bf9
commit 45ca655288
2 changed files with 29 additions and 19 deletions

View file

@ -42,7 +42,8 @@ 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>(); private final Set<String> myActualHashesFromServer = new HashSet<String>();
private final Set<String> myDeletedHashesFromServer = new HashSet<String>();
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
@ -66,21 +67,24 @@ public class SynchroniserService extends Service implements IBookCollection.List
public void run() { public void run() {
try { try {
System.err.println("HELLO THREAD"); System.err.println("HELLO THREAD");
myHashesFromServer.clear(); myActualHashesFromServer.clear();
myDeletedHashesFromServer.clear();
try { try {
myNetworkContext.perform(new PostRequest("all.hashes", null) { myNetworkContext.perform(new PostRequest("all.hashes", null) {
@Override @Override
public void processResponse(Object response) { public void processResponse(Object response) {
myHashesFromServer.addAll((List)response); final Map<String,List<String>> map = (Map<String,List<String>>)response;
myActualHashesFromServer.addAll(map.get("actual"));
myDeletedHashesFromServer.addAll(map.get("deleted"));
} }
}); });
System.err.println("RECEIVED: " + myHashesFromServer.size() + " HASHES"); System.err.println(String.format("RECEIVED: %s/%s HASHES", myActualHashesFromServer.size(), myDeletedHashesFromServer.size()));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.err.println("DO NOT SYNCHRONIZE: ALL HASHES REQUEST FAILED"); System.err.println("DO NOT SYNCHRONIZE: ALL HASHES REQUEST FAILED");
return; return;
} }
System.err.println("START SYNCRONIZATION"); System.err.println("START SYNCRONIZATION " + new Date());
for (BookQuery q = new BookQuery(new Filter.Empty(), 20);; q = q.next()) { for (BookQuery q = new BookQuery(new Filter.Empty(), 20);; q = q.next()) {
final List<Book> books = myCollection.books(q); final List<Book> books = myCollection.books(q);
if (books.isEmpty()) { if (books.isEmpty()) {
@ -100,7 +104,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
uploadBookToServer(book); uploadBookToServer(book);
} }
} finally { } finally {
System.err.println("BYE-BYE THREAD"); System.err.println("BYE-BYE THREAD " + new Date());
ourSynchronizationThread = null; ourSynchronizationThread = null;
} }
} }
@ -157,7 +161,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
System.err.println("UPLOAD FAILURE: " + error); System.err.println("UPLOAD FAILURE: " + error);
} else if (id != null && hashes != null) { } else if (id != null && hashes != null) {
System.err.println("UPLOADED SUCCESSFULLY: " + id); System.err.println("UPLOADED SUCCESSFULLY: " + id);
myHashesFromServer.addAll(hashes); myActualHashesFromServer.addAll(hashes);
} else { } else {
System.err.println("UNEXPECED RESPONSE: " + response); System.err.println("UNEXPECED RESPONSE: " + response);
} }
@ -174,16 +178,20 @@ public class SynchroniserService extends Service implements IBookCollection.List
System.err.println("Failed: checksum not computed"); System.err.println("Failed: checksum not computed");
return; return;
} }
if (myHashesFromServer.contains(hash)) { if (myActualHashesFromServer.contains(hash)) {
System.err.println("HASH ALREADY IN THE TABLE"); System.err.println("BOOK ALREADY UPLOADED");
return;
}
if (myDeletedHashesFromServer.contains(hash)) {
System.err.println("BOOK ALREADY UPLOADED AND DELETED");
return; return;
} }
final Map<String,Object> result = new HashMap<String,Object>(); final Map<String,Object> result = new HashMap<String,Object>();
final PostRequest verificationRequest = final PostRequest verificationRequest =
new PostRequest("books.by.hash", Collections.singletonMap("sha1", hash)) { new PostRequest("book.status.by.hash", Collections.singletonMap("sha1", hash)) {
@Override @Override
public void processResponse(Object response) { public void processResponse(Object response) {
result.put("result", response); result.putAll((Map)response);
} }
}; };
try { try {
@ -192,10 +200,9 @@ public class SynchroniserService extends Service implements IBookCollection.List
e.printStackTrace(); e.printStackTrace();
} }
final String csrfToken = myNetworkContext.getCookieValue(DOMAIN, "csrftoken"); final String csrfToken = myNetworkContext.getCookieValue(DOMAIN, "csrftoken");
final Object response = result.get("result");
try { try {
final List<Map<String,Object>> responseList = (List<Map<String,Object>>)response; final String status = (String)result.get("status");
if (responseList.isEmpty()) { if ("not found".equals(status)) {
try { try {
final UploadRequest uploadRequest = new UploadRequest(file.javaFile()); final UploadRequest uploadRequest = new UploadRequest(file.javaFile());
uploadRequest.addHeader("Referer", verificationRequest.getURL()); uploadRequest.addHeader("Referer", verificationRequest.getURL());
@ -205,13 +212,17 @@ public class SynchroniserService extends Service implements IBookCollection.List
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
for (Map<String,Object> bookInfo : responseList) { final List<String> hashes = (List<String>)result.get("hashes");
System.err.println("BOOK ALREADY UPLOADED: " + bookInfo.get("id")); if ("found".equals(status)) {
myHashesFromServer.addAll((List<String>)bookInfo.get("hashes")); System.err.println("BOOK ALREADY UPLOADED");
myActualHashesFromServer.addAll(hashes);
} else /* if ("deleted".equals(status)) */ {
System.err.println("BOOK ALREADY UPLOADED AND DELETED");
myDeletedHashesFromServer.addAll(hashes);
} }
} }
} catch (Exception e) { } catch (Exception e) {
System.err.println("UNEXPECTED RESPONSE: " + response); System.err.println("UNEXPECTED RESPONSE: " + result);
} }
} }

View file

@ -728,7 +728,6 @@ public class BookCollection extends AbstractBookCollection {
return null; return null;
} }
String hash = myDatabase.getHash(book.getId(), file.javaFile().lastModified()); String hash = myDatabase.getHash(book.getId(), file.javaFile().lastModified());
System.err.println("HASH FOR " + book + " FROM DB = " + hash);
if (hash == null) { if (hash == null) {
final UID uid = BookUtil.createUid(file, "SHA-1"); final UID uid = BookUtil.createUid(file, "SHA-1");
if (uid == null) { if (uid == null) {