/*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package de.todesbaum.freemule.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import de.todesbaum.freemule.application.AnnouncementListener;
import de.todesbaum.freemule.application.AnnouncementService;
import de.todesbaum.freemule.application.Client;
import de.todesbaum.freemule.application.ClientListener;
import de.todesbaum.freemule.application.ClientService;
import de.todesbaum.freemule.application.ClipboardListener;
import de.todesbaum.freemule.application.ClipboardService;
import de.todesbaum.freemule.application.DiscoveryListener;
import de.todesbaum.freemule.application.DiscoveryService;
import de.todesbaum.freemule.application.DownloadListener;
import de.todesbaum.freemule.application.DownloadService;
import de.todesbaum.freemule.application.FilelistCollectorListener;
import de.todesbaum.freemule.application.FilelistCollectorService;
import de.todesbaum.freemule.application.FilelistInsertListener;
import de.todesbaum.freemule.application.FilelistInsertService;
import de.todesbaum.freemule.application.FilelistListener;
import de.todesbaum.freemule.application.FilelistService;
import de.todesbaum.freemule.application.FreeMule;
import de.todesbaum.freemule.application.FreenetFile;
import de.todesbaum.freemule.application.Service;
import de.todesbaum.freemule.application.ServiceListener;
import de.todesbaum.freemule.application.ServiceThread;
import de.todesbaum.freemule.application.SharedFile;
import de.todesbaum.freemule.application.SharedFileListener;
import de.todesbaum.freemule.application.SharedFileService;
import de.todesbaum.freemule.application.UploadFile;
import de.todesbaum.freemule.application.UploadListener;
import de.todesbaum.freemule.application.UploadRequest;
import de.todesbaum.freemule.application.UploadRequestDiscoveryListener;
import de.todesbaum.freemule.application.UploadRequestDiscoveryService;
import de.todesbaum.freemule.application.UploadRequestInsertListener;
import de.todesbaum.freemule.application.UploadRequestInsertService;
import de.todesbaum.freemule.application.UploadRequestListener;
import de.todesbaum.freemule.application.UploadRequestService;
import de.todesbaum.freemule.application.UploadService;
import de.todesbaum.freemule.main.Configuration;
import de.todesbaum.freemule.main.LoggingPreferences;
import de.todesbaum.freemule.main.Version;
/**
* @author [email="droden@gmail.com"]David Roden[/email]
* @version $Id:MainFrame.java 221 2006-03-06 14:46:49Z bombe $
*/
public class MainFrame extends JFrame implements ServiceListener, SharedFileListener, FilelistInsertListener, FilelistCollectorListener, DownloadListener, UploadRequestInsertListener, UploadRequestDiscoveryListener, DiscoveryListener, AnnouncementListener, ClientListener, ClipboardListener, FilelistListener, UploadListener, UploadRequestListener {
private Configuration configuration;
private FreeMule freeMule;
private Action connectAction;
private Action disconnectAction;
private Action nodeAction;
private Action quitAction;
private Action preferencesAction;
private Action sharedDirectoriesAction;
private Action aboutAction;
private Action clearMessagesAction;
private Action downloadFilesAction;
private Action uploadFilesAction;
private Action KnownClientsAction;
private Action SearchAction;
private Action SharedFilesAction;
private Action loggingAction;
private LoggingPreferences loggingPreferences = new LoggingPreferences();
protected JList messageList;
protected DefaultListModel messageListModel = new DefaultListModel();
private StatusPanel statusPanel;
private DownloadFilesPanel downloadFilesPanel;
private UploadFilesPanel uploadFilesPanel;
private KnownClientsPanel knownClientsPanel;
private SharedFilesPanel sharedFilesPanel;
private LoggingPreferencesPanel loggingPreferencesPanel;
private SearchPanel searchPanel;
@SuppressWarnings("unused")
private ServicesPanel servicesPanel;
public MainFrame(FreeMule freeMule) {
super("freeMule " + Version.getVersion());
// try {
// // UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
// // UIManager.setLookAndFeel("javax.swing.plaf.multi.MultiLookAndFeel");
// // UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");
// SynthLookAndFeel synthLookAndFeel = new SynthLookAndFeel();
// synthLookAndFeel.load(getClass().getResourceAsStream("/synth-default.xml"), getClass());
// UIManager.setLookAndFeel(synthLookAndFeel);
// // } catch (ClassNotFoundException e) {
// // } catch (InstantiationException e) {
// // } catch (IllegalAccessException e) {
// } catch (UnsupportedLookAndFeelException e) {
// e.printStackTrace();
// } catch (IllegalArgumentException e) {
// e.printStackTrace();
// } catch (ParseException e) {
// e.printStackTrace();
// }
createComponents();
setFreeMule(freeMule);
getContentPane().setLayout(new BorderLayout());
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
actionQuit();
}
});
createActions();
createToolbar();
createContent();
setJMenuBar(createMenuBar());
pack();
setSize(750, 550);
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Freenet");
menuBar.add(fileMenu);
fileMenu.add(connectAction);
fileMenu.add(disconnectAction);
fileMenu.addSeparator();
fileMenu.add(nodeAction);
fileMenu.add(preferencesAction);
fileMenu.add(sharedDirectoriesAction);
fileMenu.addSeparator();
fileMenu.add(quitAction);
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
helpMenu.add(aboutAction);
return menuBar;
}
private void createActions() {
connectAction = new AbstractAction("Connect") {
public void actionPerformed(ActionEvent actionEvent) {
actionConnect();
}
};
connectAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/connect_established.png"));
connectAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
connectAction.putValue(Action.SHORT_DESCRIPTION, "Start all services and connect to Freenet");
disconnectAction = new AbstractAction("Disconnect") {
public void actionPerformed(ActionEvent actionEvent) {
actionDisconnect();
}
};
disconnectAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/connect_no.png"));
disconnectAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK));
disconnectAction.putValue(Action.SHORT_DESCRIPTION, "Stop all services and disconnect from Freenet");
disconnectAction.setEnabled(false);
preferencesAction = new AbstractAction("Preferences") {
public void actionPerformed(ActionEvent actionEvent) {
actionPreferences();
}
};
preferencesAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/kservices.png"));
preferencesAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));
preferencesAction.putValue(Action.SHORT_DESCRIPTION, "Change settings");
sharedDirectoriesAction = new AbstractAction("Shared Directories") {
public void actionPerformed(ActionEvent actionEvent) {
actionSharedDirectories();
}
};
sharedDirectoriesAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/preferences3.png"));
sharedDirectoriesAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
sharedDirectoriesAction.putValue(Action.SHORT_DESCRIPTION, "Configure shared directories");
nodeAction = new AbstractAction("Node") {
public void actionPerformed(ActionEvent actionEvent) {
actionNode();
}
};
nodeAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/node3.png"));
nodeAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
nodeAction.putValue(Action.SHORT_DESCRIPTION, "Configure Freenet node settings");
quitAction = new AbstractAction("Quit") {
public void actionPerformed(ActionEvent actionEvent) {
actionQuit();
}
};
quitAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/exit.png"));
quitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
quitAction.putValue(Action.SHORT_DESCRIPTION, "Quit freeMule");
aboutAction = new AbstractAction("About") {
public void actionPerformed(ActionEvent actionEvent) {
actionAbout();
}
};
aboutAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/about.png"));
aboutAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
aboutAction.putValue(Action.SHORT_DESCRIPTION, "Shows freeMule version information");
clearMessagesAction = new AbstractAction("Clear Messages") {
public void actionPerformed(ActionEvent actionEvent) {
actionClearMessages();
}
};
clearMessagesAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
clearMessagesAction.putValue(Action.SHORT_DESCRIPTION, "Clear the message window");
downloadFilesAction = new AbstractAction("Downloads") {
public void actionPerformed(ActionEvent actionEvent) {
actionDownloadFiles(actionEvent);
}
};
downloadFilesAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/downloads.png"));
//downloadFilesAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));
downloadFilesAction.putValue(Action.SHORT_DESCRIPTION, "Downloads");
uploadFilesAction = new AbstractAction("Uploads") {
public void actionPerformed(ActionEvent actionEvent) {
actionNode();
}
};
uploadFilesAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/uploads.png"));
uploadFilesAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));
uploadFilesAction.putValue(Action.SHORT_DESCRIPTION, "Uploads");
KnownClientsAction = new AbstractAction("Known Clients") {
public void actionPerformed(ActionEvent actionEvent) {
actionNode();
}
};
KnownClientsAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/clients.png"));
KnownClientsAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK));
KnownClientsAction.putValue(Action.SHORT_DESCRIPTION, "Known Clients");
SearchAction = new AbstractAction("Search") {
public void actionPerformed(ActionEvent actionEvent) {
actionNode();
}
};
SearchAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/kview.png"));
SearchAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
SearchAction.putValue(Action.SHORT_DESCRIPTION, "Search");
SharedFilesAction = new AbstractAction("Shared Files") {
public void actionPerformed(ActionEvent actionEvent) {
actionNode();
}
};
SharedFilesAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/sharedfiles32.png"));
SharedFilesAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
SharedFilesAction.putValue(Action.SHORT_DESCRIPTION, "Shared Files");
loggingAction = new AbstractAction("Logging") {
public void actionPerformed(ActionEvent actionEvent) {
actionNode();
}
};
loggingAction.putValue(Action.SMALL_ICON, IconLoader.loadIcon("/img/toggle_log.png"));
loggingAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
loggingAction.putValue(Action.SHORT_DESCRIPTION, "Logging");
}
private void createToolbar() {
JToolBar toolbar = new JToolBar();
toolbar.add(connectAction);
toolbar.add(disconnectAction);
toolbar.addSeparator();
toolbar.add(KnownClientsAction);
toolbar.add(downloadFilesAction);
toolbar.add(uploadFilesAction);
toolbar.add(SearchAction);
toolbar.add(SharedFilesAction);
toolbar.add(loggingAction);
toolbar.add(preferencesAction);
toolbar.add(sharedDirectoriesAction);
toolbar.add(nodeAction);
toolbar.addSeparator();
toolbar.add(quitAction);
getContentPane().add(toolbar, BorderLayout.NORTH);
}
private void createComponents() {
statusPanel = new StatusPanel(this);
downloadFilesPanel = new DownloadFilesPanel(this);
uploadFilesPanel = new UploadFilesPanel(this);
searchPanel = new SearchPanel(this);
knownClientsPanel = new KnownClientsPanel(this);
sharedFilesPanel = new SharedFilesPanel(this);
loggingPreferencesPanel = new LoggingPreferencesPanel(this);
// servicesPanel = new ServicesPanel(this);
messageList = new JList(messageListModel);
}
private void createContent() {
JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
contentPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
// split pane
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true);
splitPane.setDividerLocation(0.5);
contentPanel.add(splitPane, BorderLayout.CENTER);
// top frame: tabbed pane
JTabbedPane topTabbed = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
splitPane.setTopComponent(topTabbed);
// topTabbed.add("Status", statusPanel);
topTabbed.add("Downloads", downloadFilesPanel);
topTabbed.add("Uploads", uploadFilesPanel);
topTabbed.add("Search", searchPanel);
topTabbed.add("Known Clients", knownClientsPanel);
topTabbed.add("Shared Files", sharedFilesPanel);
topTabbed.add("Logging", loggingPreferencesPanel);
// topTabbed.add("Service Threads", servicesPanel);
// bottom frame: messages
splitPane.setBottomComponent(new JScrollPane(messageList));
splitPane.setResizeWeight(0.9);
// input line
JPanel keyInputPanel = new JPanel(new BorderLayout(5, 5));
contentPanel.add(keyInputPanel, BorderLayout.PAGE_END);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
buttonPanel.setBorder(new EmptyBorder(-12, -12, -12, -12));
buttonPanel.add(new JButton(clearMessagesAction));
keyInputPanel.add(buttonPanel, BorderLayout.LINE_END);
getContentPane().add(contentPanel, BorderLayout.CENTER);
}
/**
* @param configuration
* The configuration to set.
*/
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
loggingPreferences = configuration.getLoggingPreferences();
loggingPreferencesPanel.setLoggingPreferences(loggingPreferences);
}
/**
* @param freeMule
* The freeMule to set.
*/
public void setFreeMule(FreeMule freeMule) {
this.freeMule = freeMule;
freeMule.addServiceListener(this);
freeMule.getAnnouncementService().addServiceListener(this);
freeMule.getClientService().addServiceListener(this);
freeMule.getClipboardService().addServiceListener(this);
freeMule.getDiscoveryService().addServiceListener(this);
freeMule.getDownloadService().addServiceListener(this);
freeMule.getEngineService().addServiceListener(this);
freeMule.getFilelistCollectorService().addServiceListener(this);
freeMule.getFilelistInsertService().addServiceListener(this);
freeMule.getFilelistService().addServiceListener(this);
freeMule.getSharedFileService().addServiceListener(this);
freeMule.getUploadRequestDiscoveryService().addServiceListener(this);
freeMule.getUploadRequestInsertService().addServiceListener(this);
freeMule.getUploadService().addServiceListener(this);
freeMule.getAnnouncementService().addAnnouncementListener(this);
freeMule.getClientService().addClientListener(this);
freeMule.getClipboardService().addClipboardListener(this);
freeMule.getDiscoveryService().addDiscoveryListener(this);
freeMule.getDownloadService().addDownloadListener(this);
freeMule.getFilelistCollectorService().addFilelistCollectorListener(this);
freeMule.getFilelistInsertService().addFilelistInsertListener(this);
freeMule.getFilelistService().addFilelistListener(this);
freeMule.getSharedFileService().addSharedFileListener(this);
freeMule.getUploadRequestDiscoveryService().addUploadRequestDiscoveryListener(this);
freeMule.getUploadRequestInsertService().addUploadRequestInsertListener(this);
freeMule.getUploadService().addUploadListener(this);
downloadFilesPanel.setDownloadService(freeMule.getDownloadService());
uploadFilesPanel.setUploadService(freeMule.getUploadService());
searchPanel.setDownloadService(freeMule.getDownloadService());
searchPanel.setFilelistService(freeMule.getFilelistService());
knownClientsPanel.setClientService(freeMule.getClientService());
knownClientsPanel.setFilelistService(freeMule.getFilelistService());
sharedFilesPanel.setSharedFileService(freeMule.getSharedFileService());
// servicesPanel.setServices(freeMule.getServices());
}
public void actionConnect() {
connectAction.setEnabled(false);
//KnownClientsAction.setEnabled(false);
//downloadFilesAction.setEnabled(false);
//uploadFilesAction.setEnabled(false);
//SearchAction.setEnabled(false);
//SharedFilesAction.setEnabled(false);
preferencesAction.setEnabled(false);
sharedDirectoriesAction.setEnabled(false);
nodeAction.setEnabled(false);
freeMule.start();
statusPanel.setFreeMuleStatus("Starting", "#808080");
}
public void actionDisconnect() {
final FreeMule freeMuleCopy = freeMule;
new Thread(new Runnable() {
public void run() {
freeMuleCopy.stop();
}
}).start();
disconnectAction.setEnabled(false);
statusPanel.setFreeMuleStatus("Stopping", "#800000");
}
public void actionPreferences() {
PreferencesDialog dialog = new PreferencesDialog(this);
dialog.setUsername(configuration.getUsername());
dialog.setClientID(configuration.getPublicKey());
dialog.setIncomingDirectory(configuration.getIncomingDirectory());
dialog.setTempDirectory(configuration.getTempDirectory());
dialog.setNetworkName(configuration.getNetworkName());
dialog.setHopsToLive(configuration.getHopsToLive());
dialog.setVisible(true);
configuration.setUsername(dialog.getUsername());
configuration.setIncomingDirectory(dialog.getIncomingDirectory());
configuration.setTempDirectory(dialog.getTempDirectory());
configuration.setNetworkName(dialog.getNetworkName());
configuration.setHopsToLive(dialog.getHopsToLive());
freeMule.setUsername(dialog.getUsername());
freeMule.setIncomingDirectory(dialog.getIncomingDirectory());
freeMule.setTempDirectory(dialog.getTempDirectory());
freeMule.setNetworkName(dialog.getNetworkName());
freeMule.setHopsToLive(dialog.getHopsToLive());
}
protected void actionSharedDirectories() {
SharedDirectoriesDialog dialog = new SharedDirectoriesDialog(this);
dialog.setSharedDirectories(configuration.getSharedDirectories());
dialog.setVisible(true);
configuration.setSharedDirectories(dialog.getSharedDirectories());
freeMule.setSharedDirectories(dialog.getSharedDirectories());
}
public void actionNode() {
NodeDialog nodeDialog = new NodeDialog(this);
nodeDialog.setNodeAddress(configuration.getNodeAddress());
nodeDialog.setNodePort(configuration.getNodePort());
nodeDialog.setVisible(true);
configuration.setNodeAddress(nodeDialog.getNodeAddress());
configuration.setNodePort(nodeDialog.getNodePort());
freeMule.setNodeAddress(nodeDialog.getNodeAddress());
freeMule.setNodePort(nodeDialog.getNodePort());
}
private void actionDownloadFiles(ActionEvent actionEvent){
if(actionEvent.getActionCommand().equals("Downloads")){
JTabbedPane.setSelectedComponent(downloadFilesPanel);
}
}
public void actionQuit() {
if (!connectAction.isEnabled()) {
if (JOptionPane.showConfirmDialog(this, "The freeMule service has not yet stopped.\nDo you really want to exit?", "Exit", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
return;
}
}
configuration.save();
System.exit(0);
}
public void actionAbout() {
JOptionPane.showMessageDialog(this, "<html><big>freeMule " + Version.getVersion() + "</big>
by <a href=\"mailto:droden@gmail.com\">droden@gmail.com</a></html>", "About freeMule", JOptionPane.INFORMATION_MESSAGE);
}
protected void actionClearMessages() {
synchronized (messageListModel) {
messageListModel.clear();
}
}
protected String getCurrentTimestamp() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millis = calendar.get(Calendar.MILLISECOND);
StringBuffer result = new StringBuffer(12);
if (hour < 10) {
result.append('0');
}
result.append(hour).append(':');
if (minute < 10) {
result.append('0');
}
result.append(minute).append(':');
if (second < 10) {
result.append('0');
}
result.append(second).append('.');
if (millis < 100) {
result.append('0');
if (millis < 10) {
result.append('0');
}
}
result.append(millis);
return result.toString();
}
private void addMessageToGUI(final String message) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
synchronized (messageListModel) {
messageListModel.addElement("[" + getCurrentTimestamp() + "] " + message);
messageList.ensureIndexIsVisible(messageListModel.getSize() - 1);
}
}
});
}
private void addMessageToFile(String message) {
String filename = loggingPreferences.getLogfile();
if (filename.equals("stdout")) {
System.out.println(message);
} else if (filename.equals("stderr")) {
System.err.println(message);
} else {
Writer writer = null;
try {
writer = new FileWriter(filename, true);
writer.write(message + "\n");
} catch (IOException e) {
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException ioe1) {
}
}
}
}
@SuppressWarnings("unused")
private String[] parseThrowable(Throwable throwable) {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
PrintStream bytePrinter = new PrintStream(byteOutput);
throwable.printStackTrace(bytePrinter);
bytePrinter.close();
try {
byteOutput.close();
} catch (IOException e) {
}
byte[] throwableBytes = byteOutput.toByteArray();
String throwableText = new String(throwableBytes);
StringTokenizer throwableTokens = new StringTokenizer(throwableText, "\n");
List<String> throwableLines = new ArrayList<String>();
while (throwableTokens.hasMoreTokens()) {
String throwableLine = throwableTokens.nextToken();
throwableLines.add(throwableLine);
}
return throwableLines.toArray(new String[throwableLines.size()]);
}
private void logMessage(String serviceName, String eventName) {
logMessage(serviceName, eventName, null);
}
private void logMessage(String serviceName, String eventName, String message) {
logMessage(serviceName, eventName, message, null);
}
private void logMessage(String serviceName, String eventName, String message, Throwable throwable) {
String eventMessage = serviceName + ": " + eventName;
if (message != null) {
eventMessage += ": " + message;
}
if (throwable != null) {
eventMessage += ": " + throwable.getClass().getName() + ": " + throwable.getMessage();
}
eventMessage += ".";
if (loggingPreferences.getGUILogging(serviceName, eventName)) {
addMessageToGUI(eventMessage);
if (throwable != null) {
throwable.printStackTrace(System.err);
}
}
if (loggingPreferences.getFileLogging(serviceName, eventName)) {
addMessageToFile(eventMessage);
}
}
//
// INTERFACE ServiceListener
//
/**
* {@inheritDoc}
*/
public void serviceStarted(Service service) {
logMessage(service.getName(), "Service Started");
if (service == freeMule) {
statusPanel.setFreeMuleStatus("Running", "#00C000");
disconnectAction.setEnabled(true);
} else if (service == freeMule.getAnnouncementService()) {
statusPanel.setAnnouncementStatus("Idle", "#00C000");
} else if (service == freeMule.getFilelistInsertService()) {
statusPanel.setFilelistInsertStatus("Idle", "#00C000");
} else if (service == freeMule.getUploadRequestInsertService()) {
statusPanel.setUploadRequestInsertStatus("Idle", "#00C000");
}
}
/**
* {@inheritDoc}
*/
public void serviceAborted(Service service, Throwable throwable) {
logMessage(service.getName(), "Service Aborted", "ABNORMAL TERMINATION", throwable);
if (service.equals(freeMule)) {
statusPanel.setFreeMuleStatus("ABORTED", "#FF0000");
disconnectAction.setEnabled(false);
connectAction.setEnabled(true);
//KnownClientsAction.setEnabled(false);
//downloadFilesAction.setEnabled(false);
//uploadFilesAction.setEnabled(false);
//SearchAction.setEnabled(false);
//SharedFilesAction.setEnabled(false);
nodeAction.setEnabled(true);
preferencesAction.setEnabled(true);
sharedDirectoriesAction.setEnabled(true);
} else if (service == freeMule.getAnnouncementService()) {
statusPanel.setAnnouncementStatus("ABORTED", "#FF0000");
} else if (service == freeMule.getFilelistInsertService()) {
statusPanel.setFilelistInsertStatus("ABORTED", "#FF0000");
} else if (service == freeMule.getUploadRequestInsertService()) {
statusPanel.setUploadRequestInsertStatus("ABORTED", "#FF0000");
}
}
/**
* {@inheritDoc}
*/
public void serviceStopped(Service service) {
logMessage(service.getName(), "Service Stopped");
if (service == freeMule) {
statusPanel.setFreeMuleStatus("Stopped", "#000000");
disconnectAction.setEnabled(false);
connectAction.setEnabled(true);
//KnownClientsAction.setEnabled(true);
//downloadFilesAction.setEnabled(true);
//uploadFilesAction.setEnabled(true);
nodeAction.setEnabled(true);
preferencesAction.setEnabled(true);
sharedDirectoriesAction.setEnabled(true);
} else if (service == freeMule.getAnnouncementService()) {
statusPanel.setAnnouncementStatus("Stopped", "#000000");
} else if (service == freeMule.getFilelistInsertService()) {
statusPanel.setFilelistInsertStatus("Stopped", "#000000");
} else if (service == freeMule.getUploadRequestInsertService()) {
statusPanel.setUploadRequestInsertStatus("Stopped", "#000000");
}
}
/**
* {@inheritDoc}
*/
public void serviceThreadStarted(Service service, Thread thread) {
logMessage(service.getName(), "Thread Started", thread.getName());
}
/**
* {@inheritDoc}
*/
public void serviceThreadUpdated(Service service, Thread thread) {
logMessage(service.getName(), "Thread Updated", thread.getName() + ": " + ((ServiceThread) thread).getStatus());
}
/**
* {@inheritDoc}
*/
public void serviceThreadAborted(Service service, Thread thread, Throwable throwable) {
logMessage(service.getName(), "Thread Aborted", "ABNORMAL TERMINATION", throwable);
}
/**
* {@inheritDoc}
*/
public void serviceThreadStopped(Service service, Thread thread) {
logMessage(service.getName(), "Thread Stopped", thread.getName());
}
//
// INTERFACE SharedFileListener
//
/**
* {@inheritDoc}
*/
public void sharedFileLoadingStarted(String filename) {
logMessage(SharedFileService.NAME, "Loading Started");
}
/**
* {@inheritDoc}
*/
public void sharedFileLoadingFailed(String filename, Exception exception) {
logMessage(SharedFileService.NAME, "Loading Failed", null, exception);
}
/**
* {@inheritDoc}
*/
public void sharedFileLoadingComplete(String filename) {
logMessage(SharedFileService.NAME, "Loading Completed");
}
/**
* {@inheritDoc}
*/
public void sharedFileUnsharedFile(String filename) {
logMessage(SharedFileService.NAME, "File Unshared", filename);
}
/**
* {@inheritDoc}
*/
public void sharedFileUnsharedDirectory(String directory) {
logMessage(SharedFileService.NAME, "Directory Unshared", directory);
}
/**
* {@inheritDoc}
*/
public void sharedFileSavingStarted(String filename) {
logMessage(SharedFileService.NAME, "Saving Started", filename);
}
/**
* {@inheritDoc}
*/
public void sharedFileSavingFailed(String filename, Exception exception) {
logMessage(SharedFileService.NAME, "Saving Failed", filename, exception);
}
/**
* {@inheritDoc}
*/
public void sharedFileSavingComplete(String filename) {
logMessage(SharedFileService.NAME, "Saving Completed", filename);
}
/**
* {@inheritDoc}
*/
public void sharedFileScanStarted() {
logMessage(SharedFileService.NAME, "Directories Scan Started");
}
/**
* {@inheritDoc}
*/
public void sharedFileScanFailed(Exception exception) {
logMessage(SharedFileService.NAME, "Directories Scan Failed", null, exception);
}
/**
* {@inheritDoc}
*/
public void sharedFileScanComplete() {
logMessage(SharedFileService.NAME, "Directories Scan Completed");
}
/**
* {@inheritDoc}
*/
public void sharedFileDirectoryScanStarted(String directory) {
logMessage(SharedFileService.NAME, "Directory Scan Started", directory);
}
/**
* {@inheritDoc}
*/
public void sharedFileDirectoryScanFailed(String directory, Exception exception) {
logMessage(SharedFileService.NAME, "Directory Scan Failed", directory, exception);
}
/**
* {@inheritDoc}
*/
public void sharedFileDirectoryScanComplete(String directory) {
logMessage(SharedFileService.NAME, "Directory Scan Completed", directory);
}
/**
* {@inheritDoc}
*/
public void sharedFileHashingStarted(String filename) {
logMessage(SharedFileService.NAME, "Hashing Started", filename);
}
/**
* {@inheritDoc}
*/
public void sharedFileHashingProgress(String filename, int blockIndex, int blockCount) {
logMessage(SharedFileService.NAME, "Hashing Progress", filename + ": " + ((blockIndex + 1) * 100 / blockCount) + "%");
}
/**
* {@inheritDoc}
*/
public void sharedFileHashingFailed(String filename, Exception exception) {
logMessage(SharedFileService.NAME, "Hashing Failed", filename, exception);
}
/**
* {@inheritDoc}
*/
public void sharedFileHashingComplete(String filename, String key, long filelength, long duration) {
double rate = (filelength / 1024.0) / (duration / 1000.0);
double roundedRate = (int) (rate * 10) / 10.0;
logMessage(SharedFileService.NAME, "Hashing Completed", filename + " (" + (duration / 1000) + "s, " + roundedRate + " KiB/s)");
}
/**
* {@inheritDoc}
*/
public void sharedFileListChanged() {
logMessage(SharedFileService.NAME, "List Changed");
}
//
// INTERFACE FilelistCollectorListener
//
/**
* {@inheritDoc}
*/
public void filelistCollectingStarted(Client remoteClient, String key) {
logMessage(FilelistCollectorService.NAME, "Collecting Started", "Client: " + remoteClient.getName() + ", Key: " + key);
}
/**
* {@inheritDoc}
*/
public void filelistCollectingFailed(Client remoteClient, String key, Exception exception) {
logMessage(FilelistCollectorService.NAME, "Collecting Failed", "Client: " + remoteClient.getName() + ", Key: " + key, exception);
}
/**
* {@inheritDoc}
*/
public void filelistCollectingComplete(Client remoteClient, String key, SharedFile[] sharedFiles) {
logMessage(FilelistCollectorService.NAME, "Collecting Completed", "Client: " + remoteClient.getName() + ", Key: " + key + ", " + sharedFiles.length + " Files");
}
//
// INTERFACE FilelistInsertListener
/**
* {@inheritDoc}
*/
public void filelistInsertStarted(String key, int index) {
logMessage(FilelistInsertService.NAME, "Insert Started", "Version " + index);
statusPanel.setFilelistInsertStatus("Inserting version " + index, "#808080");
}
/**
* {@inheritDoc}
*/
public void filelistInsertFailed(String key, int index, Exception exception) {
logMessage(FilelistInsertService.NAME, "Insert Failed", "Version " + index, exception);
statusPanel.setFilelistInsertStatus("Failed", "#FF0000");
}
/**
* {@inheritDoc}
*/
public void filelistInsertComplete(String key, int index) {
logMessage(FilelistInsertService.NAME, "Insert Completed", "Version " + index);
statusPanel.setFilelistInsertStatus("Idle", "#00C000");
}
//
// INTERFACE DownloadListener
//
/**
* {@inheritDoc}
*/
public void downloadAdded(FreenetFile download) {
logMessage(DownloadService.NAME, "Download Added", download.getName());
}
/**
* {@inheritDoc}
*/
public void downloadRemoved(FreenetFile download) {
logMessage(DownloadService.NAME, "Download Removed", download.getName());
}
/**
* {@inheritDoc}
*/
public void downloadStarted(FreenetFile download) {
logMessage(DownloadService.NAME, "Download Started", download.getName());
}
/**
* {@inheritDoc}
*/
public void downloadStopped(FreenetFile download) {
logMessage(DownloadService.NAME, "Download Stopped", download.getName());
}
/**
* {@inheritDoc}
*/
public void downloadCompleting(FreenetFile download) {
logMessage(DownloadService.NAME, "Download Completing", download.getName());
}
/**
* {@inheritDoc}
*/
public void downloadCompletionFailed(FreenetFile download, Exception exception) {
logMessage(DownloadService.NAME, "Download Completion Failed", download.getName(), exception);
}
/**
* {@inheritDoc}
*/
public void downloadComplete(FreenetFile download) {
logMessage(DownloadService.NAME, "Download Completed", download.getName());
}
/**
* {@inheritDoc}
*/
public void manifestDownloadStarted(FreenetFile download) {
logMessage(DownloadService.NAME, "Manifest Download Started", download.getName());
}
/**
* {@inheritDoc}
*/
public void manifestDownloadFailed(FreenetFile download, Exception exception) {
logMessage(DownloadService.NAME, "Manifest Download Failed", download.getName(), exception);
}
/**
* {@inheritDoc}
*/
public void manifestDownloadComplete(FreenetFile download) {
logMessage(DownloadService.NAME, "Manifest Download Completed", download.getName());
}
/**
* {@inheritDoc}
*/
public void blockDownloadStarted(FreenetFile download, int blockNumber) {
logMessage(DownloadService.NAME, "Block Download Started", download.getName() + ", Block " + blockNumber);
}
/**
* {@inheritDoc}
*/
public void blockDownloadFailed(FreenetFile download, int blockNumber, Exception exception) {
logMessage(DownloadService.NAME, "Block Download Failed", download.getName() + ", Block " + blockNumber, exception);
}
/**
* {@inheritDoc}
*/
public void blockDownloadComplete(FreenetFile download, int blockNumber) {
logMessage(DownloadService.NAME, "Block Download Completed", download.getName() + ", Block " + blockNumber);
}
/**
* {@inheritDoc}
*/
public void loadDownloadsStarted(String tempDir) {
logMessage(DownloadService.NAME, "Downloads Loading Started", tempDir);
}
/**
* {@inheritDoc}
*/
public void loadDownloadsFailed(String tempDir, Exception exception) {
logMessage(DownloadService.NAME, "Downloads Loading Failed", tempDir, exception);
}
/**
* {@inheritDoc}
*/
public void loadDownloadsComplete(String tempDir) {
logMessage(DownloadService.NAME, "Downloads Loading Completed", tempDir);
}
/**
* {@inheritDoc}
*/
public void loadDownloadStarted(String filename) {
logMessage(DownloadService.NAME, "Download Loading Started", filename);
}
/**
* {@inheritDoc}
*/
public void loadDownloadFailed(String filename, Exception exception) {
logMessage(DownloadService.NAME, "Download Loading Failed", filename, exception);
}
/**
* {@inheritDoc}
*/
public void loadDownloadComplete(String filename, FreenetFile download) {
logMessage(DownloadService.NAME, "Download Loading Completed", filename + ", " + download.getName());
}
/**
* {@inheritDoc}
*/
public void saveDownloadsStarted(String tempDir) {
logMessage(DownloadService.NAME, "Downloads Saving Started", tempDir);
}
/**
* {@inheritDoc}
*/
public void saveDownloadsFailed(String tempDir, Exception exception) {
logMessage(DownloadService.NAME, "Downloads Saving Failed", tempDir, exception);
}
/**
* {@inheritDoc}
*/
public void saveDownloadsComplete(String tempDir) {
logMessage(DownloadService.NAME, "Downloads Saving Completed", tempDir);
}
/**
* {@inheritDoc}
*/
public void saveDownloadStarted(FreenetFile download) {
logMessage(DownloadService.NAME, "Download Saving Started", download.getName());
}
/**
* {@inheritDoc}
*/
public void saveDownloadFailed(FreenetFile download, Exception exception) {
logMessage(DownloadService.NAME, "Download Saving Failed", download.getName(), exception);
}
/**
* {@inheritDoc}
*/
public void saveDownloadComplete(FreenetFile download) {
logMessage(DownloadService.NAME, "Download Saving Completed", download.getName());
}
/**
* {@inheritDoc}
*/
public void saveBlockStarted(FreenetFile download, int blockNumber) {
logMessage(DownloadService.NAME, "Block Saving Started", download.getName() + ", Block " + blockNumber);
}
/**
* {@inheritDoc}
*/
public void saveBlockFailed(FreenetFile download, int blockNumber, Exception exception) {
logMessage(DownloadService.NAME, "Block Saving Failed", download.getName() + ", Block " + blockNumber, exception);
}
/**
* {@inheritDoc}
*/
public void saveBlockComplete(FreenetFile download, int blockNumber) {
logMessage(DownloadService.NAME, "Block Saving Completed", download.getName() + ", Block " + blockNumber);
}
//
// INTERFACE UploadRequestListener
//
/**
* {@inheritDoc}
*/
public void uploadRequestInsertStarted(String key) {
logMessage(UploadRequestInsertService.NAME, "Upload Request Started", key);
statusPanel.setUploadRequestInsertStatus("Inserting", "#808080");
}
/**
* {@inheritDoc}
*/
public void uploadRequestInsertFailed(String key, Exception exception) {
logMessage(UploadRequestInsertService.NAME, "Upload Request Failed", key, exception);
statusPanel.setUploadRequestInsertStatus("Failed", "#FF0000");
}
/**
* {@inheritDoc}
*/
public void uploadRequestInsertComplete(String key) {
logMessage(UploadRequestInsertService.NAME, "Upload Request Completed", key);
statusPanel.setUploadRequestInsertStatus("Idle", "#00C000");
}
//
// INTERFACE UploadRequestDiscoveryListener
/**
* {@inheritDoc}
*/
public void uploadRequestDiscoveryStarted(String key) {
logMessage(UploadRequestDiscoveryService.NAME, "Discovery Started", key);
}
/**
* {@inheritDoc}
*/
public void uploadRequestDiscoveryFailed(String key, Exception exception) {
logMessage(UploadRequestDiscoveryService.NAME, "Discovery Failed", key, exception);
}
/**
* {@inheritDoc}
*/
public void uploadRequestDiscoveryComplete(String key, UploadRequest uploadRequest) {
logMessage(UploadRequestDiscoveryService.NAME, "Discovery Completed", key + ", Requests: " + uploadRequest.size());
}
//
// INTERFACE DiscoveryListener
//
/**
* {@inheritDoc}
*/
public void discoveryStarted(String key) {
logMessage(DiscoveryService.NAME, "Discovery Started", key);
}
/**
* {@inheritDoc}
*/
public void discoveryFailed(String key, Exception exception) {
logMessage(DiscoveryService.NAME, "Discovery Failed", key, exception);
}
/**
* {@inheritDoc}
*/
public void discoveryComplete(String key, Client remoteClient) {
logMessage(DiscoveryService.NAME, "Discovery Completed", key + ", Client: " + remoteClient.getName() + ", Filelist Index: " + remoteClient.getFilelistIndex() + ", Timestamp: " + remoteClient.getTimestamp());
}
//
// INTERFACE AnnouncementListener
//
/**
* {@inheritDoc}
*/
public void announcementInsertStarted(String key) {
logMessage(AnnouncementService.NAME, "Insert Started", key);
statusPanel.setAnnouncementStatus("Inserting", "#808080");
}
/**
* {@inheritDoc}
*/
public void announcementInsertFailed(String key, Exception exception) {
logMessage(AnnouncementService.NAME, "Insert Failed", key, exception);
statusPanel.setAnnouncementStatus("Failed", "#FF0000");
}
/**
* {@inheritDoc}
*/
public void announcementInsertComplete(String key) {
logMessage(AnnouncementService.NAME, "Insert Completed", key);
statusPanel.setAnnouncementStatus("Idle", "#00C000");
}
//
// INTERFACE ClientListener
//
/**
* {@inheritDoc}
*/
public void clientAdded(Client remoteClient) {
logMessage(ClientService.NAME, "Remote Client Added", remoteClient.getName());
}
/**
* {@inheritDoc}
*/
public void clientUpdated(Client remoteClient) {
logMessage(ClientService.NAME, "Remote Client Updated", remoteClient.getName());
}
/**
* {@inheritDoc}
*/
public void clientRemoved(Client remoteClient) {
logMessage(ClientService.NAME, "Remote Client Removed", remoteClient.getName());
}
//
// INTERFACE ClipboardListener
//
/**
* {@inheritDoc}
*/
public void urlFound(String url) {
logMessage(ClipboardService.NAME, "URL Found", url);
}
//
// INTERFACE FilelistListener
//
/**
* {@inheritDoc}
*/
public void filelistAdded(Client remoteClient, SharedFile[] sharedFiles) {
logMessage(FilelistService.NAME, "Filelist Added", remoteClient.getName() + ", Files: " + sharedFiles.length);
}
/**
* {@inheritDoc}
*/
public void filelistUpdated(Client remoteClient, SharedFile[] sharedFiles) {
logMessage(FilelistService.NAME, "Filelist Updated", remoteClient.getName() + ", Files: " + sharedFiles.length);
}
/**
* {@inheritDoc}
*/
public void filelistRemoved(Client remoteClient) {
logMessage(FilelistService.NAME, "Filelist Removed", remoteClient.getName());
}
//
// INTERFACE UploadListener
//
/**
* {@inheritDoc}
*/
public void uploadAdded(UploadFile uploadFile) {
logMessage(UploadService.NAME, "Upload Added", uploadFile.getName());
}
/**
* {@inheritDoc}
*/
public void uploadUpdated(UploadFile uploadFile) {
logMessage(UploadService.NAME, "Upload Updated", uploadFile.getName());
}
/**
* {@inheritDoc}
*/
public void uploadRemoved(UploadFile uploadFile) {
logMessage(UploadService.NAME, "Upload Removed", uploadFile.getName());
}
/**
* {@inheritDoc}
*/
public void uploadComplete(UploadFile uploadFile) {
logMessage(UploadService.NAME, "Upload Completed", uploadFile.getName());
}
/**
* {@inheritDoc}
*/
public void blockUploadStarted(UploadFile uploadFile, int blockIndex) {
logMessage(UploadService.NAME, "Block Upload Started", uploadFile.getName() + ", Block " + blockIndex);
}
/**
* {@inheritDoc}
*/
public void blockUploadFailed(UploadFile uploadFile, int blockIndex, Exception exception) {
logMessage(UploadService.NAME, "Block Upload Failed", uploadFile.getName() + ", Block " + blockIndex, exception);
}
/**
* {@inheritDoc}
*/
public void blockUploadComplete(UploadFile uploadFile, int blockIndex) {
logMessage(UploadService.NAME, "Block Upload Completed", uploadFile.getName() + ", Block " + blockIndex);
}
/**
* {@inheritDoc}
*/
public void manifestUploadStarted(UploadFile uploadFile) {
logMessage(UploadService.NAME, "Manifest Upload Started", uploadFile.getName());
}
/**
* {@inheritDoc}
*/
public void manifestUploadFailed(UploadFile uploadFile, Exception exception) {
logMessage(UploadService.NAME, "Manifest Upload Failed", uploadFile.getName(), exception);
}
/**
* {@inheritDoc}
*/
public void manifestUploadComplete(UploadFile uploadFile) {
logMessage(UploadService.NAME, "Manifest Upload Completed", uploadFile.getName());
}
//
// INTERFACE UploadRequestListener
//
/**
* {@inheritDoc}
*/
public void uploadRequestAdded(Client remoteClient, UploadRequest uploadRequest) {
logMessage(UploadRequestService.NAME, "Request Added", remoteClient.getName() + ", Files: " + uploadRequest.size());
}
/**
* {@inheritDoc}
*/
public void uploadRequestUpdated(Client remoteClient, UploadRequest uploadRequest) {
logMessage(UploadRequestService.NAME, "Request Updated", remoteClient.getName() + ", Files: " + uploadRequest.size());
}
/**
* {@inheritDoc}
*/
public void uploadRequestRemoved(Client remoteClient, UploadRequest uploadRequest) {
logMessage(UploadRequestService.NAME, "Request Removed", remoteClient.getName() + ", Files: " + uploadRequest.size());
}
}