diff --git a/cs654/proj1/KaZaClient.java b/cs654/proj1/KaZaClient.java index 22bc184..0e0c2f7 100644 --- a/cs654/proj1/KaZaClient.java +++ b/cs654/proj1/KaZaClient.java @@ -56,6 +56,14 @@ public class KaZaClient m_clientDownloaders = new HashMap(); } + public boolean isDownloadActive(int index) + { + synchronized (m_clientDownloaders) + { + return m_clientDownloaders.containsKey(index); + } + } + public boolean connected() { return m_connected; } public void close() @@ -146,7 +154,7 @@ public class KaZaClient } } - public void downloadFile(String host, String fileName) + public int downloadFile(String host, String fileName) { ClientDownloader cd = new ClientDownloader(host, fileName); synchronized (m_clientDownloaders) @@ -155,6 +163,7 @@ public class KaZaClient } Thread t = new Thread(cd); t.start(); + return cd.getIndex(); } public synchronized int getTransferIndex() diff --git a/cs654/proj1/KaZaGUI.java b/cs654/proj1/KaZaGUI.java index f9b4771..33f3b91 100644 --- a/cs654/proj1/KaZaGUI.java +++ b/cs654/proj1/KaZaGUI.java @@ -11,6 +11,7 @@ public class KaZaGUI extends JFrame private KaZaServer m_server; private Thread m_serverThread; private javax.swing.Timer m_timer; + private HashMap m_activeDownloads; /* client tab components */ private JButton m_browseButton; @@ -29,6 +30,7 @@ public class KaZaGUI extends JFrame /* transfer tab components */ private JPanel m_transfersPanel; + private JLabel m_transfersLabel; /* server tab components */ private JLabel m_serverClientsLabel; @@ -58,6 +60,8 @@ public class KaZaGUI extends JFrame JPanel serverPanel = getServerPanel(); pane.add("Server", serverPanel); + m_activeDownloads = new HashMap(); + m_timer = new javax.swing.Timer(1000, m_handler); m_timer.start(); } @@ -140,6 +144,18 @@ public class KaZaGUI extends JFrame } m_serverClientsLabel.setText(caption); } + + Set indices = m_activeDownloads.keySet(); + for (Integer i : indices) + { + if (!m_client.isDownloadActive(i)) + { + KaZaClient.SearchResult sr = m_activeDownloads.get(i); + m_transfersLabel.setText(m_transfersLabel.getText() + "\n" + + "Completed downloading \"" + sr.fileName + "\""); + m_activeDownloads.remove(i); + } + } } } } @@ -169,7 +185,16 @@ public class KaZaGUI extends JFrame { if (e.getClickCount() == 2) { - int index = m_searchResultsList.locationToIndex(e.getPoint()); + KaZaClient.SearchResult sr = (KaZaClient.SearchResult) + m_searchResultsList.getSelectedValue(); + m_activeDownloads.put(m_client.downloadFile(sr.peerAddress, + sr.fileName), + sr); + String curText = m_transfersLabel.getText(); + m_transfersLabel.setText(curText + + (curText.equals("") ? "" : "\n") + + "Started download of \"" + sr.fileName + "\" from " + + sr.userName + " [" + sr.peerAddress + "]"); } } }; @@ -283,6 +308,7 @@ public class KaZaGUI extends JFrame private JPanel getTransferPanel() { m_transfersPanel = new JPanel(); + m_transfersLabel = new JLabel(); JPanel transferPanel = new JPanel(); transferPanel.setLayout(new BoxLayout(transferPanel, BoxLayout.Y_AXIS)); @@ -293,6 +319,7 @@ public class KaZaGUI extends JFrame new Dimension(Short.MAX_VALUE, 0), new Dimension(Short.MAX_VALUE, Short.MAX_VALUE))); transferPanel.add(p); + m_transfersPanel.add(m_transfersLabel); JScrollPane jsp = new JScrollPane(m_transfersPanel); jsp.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); transferPanel.add(jsp);