This is an automated email from the git hooks/post-receive script. New commit to branch feature/sync in repository jtimer. See https://gitlab.nuiton.org/chorem/jtimer.git commit f31cbedbd14230f8f4e115f3ea08e6d5e7444486 Author: servantie <servantie.c@gmail.com> Date: Thu Jun 2 17:34:13 2016 +0200 added an automatic synchronization (every min), currently synchronizes all the times of the tasks that have been modified since the last automatic sync. Sends all the times from creation date for now --- .../java/org/chorem/jtimer/data/TimerCore.java | 10 + .../chorem/jtimer/io/TimerTaskSynchronizer.java | 262 +++++++++++++++++++++ 2 files changed, 272 insertions(+) diff --git a/src/main/java/org/chorem/jtimer/data/TimerCore.java b/src/main/java/org/chorem/jtimer/data/TimerCore.java index 2234d9b..f8655bd 100644 --- a/src/main/java/org/chorem/jtimer/data/TimerCore.java +++ b/src/main/java/org/chorem/jtimer/data/TimerCore.java @@ -38,6 +38,7 @@ import org.chorem.jtimer.JTimerFactory; import org.chorem.jtimer.entities.TimerProject; import org.chorem.jtimer.io.DataLockingException; import org.chorem.jtimer.io.Saver; +import org.chorem.jtimer.io.TimerTaskSynchronizer; /** * TimerCore @@ -59,6 +60,9 @@ public class TimerCore { /** saver io controller. */ protected Saver saver; + /** sync */ + protected TimerTaskSynchronizer synchronizer; + /** * Constructor. */ @@ -87,6 +91,12 @@ public class TimerCore { data.addDataEventListener(saver); } + //init sync + synchronizer = new TimerTaskSynchronizer(this); + if (synchronizer != null) { + data.addDataEventListener(synchronizer); + } + } /** diff --git a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java new file mode 100644 index 0000000..d254548 --- /dev/null +++ b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java @@ -0,0 +1,262 @@ +package org.chorem.jtimer.io; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.jtimer.data.DataEventListener; +import org.chorem.jtimer.data.TimerCore; +import org.chorem.jtimer.entities.TimerProject; +import org.chorem.jtimer.entities.TimerTask; +import org.chorem.jtimer.entities.TimerTaskHelper; + +import java.io.*; +import java.net.*; +import java.time.LocalDateTime; +import java.util.*; +import java.util.Timer; + +/** + * + * Class dealing with automated synchronization of tasks + * + * Created by servantie on 01/06/16. + */ +public class TimerTaskSynchronizer implements DataEventListener { + + /** log */ + private static Log log = LogFactory.getLog(TimerTaskSynchronizer.class); + + /** auto sync delay */ + protected int autoSyncDelay = 1000 * 60; //1min for testing purposes + + + /** timer to schedule syncs */ + protected Timer timer; + + /** timer core (to retrieve tasks) */ + protected TimerCore core; + + /** Tasks to sync */ + protected Collection<TimerTask> tasksToSync; + + + public TimerTaskSynchronizer(TimerCore core) { + + timer = new Timer(); + timer.schedule(new UpdateTask(), autoSyncDelay, autoSyncDelay); + log.info("Starting synchronising thread"); + + this.core = core; + + + tasksToSync = Collections.synchronizedCollection(new ArrayList<>()); + } + + + public void setAutoSyncDelay(int autoSyncDelay) { + if (autoSyncDelay>0) { + this.autoSyncDelay = autoSyncDelay; + } + } + protected class UpdateTask extends java.util.TimerTask { + public void run() { + if (log.isDebugEnabled()) { + log.debug("Synchronizer wake up"); + } + synchronizeTasks(); + } + + + public void synchronizeTasks(){ + + synchronized (tasksToSync) { + Collection<TimerTask> remainingTasks = new ArrayList<>(tasksToSync); + for (TimerTask task : tasksToSync) { + //do the sync, store the result + boolean syncDone = synchronizeSingleTask(task); + if (remainingTasks.contains(task)) { + remainingTasks.remove(task); + } + log.info(" task synced : " + syncDone); + + + } + tasksToSync = remainingTasks; + } + } + + + + } + + + + /** + * Makes one synchronization for a task, without annotations by default + * + * @param task + */ + public boolean synchronizeSingleTask(TimerTask task){ + String updateJsonString = TimerTaskHelper.taskToJSONFormat(task, false).toString(); + String syncURl = task.getSynchronisingURL(); + String charset = "UTF-8"; + HttpURLConnection connection = null; + URL url = null; + try { + url = new URL("http://" + syncURl); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + try { + connection = (HttpURLConnection) url.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + connection.setUseCaches(false); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setRequestProperty("Content-Length", "" + updateJsonString.length()); + connection.setRequestProperty("Accept-Charset", charset); + connection.setRequestProperty("Content-Type", "application/json"); + try { + connection.setRequestMethod("POST"); + } catch (ProtocolException e) { + e.printStackTrace(); + } + byte[] postDataBytes = new byte[0]; + try { + postDataBytes = updateJsonString.toString().getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + try { + connection.getOutputStream().write(postDataBytes); + } catch (IOException e) { + e.printStackTrace(); + } + + //get the header fields + Map<String, List<String>> map = connection.getHeaderFields(); + boolean hasUpdated = false; + //check for positive answer from server + for (Map.Entry<String, List<String>> entry : map.entrySet()) { + for (String s : entry.getValue()) { + if (s.equals("HTTP/1.1 200 OK")) { + hasUpdated = true; + } + } + } + if (hasUpdated) { + task.setLastSync(LocalDateTime.now()); + return true; + + } + else { + log.error("Sync error, wrong URL?"); + return false; + + } + + + } + + + + + + @Override + public void addProject(TimerProject project) { + + } + + @Override + public void addTask(TimerTask task) { + } + + @Override + public void modifyProject(TimerProject project) { + + } + + @Override + public void modifyTask(TimerTask task) { + //when a task is modified, add it to the to sync list (if it isn't already there= + if(!tasksToSync.contains(task)) { + tasksToSync.add(task); + } + } + + @Override + public void deleteProject(TimerProject project) { + + } + + @Override + public void deleteTask(TimerTask task) { + if (tasksToSync.contains(task)) { + tasksToSync.remove(task); + + } + } + + @Override + public void setAnnotation(TimerTask task, Date date, String annotation) { + + } + + @Override + public void setTaskTime(TimerTask task, Date date, Long time) { + } + + @Override + public void changeClosedState(TimerTask task) { + + } + + @Override + public void preMoveTask(TimerTask task) { + + } + + @Override + public void moveTask(TimerTask task) { + + } + + @Override + public void preMergeTasks(TimerTask destinationTask, List<TimerTask> otherTasks) { + + } + + @Override + public void postMergeTasks(TimerTask destinationTask, List<TimerTask> otherTasks) { + + } + + @Override + public void startTask(TimerTask task) { + } + + @Override + public void stopTask(TimerTask task) { + if(!tasksToSync.contains(task)) { + tasksToSync.add(task); + } + } + + @Override + public void dataLoaded(Collection<TimerProject> projects) { + + } + + @Override + public void modifyTaskURL(TimerTask task, String newURL) { + if(!tasksToSync.contains(task) && (!task.getSynchronisingURL().equals(null))) { + tasksToSync.add(task); + } + } + + @Override + public void modifyTaskSyncDate(TimerTask task, LocalDateTime newDate) { + + } +} -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.