Author: jflesch Date: 2006-10-01 13:57:36 +0000 (Sun, 01 Oct 2006) New Revision: 10586
Modified: trunk/apps/Thaw/src/thaw/core/Config.java trunk/apps/Thaw/src/thaw/core/Core.java trunk/apps/Thaw/src/thaw/core/NodeConfigPanel.java trunk/apps/Thaw/src/thaw/core/PluginManager.java trunk/apps/Thaw/src/thaw/core/ThawConfigPanel.java trunk/apps/Thaw/src/thaw/fcp/FCPClientGet.java trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java trunk/apps/Thaw/src/thaw/fcp/FCPQueryManager.java trunk/apps/Thaw/src/thaw/i18n/thaw.properties trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties trunk/apps/Thaw/src/thaw/plugins/index/FileTable.java trunk/apps/Thaw/src/thaw/plugins/index/Index.java trunk/apps/Thaw/src/thaw/plugins/index/IndexCategory.java trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java trunk/apps/Thaw/src/thaw/plugins/index/Link.java trunk/apps/Thaw/src/thaw/plugins/index/TableCreator.java Log: Create separate sockets to transfer files (if global == true) Modified: trunk/apps/Thaw/src/thaw/core/Config.java =================================================================== --- trunk/apps/Thaw/src/thaw/core/Config.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/core/Config.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -27,7 +27,7 @@ /** * This class the thaw config. * - * @author <a href="mailto:jflesch at nerim.net">Jerome Flesch</a> + * @author <a href="mailto:jflesch at gmail.com">Jerome Flesch</a> */ public class Config { @@ -291,15 +291,24 @@ return false; } + /** + * Set the value only if it doesn't exits. + */ + public void setDefaultValue(String name, String val) { + if (getValue(name) == null) + setValue(name, val); + } public void setDefaultValues() { - setValue("nodeAddress", "127.0.0.1"); - setValue("nodePort", "9481"); - setValue("maxSimultaneousDownloads", "-1"); - setValue("maxSimultaneousInsertions", "-1"); - setValue("maxUploadSpeed", "-1"); - setValue("thawId", "thaw_"+Integer.toString((new Random()).nextInt(1000))); - setValue("advancedMode", "false"); + setDefaultValue("nodeAddress", "127.0.0.1"); + setDefaultValue("nodePort", "9481"); + setDefaultValue("maxSimultaneousDownloads", "-1"); + setDefaultValue("maxSimultaneousInsertions", "-1"); + setDefaultValue("maxUploadSpeed", "-1"); + setDefaultValue("thawId", "thaw_"+Integer.toString((new Random()).nextInt(1000))); + setDefaultValue("advancedMode", "false"); + setDefaultValue("userNickname", "Another anonymous"); + setDefaultValue("multipleSockets", "true"); } } Modified: trunk/apps/Thaw/src/thaw/core/Core.java =================================================================== --- trunk/apps/Thaw/src/thaw/core/Core.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/core/Core.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -34,7 +34,7 @@ private static String lookAndFeel = null; public final static int MAX_CONNECT_TRIES = 3; - public final static int TIME_BETWEEN_EACH_TRY = 2500; + public final static int TIME_BETWEEN_EACH_TRY = 3000; /** @@ -131,8 +131,7 @@ config = new Config(); config.loadConfig(); - if(config.isEmpty()) - config.setDefaultValues(); + config.setDefaultValues(); return true; } @@ -159,7 +158,8 @@ if(connection == null) { connection = new FCPConnection(config.getValue("nodeAddress"), Integer.parseInt(config.getValue("nodePort")), - Integer.parseInt(config.getValue("maxUploadSpeed"))); + Integer.parseInt(config.getValue("maxUploadSpeed")), + Boolean.valueOf(config.getValue("multipleSockets")).booleanValue()); } else { connection.setNodeAddress(config.getValue("nodeAddress")); connection.setNodePort(Integer.parseInt(config.getValue("nodePort"))); Modified: trunk/apps/Thaw/src/thaw/core/NodeConfigPanel.java =================================================================== --- trunk/apps/Thaw/src/thaw/core/NodeConfigPanel.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/core/NodeConfigPanel.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -8,6 +8,7 @@ import java.awt.event.ActionListener; import javax.swing.JTextField; import javax.swing.JLabel; +import javax.swing.JCheckBox; import java.util.Observer; import java.util.Observable; @@ -42,6 +43,7 @@ private JLabel[] paramLabels = new JLabel[paramNames.length]; private JTextField[] paramFields = new JTextField[configNames.length]; + private JCheckBox multipleSockets = null; public NodeConfigPanel(ConfigWindow configWindow, Core core) { @@ -63,6 +65,11 @@ nodeConfigPanel.add(paramFields[i]); } + multipleSockets = new JCheckBox(I18n.getMessage("thaw.config.multipleSockets"), + Boolean.valueOf(core.getConfig().getValue("multipleSockets")).booleanValue()); + nodeConfigPanel.add(new JLabel(" ")); + nodeConfigPanel.add(multipleSockets); + setVisibility(Boolean.valueOf(core.getConfig().getValue("advancedMode")).booleanValue()); configWindow.addObserver(this); @@ -78,6 +85,7 @@ paramFields[i].setVisible(advancedMode); } + multipleSockets.setVisible(advancedMode); } @@ -87,6 +95,8 @@ core.getConfig().setValue(configNames[i], paramFields[i].getText()); } + core.getConfig().setValue("multipleSockets", Boolean.toString(multipleSockets.isSelected())); + setVisibility(Boolean.valueOf(core.getConfig().getValue("advancedMode")).booleanValue()); } @@ -100,6 +110,8 @@ paramFields[i].setText(value); } + + multipleSockets.setSelected(Boolean.valueOf(core.getConfig().getValue("multipleSockets")).booleanValue()); } } Modified: trunk/apps/Thaw/src/thaw/core/PluginManager.java =================================================================== --- trunk/apps/Thaw/src/thaw/core/PluginManager.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/core/PluginManager.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -11,9 +11,9 @@ private final static String[] defaultPlugins = {"thaw.plugins.QueueWatcher", "thaw.plugins.InsertPlugin", "thaw.plugins.FetchPlugin", - "thaw.plugins.StatusBar", - "thaw.plugins.IndexEditor", - "thaw.plugins.IndexBrowser"}; + "thaw.plugins.StatusBar"}; + //"thaw.plugins.IndexEditor", + //"thaw.plugins.IndexBrowser"}; private Core core = null; Modified: trunk/apps/Thaw/src/thaw/core/ThawConfigPanel.java =================================================================== --- trunk/apps/Thaw/src/thaw/core/ThawConfigPanel.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/core/ThawConfigPanel.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -7,6 +7,8 @@ import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JTextField; import java.util.Observer; import java.util.Observable; @@ -20,8 +22,12 @@ private JCheckBox advancedModeBox = null; + private JLabel nicknameLabel = null; + private JTextField nicknameField = null; + private boolean advancedMode; + public ThawConfigPanel(ConfigWindow configWindow, Core core) { this.core = core; @@ -36,7 +42,17 @@ advancedModeBox = new JCheckBox(I18n.getMessage("thaw.config.advancedMode"), advancedMode); + nicknameLabel = new JLabel(I18n.getMessage("thaw.config.nickname")); + + if (core.getConfig().getValue("userNickname") == null) + nicknameField = new JTextField("Another anonymous"); + else + nicknameField = new JTextField(core.getConfig().getValue("userNickname")); + thawConfigPanel.add(advancedModeBox); + thawConfigPanel.add(new JLabel(" ")); + thawConfigPanel.add(nicknameLabel); + thawConfigPanel.add(nicknameField); configWindow.addObserver(this); } @@ -51,10 +67,12 @@ if(arg == core.getConfigWindow().getOkButton()) { advancedMode = advancedModeBox.isSelected(); core.getConfig().setValue("advancedMode", Boolean.toString(advancedMode)); + core.getConfig().setValue("userNickname", nicknameField.getText()); } if(arg == core.getConfigWindow().getCancelButton()) { advancedModeBox.setSelected(advancedMode); + nicknameField.setText(core.getConfig().getValue("userNickname")); } } Modified: trunk/apps/Thaw/src/thaw/fcp/FCPClientGet.java =================================================================== --- trunk/apps/Thaw/src/thaw/fcp/FCPClientGet.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/fcp/FCPClientGet.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -19,6 +19,7 @@ private final static int BLOCK_SIZE = 32768; private FCPQueueManager queueManager; + private FCPQueryManager duplicatedQueryManager; private String key = null; private String filename = null; /* Extract from the key */ @@ -136,11 +137,11 @@ } else { String cutcut[] = key.split("/"); - if(!key.startsWith("USK@")) { - filename = cutcut[cutcut.length-1]; - } else { - filename = cutcut[cutcut.length-2]; - } + //if(!key.startsWith("USK@")) { + filename = cutcut[cutcut.length-1]; + //} else { + //filename = cutcut[cutcut.length-2]; + //} } Logger.debug(this, "Query for getting "+key+" created"); @@ -197,8 +198,15 @@ public void update(Observable o, Object arg) { + + FCPQueryManager queryManager = null; FCPMessage message = (FCPMessage)arg; + if (o instanceof FCPQueryManager) + queryManager = (FCPQueryManager)o; + else + queryManager = queueManager.getQueryManager(); /* default one */ + if(message.getValue("Identifier") == null || !message.getValue("Identifier").equals(identifier)) return; @@ -291,6 +299,14 @@ if(message.getMessageName().equals("GetFailed")) { Logger.debug(this, "GetFailed !"); + if (message.getValue("RedirectURI") != null) { + Logger.debug(this, "Redirected !"); + key = message.getValue("RedirectURI"); + status = "Redirected ..."; + start(queueManager); + return; + } + Logger.warning(this, "==== GET FAILED ===\n"+message.toString()); if(!isRunning()) { /* Must be a "GetFailed: cancelled by caller", so we simply ignore */ @@ -376,7 +392,7 @@ notifyObservers(); - if(fetchDirectly(getPath(), fileSize, true)) { + if(fetchDirectly(queryManager.getConnection(), getPath(), fileSize, true)) { successful = true; status = "Available"; } else { @@ -387,15 +403,23 @@ Logger.info(this, "File received"); - queueManager.getQueryManager().getConnection().unlockReading(); - queueManager.getQueryManager().getConnection().unlockWriting(); + queryManager.getConnection().unlockReading(); + queryManager.getConnection().unlockWriting(); + + isLockOwner= false; running = false; progress = 100; - queueManager.getQueryManager().deleteObserver(this); + queryManager.deleteObserver(this); + if (queryManager != queueManager.getQueryManager()) { + queueManager.getQueryManager().deleteObserver(this); + queryManager.getConnection().disconnect(); + duplicatedQueryManager = null; + } + setChanged(); notifyObservers(); @@ -495,9 +519,15 @@ return false; } + if (globalQueue) { /* If not global, we need to remain on the same socket */ + duplicatedQueryManager = queueManager.getQueryManager().duplicate(identifier); + duplicatedQueryManager.addObserver(this); + } else + duplicatedQueryManager = queueManager.getQueryManager(); - Logger.info(this, "Waiting socket avaibility ..."); - status = "Waiting socket avaibility ..."; + + Logger.info(this, "Waiting for socket avaibility ..."); + status = "Waiting for socket avaibility ..."; progress = 99; running = true; @@ -505,7 +535,7 @@ notifyObservers(); - Thread fork = new Thread(new UnlockWaiter(this, queueManager.getQueryManager().getConnection(), dir)); + Thread fork = new Thread(new UnlockWaiter(this, duplicatedQueryManager.getConnection(), dir)); fork.start(); return true; @@ -538,7 +568,7 @@ - queueManager.getQueryManager().writeMessage(getRequestStatus, false); + duplicatedQueryManager.writeMessage(getRequestStatus, false); return true; } @@ -552,15 +582,11 @@ - private boolean fetchDirectly(String file, long size, boolean reallyWrite) { - FCPConnection connection; - + private boolean fetchDirectly(FCPConnection connection, String file, long size, boolean reallyWrite) { File newFile = new File(file); FileOutputStream fileWriter = null; - connection = queueManager.getQueryManager().getConnection(); - if(reallyWrite) { Logger.info(this, "Writing file to disk ..."); Modified: trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java =================================================================== --- trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -76,7 +76,7 @@ } /** - * To start anew insertion. + * To start a new insertion. */ public FCPClientPut(File file, int keyType, int rev, String name, @@ -613,6 +613,9 @@ fatal = false; } + Logger.warning(this, "==== PUT FAILED ==="); + Logger.warning(this, msg.toString()); + setChanged(); notifyObservers(); return; Modified: trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java =================================================================== --- trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -3,7 +3,6 @@ import java.net.Socket; import java.io.InputStream; import java.io.OutputStream; -/* import java.io.BufferedReader; */ import java.io.BufferedInputStream; import java.util.Observable; @@ -49,22 +48,29 @@ private long lastWrite = 0; /* real writes ; System.currentTimeMillis() */ + private boolean duplicationAllowed = true; + /** * Don't connect. Call connect() for that. * @param maxUploadSpeed in KB: -1 means no limit + * @param duplicationAllowed FCPClientGet and FCPClientPut will be allowed to open a separate socket to transfer the files */ public FCPConnection(String nodeAddress, int port, - int maxUploadSpeed) + int maxUploadSpeed, + boolean duplicationAllowed) { if(DEBUG_MODE) { Logger.notice(this, "DEBUG_MODE ACTIVATED"); } + maxUploadSpeed = -1; + setNodeAddress(nodeAddress); setNodePort(port); setMaxUploadSpeed(maxUploadSpeed); + setDuplicationAllowed(duplicationAllowed); } @@ -80,6 +86,9 @@ this.maxUploadSpeed = max; } + public void setDuplicationAllowed(boolean allowed) { + this.duplicationAllowed = allowed; + } public void disconnect() { try { @@ -89,7 +98,7 @@ Logger.info(this, "Disconnect(): Already disconnected."); } } catch(java.io.IOException e) { - Logger.warning(this, "Unable to close cleanly the connection : "+e.toString()); + Logger.warning(this, "Unable to close cleanly the connection : "+e.toString() +" ; "+e.getMessage()); } socket = null; @@ -130,7 +139,7 @@ return false; } catch(java.io.IOException e) { Logger.error(this, "Error while trying to connect to "+nodeAddress+":"+port+" : "+ - e.toString()); + e.toString() + " ; "+e.getMessage()); socket = null; return false; } @@ -144,7 +153,7 @@ in = socket.getInputStream(); out = socket.getOutputStream(); } catch(java.io.IOException e) { - Logger.error(this, "Socket and connection established, but unable to get in/output streams ?! : "+e.toString()); + Logger.error(this, "Socket and connection established, but unable to get in/output streams ?! : "+e.toString()+ " ; "+e.getMessage() ); return false; } @@ -252,7 +261,7 @@ out.write(data); } catch(java.io.IOException e) { - Logger.warning(this, "Unable to write() on the socket ?! : "+ e.toString()); + Logger.warning(this, "Unable to write() on the socket ?! : "+ e.toString()+ " ; "+e.getMessage()); disconnect(); return false; } @@ -344,7 +353,7 @@ return rdBytes; } catch(java.io.IOException e) { Logger.error(this, "IOException while reading raw bytes on socket => disconnection"); - Logger.error(this, e.getMessage() + ":" +e.getCause()); + Logger.error(this, e.getMessage() + ":" +e.getCause().toString()+ " ; "+e.getMessage() ); disconnect(); return -2; /* -1 can mean eof */ } @@ -429,9 +438,9 @@ } catch (java.io.IOException e) { if(isConnected()) - Logger.error(this, "IOException while reading but still connected, wtf? : "+e.toString()); + Logger.error(this, "IOException while reading but still connected, wtf? : "+e.toString()+ " ; "+e.getMessage() ); else - Logger.notice(this, "IOException. Disconnected."); + Logger.notice(this, "IOException. Disconnected. : "+e.toString() + " ; "+e.getMessage()); disconnect(); @@ -444,4 +453,28 @@ return null; } + + /** + * If duplicationAllowed, returns a copy of this object, using a different socket and differents lock / buffer. + * If !duplicationAllowed, returns this object. + * The duplicate socket is just connected but not initialized (ClientHello, etc). + */ + public FCPConnection duplicate() { + if (!duplicationAllowed) + return this; + + Logger.info(this, "Duplicating connection to the node ..."); + + FCPConnection newConnection; + + newConnection = new FCPConnection(nodeAddress, port, -1, duplicationAllowed); /* upload limit is useless here, since we can't do a global limit on all the connections */ + + if (!newConnection.connect()) { + Logger.warning(this, "Unable to duplicate socket !"); + return this; + } + + return newConnection; + } + } Modified: trunk/apps/Thaw/src/thaw/fcp/FCPQueryManager.java =================================================================== --- trunk/apps/Thaw/src/thaw/fcp/FCPQueryManager.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/fcp/FCPQueryManager.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -135,5 +135,36 @@ } } + + /** + * This function is mainly used by FCPClientGet to have a separate socket to transfer the files. + * If FCPConnection is allowed to duplicate itself, then it will duplicate it and create a dedicated FCPQueryManager for. + * A FCPClientHello is sent with the given id. + * @return This object if it cannot duplicate FCPConnection + */ + public FCPQueryManager duplicate(String connectionId) { + FCPConnection newConnection; + FCPQueryManager queryManager; + + newConnection = connection.duplicate(); + + if (newConnection == connection) + return this; + + queryManager = new FCPQueryManager(newConnection); + + queryManager.startListening(); + + FCPClientHello clientHello = new FCPClientHello(queryManager, connectionId); + + if (!clientHello.start(null)) { + Logger.warning(this, "ID already used ?! Using initial socket ..."); + newConnection.disconnect(); + return this; + } + + return queryManager; + } + } Modified: trunk/apps/Thaw/src/thaw/i18n/thaw.properties =================================================================== --- trunk/apps/Thaw/src/thaw/i18n/thaw.properties 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/i18n/thaw.properties 2006-10-01 13:57:36 UTC (rev 10586) @@ -102,7 +102,10 @@ thaw.config.advancedMode=Advanced mode +thaw.config.nickname=Your nickname +thaw.config.multipleSockets=Allow Thaw to exchange many files at ones with the node + ## Plugins thaw.plugin.insert.fileToInsert=File to insert thaw.plugin.insert.filesToInsert=File(s) to insert Modified: trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties =================================================================== --- trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties 2006-10-01 13:57:36 UTC (rev 10586) @@ -193,8 +193,8 @@ thaw.plugin.index.yourIndexes=Vos indexes thaw.plugin.index.indexes=Indexes -thaw.plugin.index.addIndex=Add a file index -thaw.plugin.index.createIndex=Cr?er a file index +thaw.plugin.index.addIndex=Ajouter un index de fichiers +thaw.plugin.index.createIndex=Cr?er un index de fichiers thaw.plugin.index.addCategory=Ajouter un r?pertoire thaw.plugin.index.rename=Renommer thaw.plugin.index.delete=Effacer Modified: trunk/apps/Thaw/src/thaw/plugins/index/FileTable.java =================================================================== --- trunk/apps/Thaw/src/thaw/plugins/index/FileTable.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/plugins/index/FileTable.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -223,7 +223,7 @@ FCPClientPut insertion = new FCPClientPut(new java.io.File(file.getLocalPath()), 0, 0, null, null, 4, - true, 2, false); /* getCHKOnly */ + true, 2, true); /* getCHKOnly */ insertion.start(queueManager); file.setTransfer(insertion); Modified: trunk/apps/Thaw/src/thaw/plugins/index/Index.java =================================================================== --- trunk/apps/Thaw/src/thaw/plugins/index/Index.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/plugins/index/Index.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -33,7 +33,7 @@ import thaw.plugins.Hsqldb; import thaw.core.*; -public class Index extends java.util.Observable implements FileList, IndexTreeNode, java.util.Observer { +public class Index extends java.util.Observable implements FileList, LinkList, IndexTreeNode, java.util.Observer { private Hsqldb db; private IndexTree tree; @@ -50,6 +50,7 @@ private int revision = 0; private Vector fileList; + private Vector linkList; private DefaultMutableTreeNode treeNode; @@ -60,11 +61,14 @@ private FCPTransferQuery transfer = null; private java.io.File targetFile = null; + private String author = null; + + public Index(Hsqldb db, FCPQueueManager queueManager, int id, IndexCategory parent, String realName, String displayName, String publicKey, String privateKey, - int revision, + int revision, String author, boolean modifiable) { this.queueManager = queueManager; @@ -85,6 +89,8 @@ this.revision = revision; + this.author = author; + treeNode.setUserObject(this); } @@ -120,21 +126,21 @@ id = 1; } - st = c.prepareStatement("INSERT INTO indexes (id, originalName, displayName, publicKey, privateKey, positionInTree, revision, parent) "+ - "VALUES (?, ?,?,?,?,?,?, ?)"); + st = c.prepareStatement("INSERT INTO indexes (id, originalName, displayName, publicKey, privateKey, author, positionInTree, revision, parent) VALUES (?, ?,?,?,?,?, ?,?, ?)"); st.setInt(1, id); st.setString(2, realName); st.setString(3, displayName); st.setString(4, publicKey); st.setString(5, privateKey); - st.setInt(6, 0); + st.setString(6, author); + st.setInt(7, 0); - st.setInt(7, revision); + st.setInt(8, revision); if(parent.getId() >= 0) - st.setInt(8, parent.getId()); + st.setInt(9, parent.getId()); else - st.setNull(8, Types.INTEGER); + st.setNull(9, Types.INTEGER); st.execute(); @@ -178,16 +184,18 @@ public void delete() { try { loadFiles(null, false); + loadLinks(null, false); - for(Iterator fileIt = fileList.iterator(); - fileIt.hasNext(); ) { + for(Iterator fileIt = fileList.iterator(); fileIt.hasNext(); ) { thaw.plugins.index.File file = (thaw.plugins.index.File)fileIt.next(); file.delete(); } + for (Iterator linkIt = linkList.iterator(); linkIt.hasNext() ;) { + Link link = (Link)linkIt.next(); + link.delete(); + } - /* TODO : Delete links */ - Connection c = db.getConnection(); PreparedStatement st = c.prepareStatement("DELETE FROM indexes WHERE id = ?"); st.setInt(1, id); @@ -244,7 +252,14 @@ } public void purgeLinkList() { - /* TODO */ + try { + Connection c = db.getConnection(); + PreparedStatement st = c.prepareStatement("DELETE FROM links WHERE indexParent = ?"); + st.setInt(1, getId()); + st.execute(); + } catch(SQLException e) { + Logger.warning(this, "Unable to purge da list ! Exception: "+e.toString()); + } } public void purgeFileList() { @@ -261,7 +276,7 @@ public void save() { try { Connection c = db.getConnection(); - PreparedStatement st = c.prepareStatement("UPDATE indexes SET originalName = ?, displayName = ?, publicKey = ?, privateKey = ?, positionInTree = ?, revision = ?, parent = ? WHERE id = ?"); + PreparedStatement st = c.prepareStatement("UPDATE indexes SET originalName = ?, displayName = ?, publicKey = ?, privateKey = ?, positionInTree = ?, revision = ?, parent = ? , author = ? WHERE id = ?"); st.setString(1, realName); st.setString(2, displayName); @@ -278,8 +293,10 @@ st.setNull(7, Types.INTEGER); else st.setInt(7, ((IndexTreeNode)treeNode.getParent()).getId()); + + st.setString(8, author); - st.setInt(8, getId()); + st.setInt(9, getId()); st.execute(); } catch(SQLException e) { @@ -339,6 +356,10 @@ Logger.info(this, "Updating index ..."); + publicKey = transfer.getFileKey(); + + Logger.info(this, "Most up-to-date key found: " + publicKey); + loadXML(file); save(); @@ -456,14 +477,114 @@ * Do the update all the files in the database. */ public void updateFileList() { - for(Iterator it = fileList.iterator(); - it.hasNext();) { + for(Iterator it = fileList.iterator(); it.hasNext();) { thaw.plugins.index.File file = (thaw.plugins.index.File)it.next(); file.update(); } } + //// LINKS //// + + public void addLink(Link link) { + link.setParent(this); + link.insert(); + + if (linkList != null) { + linkList.add(link); + + setChanged(); + notifyObservers(link); + } + } + + public void removeLink(Link link) { + link.delete(); + + if (linkList != null) { + linkList.remove(link); + setChanged(); + notifyObservers(link); + } + } + + /** + * Update all the links in the database. + */ + public void updateLinkList() { + for(Iterator it = linkList.iterator(); it.hasNext();) { + Link link = (Link)it.next(); + link.update(); + } + } + + public void loadLinks(String columnToSort, boolean asc) + { + if(linkList != null) + return; + + linkList = new Vector(); + + try { + String query = "SELECT id, publicKey, mark, comment, indexTarget FROM links WHERE indexParent = ?"; + + if(columnToSort != null) { + query = query + "ORDER BY " + columnToSort; + + if(!asc) + query = query + " DESC"; + } + + PreparedStatement st = db.getConnection().prepareStatement(query); + + st.setInt(1, getId()); + + if(st.execute()) { + ResultSet results = st.getResultSet(); + + while(results.next()) { + String[] split = publicKey.split("/"); + try { + String indexName = split[split.length-2]; + Link link = new Link(db, indexName, publicKey, this); + linkList.add(link); + } catch(Exception e) { + Logger.warning(this, "Unable to add index '"+publicKey+"' to the list because: "+e.toString()); + } + } + } + + + } catch(java.sql.SQLException e) { + Logger.warning(this, "Unable to get the link list for index: '"+toString()+"' because: "+e.toString()); + } + + } + + /* Returns a copy ! */ + public Vector getLinkList() + { + Vector newList = new Vector(); + + for(Iterator it = linkList.iterator(); + it.hasNext();) { + newList.add(it.next()); + } + + return newList; + } + + public Link getLink(int index) + { + return (Link)linkList.get(index); + } + + public void unloadLinks() + { + linkList = null; + } + + //// XML //// public void generateXML(java.io.File file) { @@ -524,7 +645,7 @@ return; } - serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); + serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT,"yes"); /* final step */ @@ -543,9 +664,14 @@ Text titleText = xmlDoc.createTextNode(toString()); title.appendChild(titleText); - /* TODO : Allow to change username + email */ Element owner = xmlDoc.createElement("owner"); - Text ownerText = xmlDoc.createTextNode("Another anonymous"); + Text ownerText; + + if (author == null) + ownerText = xmlDoc.createTextNode("Another anonymous"); + else + ownerText = xmlDoc.createTextNode(author); + owner.appendChild(ownerText); header.appendChild(title); @@ -617,8 +743,10 @@ Element header = (Element)rootEl.getElementsByTagName("header").item(0); realName = getHeaderElement(header, "title"); - - /* TODO : Author */ + author = getHeaderElement(header, "author"); + + if (author == null) + author = "Another anonymous"; } public String getHeaderElement(Element header, String name) { Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexCategory.java =================================================================== --- trunk/apps/Thaw/src/thaw/plugins/index/IndexCategory.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/plugins/index/IndexCategory.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -243,7 +243,7 @@ String query; - query = "SELECT id, originalName, displayName, publicKey, privateKey, positionInTree, revision FROM indexes"; + query = "SELECT id, originalName, displayName, publicKey, privateKey, author, positionInTree, revision FROM indexes"; if(id < 0) query = query + " WHERE parent IS NULL"; @@ -278,9 +278,12 @@ int revision = result.getInt("revision"); + String author = result.getString("author"); + set(children, position, (new Index(db, queueManager, id, this, realName, displayName, publicKey, privateKey, revision, + author, modifiables)).getTreeNode()); } } catch (java.sql.SQLException e) { Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java =================================================================== --- trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -271,7 +271,7 @@ IndexCategory parent = (IndexCategory)selectedNode; - Index index = new Index(db, queueManager, -2, parent, name, name, publicKey, null, 0, modifiables); + Index index = new Index(db, queueManager, -2, parent, name, name, publicKey, null, 0, null, modifiables); if(modifiables) index.generateKeys(queueManager); Modified: trunk/apps/Thaw/src/thaw/plugins/index/Link.java =================================================================== --- trunk/apps/Thaw/src/thaw/plugins/index/Link.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/plugins/index/Link.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -2,28 +2,126 @@ import java.sql.*; -import thaw.fcp.*; +import thaw.core.Logger; import thaw.plugins.Hsqldb; public class Link extends java.util.Observable { + private int id; - String indexName = null; - String key = null; + private String indexName = null; + private String key = null; - public Link() { + private Index parent = null; - } + private Hsqldb db; - public Link(String indexName, String key) { + + public Link(Hsqldb hsqldb, String indexName, String key, Index parent) { this.indexName = indexName; this.key = key; + this.parent = parent; } + public String getKey() { + return key; + } + + public void setParent(Index index) { + this.parent = index; + } + + public Index getParent() { + return parent; + } + public String getIndexName() { return indexName; } - public String getKey() { + public void setIndexKey(String key) { + this.key = key; + + setChanged(); + notifyObservers(); + } + + public String getIndexKey() { return key; } + + public void insert() { + try { + PreparedStatement st; + + st = db.getConnection().prepareStatement("SELECT id FROM links ORDER BY id DESC LIMIT 1"); + + try { + if(st.execute()) { + ResultSet result = st.getResultSet(); + result.next(); + id = result.getInt("id")+1; + } else + id = 1; + } catch(SQLException e) { + id = 1; + } + + + st = db.getConnection().prepareStatement("INSERT INTO links (id, publicKey, "+ + "mark, comment, indexParent, indexTarget) "+ + "VALUES (?, ?, ?, ?, ?, ?)"); + st.setInt(1, id); + + if(key != null) + st.setString(2, key); + else + st.setString(2, indexName); + + st.setInt(3, 0); + st.setString(4, "No comment"); + st.setInt(5, parent.getId()); + st.setNull(6, Types.INTEGER); + + st.execute(); + } catch(SQLException e) { + Logger.error(this, "Unable to insert link to '"+indexName+"' because: "+e.toString()); + } + + + } + + public void delete() { + try { + PreparedStatement st; + + st = db.getConnection().prepareStatement("DELETE FROM links WHERE id = ?"); + st.setInt(1, id); + + st.execute(); + + } catch(SQLException e) { + Logger.error(this, "Unable to remove link to '"+indexName+"' because: "+e.toString()); + } + } + + public void update() { + try { + PreparedStatement st; + + st = db.getConnection().prepareStatement("UPDATE links SET publicKey = ?, indexParent = ? WHERE id = ?"); + + if(key != null) + st.setString(1, key); + else + st.setString(1, indexName); + + st.setInt(2, getParent().getId()); + + st.setInt(3, id); + + st.execute(); + } catch(SQLException e) { + Logger.error(this, "Unable to update link to '"+indexName+"' because: "+e.toString()); + } + } } Modified: trunk/apps/Thaw/src/thaw/plugins/index/TableCreator.java =================================================================== --- trunk/apps/Thaw/src/thaw/plugins/index/TableCreator.java 2006-09-30 02:46:55 UTC (rev 10585) +++ trunk/apps/Thaw/src/thaw/plugins/index/TableCreator.java 2006-10-01 13:57:36 UTC (rev 10586) @@ -53,15 +53,16 @@ sendQuery(db, "CREATE CACHED TABLE indexes (" - + "id INTEGER IDENTITY NOT NULL," - + "originalName VARCHAR(255) NOT NULL," - + "displayName VARCHAR(255) NULL," - + "publicKey VARCHAR(255) NOT NULL," - + "privateKey VARCHAR(255) NULL," - + "positionInTree INTEGER NOT NULL," - + "revision INTEGER NOT NULL," - + "parent INTEGER NULL," - + "PRIMARY KEY (id)," + + "id INTEGER IDENTITY NOT NULL, " + + "originalName VARCHAR(255) NOT NULL, " + + "displayName VARCHAR(255) NULL, " + + "publicKey VARCHAR(255) NOT NULL, " + + "privateKey VARCHAR(255) NULL, " + + "author VARCHAR(255) NULL, " + + "positionInTree INTEGER NOT NULL, " + + "revision INTEGER NOT NULL, " + + "parent INTEGER NULL, " + + "PRIMARY KEY (id), " + "FOREIGN KEY (parent) REFERENCES indexCategories (id))"); sendQuery(db,
