From: <se...@us...> - 2008-05-10 10:09:07
|
Revision: 119 http://acmcontester.svn.sourceforge.net/acmcontester/?rev=119&view=rev Author: sem62 Date: 2008-05-10 03:09:14 -0700 (Sat, 10 May 2008) Log Message: ----------- * Autosave process modification: AvtoSave called after last saving or opening with some interval, but not every interval milliseconds. * Not calling process. Simply getting LectureModel.conf from template with some interval after last communicating. Modified Paths: -------------- WebEditor/src/edu/lnu/FireFly/WebEditor/Data/AutoSaveThread.java WebEditor/src/edu/lnu/FireFly/WebEditor/Settings.java WebEditor/src/edu/lnu/FireFly/WebEditor/WSClients/WebEditorServiceClient.java WebEditor/src/edu/lnu/FireFly/WebEditor/WebEditor.java Modified: WebEditor/src/edu/lnu/FireFly/WebEditor/Data/AutoSaveThread.java =================================================================== --- WebEditor/src/edu/lnu/FireFly/WebEditor/Data/AutoSaveThread.java 2008-05-10 09:32:21 UTC (rev 118) +++ WebEditor/src/edu/lnu/FireFly/WebEditor/Data/AutoSaveThread.java 2008-05-10 10:09:14 UTC (rev 119) @@ -6,15 +6,18 @@ public class AutoSaveThread implements Runnable { + private long nextSaveAt = 0; + @Override public void run() { + reset(); + try { while (true) { - Thread.sleep(Settings.getInstance().getAutoSaveInterval()); - try { - WebEditor.instance.saveManifest(); - } catch (TestDocException e) { - System.out.println("WARNING: Can't save manifest."); + Thread.sleep(5000); + if (System.currentTimeMillis() > nextSaveAt){ + processSave(); + reset(); } } } catch (InterruptedException e) { @@ -23,4 +26,16 @@ } } + public void reset() { + nextSaveAt = System.currentTimeMillis() + Settings.getInstance().getAutoSaveInterval(); + } + + private void processSave() { + try { + WebEditor.instance.saveManifest(); + } catch (TestDocException e) { + System.out.println("WARNING: Can't save manifest."); + } + } + } Modified: WebEditor/src/edu/lnu/FireFly/WebEditor/Settings.java =================================================================== --- WebEditor/src/edu/lnu/FireFly/WebEditor/Settings.java 2008-05-10 09:32:21 UTC (rev 118) +++ WebEditor/src/edu/lnu/FireFly/WebEditor/Settings.java 2008-05-10 10:09:14 UTC (rev 119) @@ -7,6 +7,7 @@ private String resourceLocation = "http://127.0.0.1/sem/WebEditor/"; private long autoSaveInterval = 5 * 60 * 1000; + private long nopInterval = 60 * 1000; public static Settings obj = null; @@ -58,4 +59,12 @@ public void setAutoSaveInterval(long autoSaveInterval) { this.autoSaveInterval = autoSaveInterval; } + + public long getNopInterval() { + return nopInterval; + } + + public void setNopInterval(long nopInterval) { + this.nopInterval = nopInterval; + } } Modified: WebEditor/src/edu/lnu/FireFly/WebEditor/WSClients/WebEditorServiceClient.java =================================================================== --- WebEditor/src/edu/lnu/FireFly/WebEditor/WSClients/WebEditorServiceClient.java 2008-05-10 09:32:21 UTC (rev 118) +++ WebEditor/src/edu/lnu/FireFly/WebEditor/WSClients/WebEditorServiceClient.java 2008-05-10 10:09:14 UTC (rev 119) @@ -14,43 +14,68 @@ private HashMap<String, String> templates; private HashMap<String, String> contents; - + private long lastCommunicationTime = 0; + private static WebEditorServiceClient obj = null; - - private WebEditorServiceClient(){ - templates = new HashMap<String, String>(); - contents = new HashMap<String, String>(); + + private WebEditorServiceClient() { + templates = new HashMap<String, String>(); + contents = new HashMap<String, String>(); + + nopThread = new Thread(new Runnable() { + public void run() { + while (true) { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + // do nothing. + } + + long lastCommunication = WebEditorServiceClient + .getInstance().getLastCommunicationTime(); + + if (System.currentTimeMillis() - lastCommunication > Settings.getInstance().getNopInterval()){ + WebEditorServiceClient.getInstance().nop(); + } + } + } + }); + nopThread.start(); } - - public static WebEditorServiceClient getInstance(){ - if (obj == null){ + + public static WebEditorServiceClient getInstance() { + if (obj == null) { obj = new WebEditorServiceClient(); } - + return obj; } + /** - * @param fName File name - * @param location Location name = template | course + * @param fName + * File name + * @param location + * Location name = template | course * @return */ public String getFileContent(String fName, String location) { - System.out.println("Getting file's content (fName=" + fName + "; location=" + location + ")..."); - - if (location.equalsIgnoreCase("template")){ + System.out.println("Getting file's content (fName=" + fName + + "; location=" + location + ")..."); + + if (location.equalsIgnoreCase("template")) { String template = templates.get(fName); - if (template != null){ + if (template != null) { System.out.println("Founded in templates hash."); return template; } } else { String content = contents.get(fName); - if (content != null){ + if (content != null) { System.out.println("Founded in contents hash."); return content; } } - + String result = ""; try { // Construct data @@ -58,18 +83,18 @@ + URLEncoder.encode(fName, "UTF-8"); data += "&" + URLEncoder.encode("location", "UTF-8") + "=" - + URLEncoder.encode(location, "UTF-8"); + + URLEncoder.encode(location, "UTF-8"); // Send data URL url = new URL(Settings.getInstance().getServiceLocation() + "getFileContent.php"); - + URLConnection conn = url.openConnection(); conn.setDoOutput(true); - + OutputStreamWriter wr = new OutputStreamWriter(conn .getOutputStream()); - + wr.write(data); wr.flush(); @@ -85,23 +110,24 @@ } catch (Exception e) { e.printStackTrace(); } - - if (location.equalsIgnoreCase("template")){ + + if (location.equalsIgnoreCase("template")) { templates.put(fName, result); } else { contents.put(fName, result); } - + + lastCommunicationTime = System.currentTimeMillis(); return result; } public void setFileContent(String fName, String content) { System.out.println("Setting file's content (fName=" + fName + ")..."); - + String hashedContent = contents.get(fName); - if (content.equals(hashedContent)){ + if (content.equals(hashedContent)) { System.out.println("Not modified."); - return ; + return; } contents.put(fName, content); @@ -112,18 +138,18 @@ String data = URLEncoder.encode("fName", "UTF-8") + "=" + URLEncoder.encode(fName, "UTF-8"); data += "&" + URLEncoder.encode("content", "UTF-8") + "=" - + URLEncoder.encode(content, "UTF-8"); + + URLEncoder.encode(content, "UTF-8"); // Send data URL url = new URL(Settings.getInstance().getServiceLocation() + "setFileContent.php"); - + URLConnection conn = url.openConnection(); conn.setDoOutput(true); - + OutputStreamWriter wr = new OutputStreamWriter(conn .getOutputStream()); - + wr.write(data); wr.flush(); @@ -139,5 +165,24 @@ } catch (Exception e) { e.printStackTrace(); } + + lastCommunicationTime = System.currentTimeMillis(); } + + protected void nop() { + lastCommunicationTime = System.currentTimeMillis(); + System.out.println("Nop"); + + getFileContent("LectureModel.conf", "template"); + } + + protected Thread nopThread; + + public long getLastCommunicationTime() { + return lastCommunicationTime; + } + + public void setLastCommunicationTime(long lastCommunicationTime) { + this.lastCommunicationTime = lastCommunicationTime; + } } Modified: WebEditor/src/edu/lnu/FireFly/WebEditor/WebEditor.java =================================================================== --- WebEditor/src/edu/lnu/FireFly/WebEditor/WebEditor.java 2008-05-10 09:32:21 UTC (rev 118) +++ WebEditor/src/edu/lnu/FireFly/WebEditor/WebEditor.java 2008-05-10 10:09:14 UTC (rev 119) @@ -49,6 +49,10 @@ protected JMenuItem saveMenuItem = null; + private Thread threadForAautoSave; + + private AutoSaveThread autoSaveThread; + @Override public void actionPerformed(ActionEvent arg0) { try { @@ -66,12 +70,18 @@ public void saveManifest() throws TestDocException { Parser parser = new Parser(); + + autoSaveThread.reset(); + WebEditorServiceClient.getInstance().setFileContent("imsmanifest.xml", parser.toXML(manifest)); } public void openManifest() throws TestDocException { Parser parser = new Parser(); + + autoSaveThread.reset(); + manifest = parser.fromXML(WebEditorServiceClient.getInstance() .getFileContent("imsmanifest.xml", "course")); TreeDataModel.initInstance(manifest); @@ -86,26 +96,14 @@ @Override public void init() { - String paramCourseLocation = getParameter("courseLocation"); - String paramServiceLocation = getParameter("serviceLocation"); - String paramTemplateLocation = getParameter("templateLocation"); - String paramResourceLocation = getParameter("resourceLocation"); + parseParameter(); - if (paramCourseLocation != null){ - Settings.getInstance().setCourseLocation(paramCourseLocation); - } - if (paramServiceLocation != null){ - Settings.getInstance().setServiceLocation(paramServiceLocation); - } - if (paramTemplateLocation != null){ - Settings.getInstance().setTemplateLocation(paramTemplateLocation); - } - if (paramResourceLocation != null){ - Settings.getInstance().setResourceLocation(paramResourceLocation); - } - instance = this; + autoSaveThread = new AutoSaveThread(); + threadForAautoSave = new Thread(autoSaveThread); + threadForAautoSave.start(); + initMenu(); ItemModels.clear(); ItemModels.registerModel(new LectureModel()); @@ -121,10 +119,6 @@ CreateNewCourse(); } - Thread autoSaveThread = new Thread(new AutoSaveThread()); - - autoSaveThread.start(); - tree = new JTree(TreeDataModel.getInstance()); this.add(new JScrollPane(tree)); @@ -133,6 +127,26 @@ tree.addMouseListener(new CourseTreePopupMenu()); } + private void parseParameter() { + String paramCourseLocation = getParameter("courseLocation"); + String paramServiceLocation = getParameter("serviceLocation"); + String paramTemplateLocation = getParameter("templateLocation"); + String paramResourceLocation = getParameter("resourceLocation"); + + if (paramCourseLocation != null){ + Settings.getInstance().setCourseLocation(paramCourseLocation); + } + if (paramServiceLocation != null){ + Settings.getInstance().setServiceLocation(paramServiceLocation); + } + if (paramTemplateLocation != null){ + Settings.getInstance().setTemplateLocation(paramTemplateLocation); + } + if (paramResourceLocation != null){ + Settings.getInstance().setResourceLocation(paramResourceLocation); + } + } + private void CreateNewCourse() { try { manifest = ManifestFactory.createManifest(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |