Skip to content

Commit

Permalink
Added the identity menu. Better publishing of large files. Performanc…
Browse files Browse the repository at this point in the history
…e improvements.
  • Loading branch information
itsMatoosh committed Jan 15, 2019
1 parent 53aaaa5 commit a55363b
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ allprojects {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
group 'me.matoosh.undernet'
version '0.3'
version '0.3.1'
}
subprojects {
repositories {
Expand Down
4 changes: 4 additions & 0 deletions shared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ dependencies {
implementation group: 'org.apache.commons', name: 'commons-crypto', version: '1.0.0'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.45'
}

jar {
baseName = 'UnderNet-base'
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public ResourceTransferHandler(Resource resource, ResourceTransferType transferT
router.resourceManager.inboundHandlers.add(this);
}

setLastMessageTime(System.currentTimeMillis() - 10000);

EventManager.callEvent(new ResourceTransferStartedEvent(this));
}

Expand Down Expand Up @@ -113,7 +115,7 @@ public void close() {
* @param dataMessage
*/
public void callDataReceived(ResourceDataMessage dataMessage) {
lastMessageTime = System.currentTimeMillis();
setLastMessageTime(System.currentTimeMillis());
onDataReceived(dataMessage);
}

Expand Down
7 changes: 7 additions & 0 deletions standalone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ distZip.shouldRunAfter(build)
jar {
// Keep jar clean:
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
baseName = 'UnderNet'

manifest {
attributes 'Main-Class': 'me.matoosh.undernet.standalone.UnderNetStandalone',
Expand All @@ -25,4 +26,10 @@ jar {
// How-to add class path:
// http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle
// https://gist.github.com/simon04/6865179
}
distZip {
baseName = 'UnderNet'
}
distTar {
baseName = 'UnderNet'
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.matoosh.undernet.UnderNet;
import me.matoosh.undernet.file.StandaloneFileManager;
import me.matoosh.undernet.identity.NetworkIdentity;
import me.matoosh.undernet.p2p.router.InterfaceStatus;
import me.matoosh.undernet.standalone.config.StandaloneConfig;
import me.matoosh.undernet.standalone.config.StandaloneConfigManager;
import me.matoosh.undernet.standalone.serialization.SerializationTools;
Expand Down Expand Up @@ -139,6 +140,10 @@ private static void setup() {
* @param identity
*/
public static void setNetworkIdentity(NetworkIdentity identity, File identityFile) {
if(UnderNet.router.status != InterfaceStatus.STOPPED) {
logger.warn("Can't change identity while the router is running!");
return;
}
if(identity == null || identityFile == null || !identity.isCorrect()) {
logger.warn("Network Identity incorrect, creating a new identity!");
identity = new NetworkIdentity();
Expand All @@ -149,6 +154,9 @@ public static void setNetworkIdentity(NetworkIdentity identity, File identityFil
//Setting the identity.
logger.info("Setting the current UnderNet identity to: {}", identity.getNetworkId().getStringValue());
UnderNetStandalone.networkIdentity = identity;
if(MainFrame.instance != null && MainFrame.instance.frame != null) {
MainFrame.instance.frame.repaint();
}

//Save the changed identity.
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
import me.matoosh.undernet.event.resource.transfer.ResourceTransferDataSentEvent;
import me.matoosh.undernet.event.resource.transfer.ResourceTransferFinishedEvent;
import me.matoosh.undernet.event.router.RouterStatusEvent;
import me.matoosh.undernet.identity.NetworkIdentity;
import me.matoosh.undernet.p2p.router.InterfaceStatus;
import me.matoosh.undernet.p2p.router.data.resource.transfer.FileTransferHandler;
import me.matoosh.undernet.p2p.router.data.resource.transfer.ResourceTransferHandler;
import me.matoosh.undernet.standalone.UnderNetStandalone;
import me.matoosh.undernet.standalone.serialization.SerializationTools;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;
import java.util.ResourceBundle;

public class MainFrame extends EventHandler {
Expand Down Expand Up @@ -48,9 +53,20 @@ public class MainFrame extends EventHandler {
private VisualPanel visualPanel;
private TransferPanel transferPanel;

//top menu
JMenuBar menuBar;
JMenu identityMenu;
JMenuItem newIdentityOption;
JMenuItem changeIdentityOption;

public static final int START_HEIGHT = 600;
public static final int START_WIDTH = 950;

/**
* Whether the app is running on mac.
*/
public static boolean IS_MAC;

public MainFrame() {
$$$setupUI$$$();
mainButton.addActionListener(e -> onMainButtonClicked());
Expand All @@ -69,13 +85,38 @@ public static void newInstance() {
instance.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
instance.initialize();
instance.frame.pack();
instance.frame.setLocationRelativeTo(null);
instance.frame.setSize(START_WIDTH, START_HEIGHT);

instance.frame.setVisible(true);
}

private void initialize() {
//Check if running on a mac.
String lcOSName = System.getProperty("os.name").toLowerCase();
IS_MAC = lcOSName.startsWith("mac os x");

setLook();
registerListener();
addMenus();
}

private void setLook() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
if (IS_MAC) {
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "UnderNet");
System.setProperty("apple.laf.useScreenMenuBar", "true");
}
}

private void registerListener() {
Expand All @@ -85,6 +126,60 @@ private void registerListener() {
EventManager.registerHandler(this, ResourceTransferFinishedEvent.class);
}

private void addMenus() {
menuBar = new JMenuBar();
identityMenu = new JMenu(ResourceBundle.getBundle("language").getString("menu_identity"));
newIdentityOption = new JMenuItem(ResourceBundle.getBundle("language").getString("menu_identity_new"));
newIdentityOption.addActionListener(e -> newIdentityPressed());
changeIdentityOption = new JMenuItem(ResourceBundle.getBundle("language").getString("menu_identity_change"));
changeIdentityOption.addActionListener(e -> changeIdentityPressed());
identityMenu.add(newIdentityOption);
identityMenu.add(changeIdentityOption);
menuBar.add(identityMenu);
frame.setJMenuBar(menuBar);
}

private void newIdentityPressed() {
NetworkIdentity identity = new NetworkIdentity();

//Open file choose dialog
File saveFile = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("Identity Files", "id", "identity");
fileChooser.setFileFilter(filter);
int result = fileChooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
saveFile = fileChooser.getSelectedFile();
if (FilenameUtils.getExtension(saveFile.getPath()) != ".id") {
saveFile = new File(saveFile.getPath() + ".id");
}
}

if (saveFile == null) return;

SerializationTools.writeObjectToFile(identity, saveFile);
UnderNetStandalone.setNetworkIdentity(identity, saveFile);
}

private void changeIdentityPressed() {
//Open file choose dialog
File openFile = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("Identity Files", "id", "identity");
fileChooser.setFileFilter(filter);
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
openFile = fileChooser.getSelectedFile();
}

if (openFile == null || !openFile.exists()) return;

NetworkIdentity identity = (NetworkIdentity) SerializationTools.readObjectFromFile(openFile);
UnderNetStandalone.setNetworkIdentity(identity, openFile);
}

/**
* Called when the main button is clicked.
*/
Expand All @@ -104,18 +199,22 @@ public void onEventCalled(Event e) {
switch (statusEvent.newStatus) {
case STOPPED:
mainButton.setEnabled(true);
identityMenu.setEnabled(true);
mainButton.setText(ResourceBundle.getBundle("language").getString("button_connect"));
this.frame.repaint();
break;
case STARTED:
instance.mainButton.setEnabled(true);
instance.identityMenu.setEnabled(false);
instance.mainButton.setText(ResourceBundle.getBundle("language").getString("button_disconnect"));
break;
case STOPPING:
mainButton.setEnabled(false);
identityMenu.setEnabled(false);
break;
case STARTING:
mainButton.setEnabled(false);
identityMenu.setEnabled(false);
new Thread(() -> drawLoop()).start();
break;
}
Expand Down Expand Up @@ -220,4 +319,5 @@ private void createUIComponents() {
public JComponent $$$getRootComponent$$$() {
return panel;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ private void registerListeners() {
}

private void refreshList() {
resourceList.setListData(UnderNet.router.resourceManager.getStoredFileResources().toArray());
if (resourceList.getModel().getSize() == 0) {
resourceList.setListData(new String[]{ResourceBundle.getBundle("language").getString("string_empty")});
}
new Thread(() -> {
Object[] resources = UnderNet.router.resourceManager.getStoredFileResources().toArray();
EventQueue.invokeLater(() -> resourceList.setListData(resources));
}).start();
}

@Override
Expand Down Expand Up @@ -225,6 +231,7 @@ private void createUIComponents() {
public JComponent $$$getRootComponent$$$() {
return panel;
}

}
/**
* Renders elements within the resource list.
Expand All @@ -248,6 +255,8 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
if (isSelected) {
setBackground(getBackground().darker());
}
} else if (value instanceof String) {
setText((String) value);
} else {
setText("UNKNOWN");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.matoosh.undernet.event.channel.message.MessageReceivedEvent;
import me.matoosh.undernet.p2p.node.Node;
import me.matoosh.undernet.p2p.router.InterfaceStatus;
import me.matoosh.undernet.standalone.UnderNetStandalone;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -72,9 +73,9 @@ private void drawSelfNode(Graphics g) {
g.setColor(Color.BLACK);
g.drawOval(getWidth()/2 - diam/2, getHeight()/2 - diam/2, diam, diam);

if(Node.self.getIdentity() != null) {
if(UnderNetStandalone.networkIdentity != null) {
g.setColor(Color.WHITE);
String identity = Node.self.getIdentity().getNetworkId().getStringValue().substring(0, 12) + "...";
String identity = UnderNetStandalone.networkIdentity.getNetworkId().getStringValue().substring(0, 12) + "...";
g.drawString(identity, getWidth()/2 - g.getFontMetrics().stringWidth(identity) / 2, getHeight()/2 + diam / 2 + g.getFontMetrics().getHeight() + 5);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ResourceBundle;

Expand Down Expand Up @@ -59,21 +62,29 @@ public void keyTyped(KeyEvent e) {
private void onPublish() {
//Button clicked. Checking whether a correct path has been chosen.
if (filePathField.getText() != null && !filePathField.getText().trim().equals("")) {
//Publishing resource on UnderNet.
FileResource fileResource = new FileResource(UnderNet.router, new File(filePathField.getText()));
if (!fileResource.copyToContent()) {
JOptionPane.showMessageDialog(MainFrame.instance.frame, String.format(ResourceBundle.getBundle("language").getString("dialog_publishResource_cantPublishMessage"), fileResource.file), ResourceBundle.getBundle("language").getString("dialog_publishResource_cantPublishTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
UnderNet.router.resourceManager.publish(fileResource);
new Thread(() -> {
//Publishing resource on UnderNet.
FileResource fileResource = new FileResource(UnderNet.router, new File(filePathField.getText()));

//Copying the network to clipboard.
StringSelection stringSelection = new StringSelection(fileResource.getNetworkID().getStringValue());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);

//Copying the network to clipboard.
StringSelection stringSelection = new StringSelection(fileResource.getNetworkID().getStringValue());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
//Showing the published network id.
EventQueue.invokeLater(() -> JOptionPane.showMessageDialog(MainFrame.instance.frame, String.format(ResourceBundle.getBundle("language").getString("dialog_publishResource_publishingMessage"), fileResource.file, fileResource.getNetworkID().getStringValue()), ResourceBundle.getBundle("language").getString("dialog_publishResource_publishingTitle"), JOptionPane.INFORMATION_MESSAGE));

//Showing the published network id.
JOptionPane.showMessageDialog(MainFrame.instance.frame, String.format(ResourceBundle.getBundle("language").getString("dialog_publishResource_publishingMessage"), fileResource.file, fileResource.getNetworkID().getStringValue()), ResourceBundle.getBundle("language").getString("dialog_publishResource_publishingTitle"), JOptionPane.INFORMATION_MESSAGE);
if (!fileResource.copyToContent()) {
EventQueue.invokeLater(() -> JOptionPane.showMessageDialog(MainFrame.instance.frame, String.format(ResourceBundle.getBundle("language").getString("dialog_publishResource_cantPublishMessage"), fileResource.file), ResourceBundle.getBundle("language").getString("dialog_publishResource_cantPublishTitle"), JOptionPane.ERROR_MESSAGE));
return;
}
try {
UnderNet.router.resourceManager.publish(fileResource);
} catch (Exception e) {
EventQueue.invokeLater(() -> JOptionPane.showMessageDialog(MainFrame.instance.frame, String.format(ResourceBundle.getBundle("language").getString("dialog_publishResource_cantPublishMessage"), fileResource.file), ResourceBundle.getBundle("language").getString("dialog_publishResource_cantPublishTitle"), JOptionPane.ERROR_MESSAGE));
return;
}
}).start();

dispose();
} else {
Expand Down
9 changes: 7 additions & 2 deletions standalone/src/main/resources/language.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ dialog_publishResource_publishingTitle=Publishing resource
dialog_publishResource_publishingMessage=Publishing %s\nNetwork ID: %s\nThe Network ID has been copied to your clipboard.
dialog_publishResource_cantPublishTitle=Can't publish resource!
dialog_publishResource_cantPublishMessage=There was a problem accessing file:\n%s.
dialog_publishResource_chooseFile=Choose...
dialog_publishResource_chooseFile=Choose
dialog_publishResource_file=File:
dialog_pullResource_transferFinished_title=File Retrieved!
dialog_pullResource_transferFinished_message=Retrieved file %s!\nNetwork id: %s\nSaved to: %s
dialog_pullResource_saveTo=Save to:
dialog_pullResource_networkID=Network ID:
dialog_pullResource_networkID=Network ID:
menu_identity=Identity
menu_identity_new=Create New Identity
menu_identity_change=Change Identity
string_loading=Loading...
string_empty=...
Loading

0 comments on commit a55363b

Please sign in to comment.