From: Eddie Date: Wed, 3 Jun 2015 12:06:53 +0000 (+0100) Subject: Add Registration functionality and tidy up X-Git-Url: https://iam.tj/gitweb/gitweb.cgi?p=WeStealzYourDataz.git;a=commitdiff_plain;h=HEAD Add Registration functionality and tidy up --- diff --git a/WSYD_Members.serialized b/WSYD_Members.serialized deleted file mode 100644 index 553c40c..0000000 Binary files a/WSYD_Members.serialized and /dev/null differ diff --git a/nbproject/project.properties b/nbproject/project.properties index d0bb991..9cc87a4 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -5,7 +5,7 @@ annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output application.desc=Social Networking and Chat client and server application.title=WSYD -application.vendor=TJ +application.vendor=Eddie Berrisford-Lynch auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml build.classes.dir=${build.dir}/classes build.classes.excludes=**/*.java,**/*.form @@ -70,7 +70,7 @@ jnlp.signed=false jnlp.signing= jnlp.signing.alias= jnlp.signing.keystore= -main.class= +main.class=uk.ac.ntu.n0521366.wsyd.Launcher # Optional override of default Codebase manifest attribute, use to prevent RIAs from being repurposed manifest.custom.codebase= # Optional override of default Permissions manifest attribute (supported values: sandbox, all-permissions) diff --git a/src/uk/ac/ntu/n0521366/wsyd/Launcher.java b/src/uk/ac/ntu/n0521366/wsyd/Launcher.java new file mode 100644 index 0000000..03b1293 --- /dev/null +++ b/src/uk/ac/ntu/n0521366/wsyd/Launcher.java @@ -0,0 +1,97 @@ +/* + * The MIT License + * + * Copyright 2015 Eddie Berrisford-Lynch . + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package uk.ac.ntu.n0521366.wsyd; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import uk.ac.ntu.n0521366.wsyd.server.ServerSocial; +import uk.ac.ntu.n0521366.wsyd.management.ServerManagement; +import uk.ac.ntu.n0521366.wsyd.client.ClientGUI; + +/** + * + * @author Eddie Berrisford-Lynch + */ +public class Launcher { + + static class App implements Runnable { + + private final Class _classRef; + private final String[] _args; + + public App(Class classRef, String[] args) { + _classRef = classRef; + _args = args; + } + + @Override + public void run() { + try { + ClassLoader cl = _classRef.getClassLoader(); + try { + Object o = cl.loadClass(_classRef.getName()).newInstance(); + Class newClass = o.getClass(); + Method m = newClass.getMethod("main", String[].class); + m.invoke(null, (Object) this._args); + } catch (ClassNotFoundException ex) { + Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex); + } + } catch (NoSuchMethodException ex) { + Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex); + } catch (IllegalArgumentException ex) { + Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex); + } catch (InvocationTargetException ex) { + Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex); + } + + } + } + static ArrayList apps; + + public static void main(String[] args) { + + apps = new ArrayList<>(); + + apps.add(new App( ServerSocial.class, args)); + apps.add(new App(ServerManagement.class, args)); + for (int qty = 1; qty <= 1; qty++) { + apps.add(new App(ClientGUI.class, args)); + } + + for (App app: apps) { + System.out.println("Starting app " + app._classRef.getTypeName()); + new Thread(app).start(); + } + } + +} diff --git a/src/uk/ac/ntu/n0521366/wsyd/client/ClientGUI.java b/src/uk/ac/ntu/n0521366/wsyd/client/ClientGUI.java index 06daa9c..8fe4601 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/client/ClientGUI.java +++ b/src/uk/ac/ntu/n0521366/wsyd/client/ClientGUI.java @@ -1,7 +1,25 @@ /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. + * The MIT License + * + * Copyright 2015 Eddie Berrisford-Lynch . + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ package uk.ac.ntu.n0521366.wsyd.client; @@ -11,6 +29,7 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.text.MessageFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.SortedMap; import javax.swing.JDialog; @@ -20,9 +39,11 @@ import java.util.TreeSet; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JProgressBar; +import javax.swing.ProgressMonitor; import javax.swing.Timer; import uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member; import uk.ac.ntu.n0521366.wsyd.libs.message.MessageLogin; +import uk.ac.ntu.n0521366.wsyd.libs.message.MessageMember; import uk.ac.ntu.n0521366.wsyd.libs.net.Network; import uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage; import uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessageEvent; @@ -43,18 +64,24 @@ public class ClientGUI extends javax.swing.JFrame implements NetworkMessageEvent private boolean loginSuccessful = false; - private boolean regiSuccessful = false; + private boolean registrationSuccessful = false; + + private final Login loginDialog = new Login(this, true); + private final EditProfile registrationDialog = new EditProfile(this, true); - private Login login = new Login(this, true); + /** + * Member record for this client's user. + */ + private WSYD_Member myMember; Socket socialSocket; /** * Readable/displayable name of this application * - * Will be assigned userID when SocialServer connection established + * TODO: title should be assigned userID when SocialServer connection established */ - private String _title = null; + private String _title = null; /** * Network services to address map. @@ -417,34 +444,32 @@ public class ClientGUI extends javax.swing.JFrame implements NetworkMessageEvent } private void connectionDialog() { - JProgressBar progressBar = new JProgressBar(0,100); - progressBar.setValue(0); - progressBar.setIndeterminate(true); - progressBar.setString("Waiting for Server..."); - progressBar.setStringPainted(true); - System.err.println("Progress Bar created"); - - while (!_serviceToAddressMap.containsService("ServerSocialMC")) { - } + /* FIXME: progress monitor + ProgressMonitor pm = new ProgressMonitor(this, "Waiting for the Server...", "", 0, 10); + pm.setMillisToPopup(100); + pm.setProgress(1); + */ + while (!_serviceToAddressMap.containsService("ServerSocialMC")); + + // pm.setProgress(10); try { socialSocket = new Socket(_serviceToAddressMap.getServiceAddress("ServerSocialMC").getAddress(), Network.PORTS_SERVER_SOCIAL); NetworkStream newStream = new NetworkStream(socialSocket, _tcpStreamManager); _tcpStreamManager.addStream(NetworkStreamManager.SERVERSOCIAL, newStream); + newStream.getEventManager().addNetworkMessageEventListener(this); } catch (IOException ex) { //Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex); System.err.println("IOException in connectionDialog()"); } - //progressBar.setIndeterminate(false); - //progressBar.setValue(100); loginDialog(); } private void loginDialog() { - //WSYD_Login login = new Login(this, true); - login.setLocationRelativeTo(null); - login.setVisible(true); + //WSYD_Login loginDialog = new Login(this, true); + loginDialog.setLocationRelativeTo(null); + loginDialog.setVisible(true); if (loginSuccessful) { this.setLocationRelativeTo(null); @@ -469,40 +494,36 @@ public class ClientGUI extends javax.swing.JFrame implements NetworkMessageEvent result = true; } else { - // TODO: wrap a login message with credentials NetworkMessage message = new NetworkMessage("Login", null, new MessageLogin(uName, pWord)); - _tcpStreamManager._tcpStreams.get(NetworkStreamManager.SERVERSOCIAL).write(message); - // TODO: send login credentials to server for verification - result = true; + NetworkStream ns = _tcpStreamManager._tcpStreams.get(NetworkStreamManager.SERVERSOCIAL); + if (ns != null) { + ns.write(message); + result = true; + } + else + System.err.println(" validateLogin(): do not know where ServerSocial is"); } return result; } - public void regiProfileDialog() + public void registrationProfileDialog() { - EditProfile registration = new EditProfile(this, true); - registration.setLocationRelativeTo(null); - registration.setVisible(true); - if (regiSuccessful) - { - login.dispose(); - this.setLocationRelativeTo(null); - this.setVisible(true); - } + registrationDialog.setLocationRelativeTo(null); + registrationDialog.setVisible(true); } - public boolean confirmRegistration(String uName, String pWord, String bPlace, String cLocation, TreeSet interests, String bio) + public void confirmRegistration(WSYD_Member registrationData) { - System.out.println(uName); - System.out.println(pWord); - System.out.println(bPlace); - System.out.println(cLocation); - - System.out.println(bio); - //TO DO: If server accepts registration return true - regiSuccessful = true; - return true; - //return false; + // TODO: confirmRegistration(): submit registrationDialog data to SocialServer + if (registrationData != null) { + NetworkMessage message = new NetworkMessage("Register", null, new MessageMember(registrationData)); + NetworkStream ns = this._tcpStreamManager._tcpStreams.get(NetworkStreamManager.SERVERSOCIAL); + if (ns != null) { + ns.write(message); + } + else + System.err.println("confirmRegistration(): do not know where SocialServer is"); + } } @@ -579,7 +600,37 @@ public class ClientGUI extends javax.swing.JFrame implements NetworkMessageEvent NetworkMessage nm = event.getNetworkMessage(); if (nm == null) return; - System.err.println(MessageFormat.format("Network Message received with intent {0} from sender {1}", nm.getIntent(), nm.getSender())); + switch (nm.getIntent()) { + case "Login": + MessageLogin ml = (MessageLogin) nm.getMessage(); + if (ml != null) { + this.loginSuccessful = ml._loggedIn; + if (ml._loggedIn) + this.loginDialog.dispose(); + else + loginDialog.setUserName("Invalid Login: retry"); + } + break; + case "Register": + MessageMember mm = (MessageMember) nm.getMessage(); + if (mm != null) { + if (mm.getRegistered() == MessageMember.STATE.REGISTERED) { + this.myMember = mm.getMember(); + this.registrationSuccessful = true; + // dismiss the dialogs and show client's main window + registrationDialog.dispose(); + loginDialog.dispose(); + this.setTitle("WSYD logged in as " + this.myMember._userName); + this.setLocationRelativeTo(null); + this.setVisible(true); + } else + javax.swing.JOptionPane.showMessageDialog(registrationDialog, mm.getStatus(), "Registration failed", javax.swing.JOptionPane.ERROR_MESSAGE); + } + break; + default: + System.err.println(MessageFormat.format("Unhandled NetworkMessage received with intent {0} from sender {1}", nm.getIntent(), nm.getSender())); + + } } } diff --git a/src/uk/ac/ntu/n0521366/wsyd/client/EditProfile.form b/src/uk/ac/ntu/n0521366/wsyd/client/EditProfile.form index c822c42..55dca98 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/client/EditProfile.form +++ b/src/uk/ac/ntu/n0521366/wsyd/client/EditProfile.form @@ -27,6 +27,9 @@ + + + @@ -65,6 +68,9 @@ + + + @@ -90,7 +96,11 @@ - + + + + + @@ -120,9 +130,12 @@ - + - + + + + @@ -130,8 +143,9 @@ - + + @@ -140,7 +154,7 @@ - + @@ -150,6 +164,9 @@ + + + @@ -175,6 +192,9 @@ + + + @@ -197,17 +217,8 @@ - - - - - - - - - - - + + @@ -216,7 +227,10 @@ - + + + + @@ -942,11 +956,11 @@ - - + + - + @@ -998,11 +1012,11 @@ - - + + - + diff --git a/src/uk/ac/ntu/n0521366/wsyd/client/EditProfile.java b/src/uk/ac/ntu/n0521366/wsyd/client/EditProfile.java index de8b655..a30def6 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/client/EditProfile.java +++ b/src/uk/ac/ntu/n0521366/wsyd/client/EditProfile.java @@ -1,6 +1,38 @@ +/* + * The MIT License + * + * Copyright 2015 Eddie Berrisford-Lynch . + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package uk.ac.ntu.n0521366.wsyd.client; +import java.awt.Color; +import java.lang.reflect.Array; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; import java.util.TreeSet; +import javax.swing.DefaultListModel; +import javax.swing.JOptionPane; +import javax.swing.ListModel; +import uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates @@ -21,6 +53,18 @@ public class EditProfile extends javax.swing.JDialog { initComponents(); } + /** + * Populate a model with the applications pre-defined interests. + * + * @return the model + */ + DefaultListModel getInterestsListModel() { + DefaultListModel m = new DefaultListModel<>(); + for (String i : WSYD_Member.Interests) + m.addElement(i); + System.err.println(m.toString()); + return m; + } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -39,8 +83,8 @@ public class EditProfile extends javax.swing.JDialog { gLabelPConfirmation = new javax.swing.JLabel(); gPFieldPConfirmation = new javax.swing.JPasswordField(); gLabelPWarning = new javax.swing.JLabel(); - gLabelBirthplace = new javax.swing.JLabel(); - gFieldBirthplace = new javax.swing.JTextField(); + gLabelBirthDate = new javax.swing.JLabel(); + gFieldBirthDate = new javax.swing.JTextField(); gLabelCurrLocation = new javax.swing.JLabel(); gFieldCurrLocation = new javax.swing.JTextField(); gLabelInterests = new javax.swing.JLabel(); @@ -58,6 +102,7 @@ public class EditProfile extends javax.swing.JDialog { layout.rowHeights = new int[] {0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0}; getContentPane().setLayout(layout); + gLabelUsername.setLabelFor(gFieldUsername); gLabelUsername.setText("Username:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; @@ -84,6 +129,7 @@ public class EditProfile extends javax.swing.JDialog { gridBagConstraints.gridy = 0; getContentPane().add(gButtonConfirmation, gridBagConstraints); + gLabelPassword.setLabelFor(gPFieldPassword); gLabelPassword.setText("Password:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; @@ -100,7 +146,9 @@ public class EditProfile extends javax.swing.JDialog { gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; getContentPane().add(gPFieldPassword, gridBagConstraints); - gLabelPConfirmation.setText("Confirm:"); + gLabelPConfirmation.setLabelFor(gPFieldPConfirmation); + gLabelPConfirmation.setText("Password:"); + gLabelPConfirmation.setToolTipText("Repeat password"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; @@ -121,25 +169,28 @@ public class EditProfile extends javax.swing.JDialog { gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START; getContentPane().add(gLabelPWarning, gridBagConstraints); - gLabelBirthplace.setText("Birthplace:"); + gLabelBirthDate.setLabelFor(gFieldBirthDate); + gLabelBirthDate.setText("Birth Date:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 6; gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END; - getContentPane().add(gLabelBirthplace, gridBagConstraints); + getContentPane().add(gLabelBirthDate, gridBagConstraints); - gFieldBirthplace.setMinimumSize(new java.awt.Dimension(100, 25)); - gFieldBirthplace.setPreferredSize(new java.awt.Dimension(200, 25)); - gFieldBirthplace.addActionListener(new java.awt.event.ActionListener() { + gFieldBirthDate.setText("YYYY-mm-dd"); + gFieldBirthDate.setMinimumSize(new java.awt.Dimension(100, 25)); + gFieldBirthDate.setPreferredSize(new java.awt.Dimension(200, 25)); + gFieldBirthDate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - gFieldBirthplaceActionPerformed(evt); + gFieldBirthDateActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 6; - getContentPane().add(gFieldBirthplace, gridBagConstraints); + getContentPane().add(gFieldBirthDate, gridBagConstraints); + gLabelCurrLocation.setLabelFor(gLabelCurrLocation); gLabelCurrLocation.setText("Current City/Town:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; @@ -154,6 +205,7 @@ public class EditProfile extends javax.swing.JDialog { gridBagConstraints.gridy = 8; getContentPane().add(gFieldCurrLocation, gridBagConstraints); + gLabelInterests.setLabelFor(gListInterests); gLabelInterests.setText("Interests:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; @@ -161,11 +213,7 @@ public class EditProfile extends javax.swing.JDialog { gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END; getContentPane().add(gLabelInterests, gridBagConstraints); - gListInterests.setModel(new javax.swing.AbstractListModel() { - String[] strings = { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8" }; - public int getSize() { return strings.length; } - public Object getElementAt(int i) { return strings[i]; } - }); + gListInterests.setModel(getInterestsListModel()); gListInterests.setToolTipText("Select the interests you have, they will be displayed (Hold CTRL + Click)"); jScrollPane1.setViewportView(gListInterests); @@ -175,7 +223,8 @@ public class EditProfile extends javax.swing.JDialog { gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; getContentPane().add(jScrollPane1, gridBagConstraints); - gLabelBio.setText("Bio:"); + gLabelBio.setLabelFor(gTAreaBio); + gLabelBio.setText("Biography:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 12; @@ -201,25 +250,102 @@ public class EditProfile extends javax.swing.JDialog { pack(); }// //GEN-END:initComponents - private void gFieldBirthplaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gFieldBirthplaceActionPerformed + private void gFieldBirthDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gFieldBirthDateActionPerformed // TODO add your handling code here: - }//GEN-LAST:event_gFieldBirthplaceActionPerformed + }//GEN-LAST:event_gFieldBirthDateActionPerformed + /** + * Verify the user-entered data before attempting to submit it to SocialServer. + * + * Provides error feedback to the user in the form of a message dialog with a list of problems + * and sets the colour of field data to red if an error was found, or green otherwise. + * + * @param evt + */ private void gButtonConfirmationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gButtonConfirmationMouseClicked // TODO add your handling code here: + boolean dataGood = true; // assume user input is OK until errors found + ArrayList errorMessages = new ArrayList<>(); + + String birthdateFormat = "YYYY-mm-dd"; + SimpleDateFormat sd = new SimpleDateFormat(birthdateFormat); + + WSYD_Member m = new WSYD_Member(); + + m._userName = this.gFieldUsername.getText(); + if (m._userName.length() == 0) { + this.gFieldUsername.setForeground(Color.red); + errorMessages.add("Username cannot be empty"); + } + else + this.gFieldUsername.setForeground(Color.green); + + if (Array.getLength(this.gPFieldPassword.getPassword()) == 0 || Array.getLength(this.gPFieldPConfirmation.getPassword()) == 0) { + dataGood = false; + this.gPFieldPassword.setForeground(Color.red); + this.gPFieldPConfirmation.setForeground(Color.red); + errorMessages.add("Password cannot be empty"); + } else if (!Arrays.equals(this.gPFieldPassword.getPassword(), this.gPFieldPConfirmation.getPassword())) { + dataGood = false; + this.gPFieldPassword.setForeground(Color.red); + this.gPFieldPConfirmation.setForeground(Color.red); + errorMessages.add("Passwords do not match"); + } else { + this.gPFieldPassword.setForeground(Color.green); + this.gPFieldPConfirmation.setForeground(Color.green); + m._password = new String(this.gPFieldPassword.getPassword()); + } + + try { + m._birthDate = sd.parse(this.gFieldBirthDate.getText()); + this.gFieldBirthDate.setForeground(Color.green); + } catch(java.text.ParseException e) { + dataGood = false; + this.gFieldBirthDate.setForeground(Color.red); + errorMessages.add("Birth Date format is " + birthdateFormat); + } + + m._bio = this.gTAreaBio.getText(); + m._currentLocation = this.gFieldCurrLocation.getText(); + + java.util.List selectedInterests = this.gListInterests.getSelectedValuesList(); + if (selectedInterests.isEmpty()) { + dataGood = false; + this.gListInterests.setForeground(Color.red); + errorMessages.add("Must select at least 1 interest"); + } else + this.gListInterests.setForeground(Color.green); + + m._interests = new TreeSet<>(); + m._interests.addAll(this.gListInterests.getSelectedValuesList()); + if (!dataGood) { + DefaultListModel errorModel = new DefaultListModel<>(); + for (String s: errorMessages) + errorModel.addElement(s); + javax.swing.JList errorList = new javax.swing.JList(); + errorList.setModel(errorModel); + JOptionPane.showMessageDialog(this, errorList, "Errors in Registration details", JOptionPane.ERROR_MESSAGE); + } + else { + // trigger sending of the data to SocialServer + ((ClientGUI)this.getOwner()).confirmRegistration(m); + } + // FIXME: gButtonConfirmationMouseClicked(): remove superceded code that has been commented out + /* TreeSet interests = null; - /* + for (int i = 0; i < gListInterests.getModel().getSize(); i++) { interests.add(((String)gListInterests.getModel().getElementAt(i))); - }*/ + } if (((ClientGUI)this.getOwner()).confirmRegistration(gFieldUsername.getText(), new String(gPFieldPassword.getPassword()), - gFieldBirthplace.getText(), + gFieldBirthDate.getText(), gFieldCurrLocation.getText(), interests, gTAreaBio.getText())) this.dispose(); else gLabelPWarning.setText("Server failed to register. Retry."); + */ }//GEN-LAST:event_gButtonConfirmationMouseClicked /** @@ -267,11 +393,11 @@ public class EditProfile extends javax.swing.JDialog { // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton gButtonConfirmation; - private javax.swing.JTextField gFieldBirthplace; + private javax.swing.JTextField gFieldBirthDate; private javax.swing.JTextField gFieldCurrLocation; private javax.swing.JTextField gFieldUsername; private javax.swing.JLabel gLabelBio; - private javax.swing.JLabel gLabelBirthplace; + private javax.swing.JLabel gLabelBirthDate; private javax.swing.JLabel gLabelCurrLocation; private javax.swing.JLabel gLabelInterests; private javax.swing.JLabel gLabelPConfirmation; diff --git a/src/uk/ac/ntu/n0521366/wsyd/client/Login.java b/src/uk/ac/ntu/n0521366/wsyd/client/Login.java index db417c1..27779e1 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/client/Login.java +++ b/src/uk/ac/ntu/n0521366/wsyd/client/Login.java @@ -1,7 +1,25 @@ /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. + * The MIT License + * + * Copyright 2015 Eddie Berrisford-Lynch . + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ package uk.ac.ntu.n0521366.wsyd.client; @@ -18,8 +36,13 @@ public class Login extends javax.swing.JDialog { public Login(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); + this.getRootPane().setDefaultButton(gButtonLogin); + + } + public void setUserName(String newName) { + if (newName != null) + gFieldName.setText(newName); } - /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -152,10 +175,14 @@ public class Login extends javax.swing.JDialog { // TODO add your handling code here: }//GEN-LAST:event_gFieldPasswordActionPerformed + /** + * Send the Username Password pair to the server to be validated. + * + * @param evt + */ private void gButtonLoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gButtonLoginMouseClicked // TODO add your handling code here: - if (((ClientGUI)this.getOwner()).validateLogin(gFieldName.getText(), new String(gFieldPassword.getPassword()))); //this.dispose(); - else gFieldName.setText("Invalid Login..."); + ((ClientGUI)this.getOwner()).validateLogin(gFieldName.getText(), new String(gFieldPassword.getPassword())); }//GEN-LAST:event_gButtonLoginMouseClicked private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed @@ -169,7 +196,7 @@ public class Login extends javax.swing.JDialog { private void gButtonRegisterMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gButtonRegisterMouseClicked // TODO add your handling code here: - ((ClientGUI)this.getOwner()).regiProfileDialog(); + ((ClientGUI)this.getOwner()).registrationProfileDialog(); }//GEN-LAST:event_gButtonRegisterMouseClicked /** diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.java b/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.java index 621e147..7bd1acd 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,6 +25,7 @@ package uk.ac.ntu.n0521366.wsyd.libs; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.TreeSet; @@ -44,9 +45,15 @@ import java.util.logging.Level; * WSYD_Member_Comparator_UserID supports sorted Maps with userID as the sort key * WSYD_Member_Comparator_UserName supports sorted Sets and Collections with userName as the sort key * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class WSYD_Member implements java.io.Serializable, java.lang.Comparable { + + /** + * Choices for _interests + */ + public static String[] Interests = {"Computers","Driving","DIY","Gardening","Games","Gym","Music","Reading" }; + public long _userID; public String _userName; public String _password; diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserID.java b/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserID.java index bdcf4bf..e5a9d5b 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserID.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserID.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -32,7 +32,7 @@ import java.io.Serializable; * Implements serializable in order that the Collection using it doesn't cause * deserialize Exceptions due to missing Comparator * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class WSYD_Member_Comparator_UserID implements Comparator, Serializable { /** diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserName.java b/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserName.java index 2c31a06..db526a6 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserName.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserName.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -32,7 +32,7 @@ import java.util.Comparator; * Implements serializable in order that the Collection using it doesn't cause * deserialize Exceptions due to missing Comparator * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class WSYD_Member_Comparator_UserName implements Comparator, Serializable { /** diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/logging/PacketHandler.java b/src/uk/ac/ntu/n0521366/wsyd/libs/logging/PacketHandler.java index b8a3da8..8bd688d 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/logging/PacketHandler.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/logging/PacketHandler.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -40,7 +40,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage; * * @see java.util.logging.Handler * @see uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage - * @author TJ + * @author Eddie Berrisford-Lynch */ public class PacketHandler extends Handler { diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/logging/TableModelHandler.java b/src/uk/ac/ntu/n0521366/wsyd/libs/logging/TableModelHandler.java index 6fea35a..fb48527 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/logging/TableModelHandler.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/logging/TableModelHandler.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -36,7 +36,7 @@ import java.util.logging.Filter; * @see javax.swing.JScrollPane * @see javax.swing.JTable * @see javax.swing.table.DefaultTableModel - * @author TJ + * @author Eddie Berrisford-Lynch */ public class TableModelHandler extends Handler { diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageAbstract.java b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageAbstract.java index 66530d8..54e5b2c 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageAbstract.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageAbstract.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,7 @@ import java.net.InetAddress; /** * Base class of all Message types. * - * @author TJ + * @author Eddie Berrisford-Lynch */ public abstract class MessageAbstract implements Serializable { /** @@ -38,18 +38,5 @@ public abstract class MessageAbstract implements Serializable { * @return E.g. "LogRecord", "FriendOnline", "ChatRequest" */ public abstract String getMessageType(); - - /** - * IP address of the sending host. - * - * This should be set by the receiving service using the value obtained from - * the packet or connection the message arrived in. - * - * FIXME: sourceAddress: is this really needed? the message encapsulated in a NetworkMessage already - */ - public InetAddress sourceAddress; - - public MessageAbstract() { - sourceAddress = null; - } + } diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageLogRecord.java b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageLogRecord.java index 485b879..74cc642 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageLogRecord.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageLogRecord.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,7 @@ import java.util.logging.LogRecord; * A specialised message containing a LogRecord * * @see java.util.logging.LogRecord - * @author TJ + * @author Eddie Berrisford-Lynch */ public class MessageLogRecord extends MessageAbstract { private static final String _type = "LogRecord"; diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageLogin.java b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageLogin.java index 608bc89..6c58f52 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageLogin.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageLogin.java @@ -23,23 +23,58 @@ */ package uk.ac.ntu.n0521366.wsyd.libs.message; +import java.io.Serializable; + /** * Message used to authenticate a login with the SocialServer. * * @author eddie */ -public class MessageLogin extends MessageAbstract { +public class MessageLogin extends MessageAbstract implements Serializable { + /** + * Message type. + */ private static final String _type = "Login"; + /** + * A Username. + */ public final String _uName; + /** + * A password. + */ public final String _uPass; + /** + * After login the user's ID. + */ public long _userID = 0; + /** + * Result of a login request. + */ public boolean _loggedIn = false; + /** + * Make the Message printable. + * + * @return human readable text + */ + public String toString() { + return new String("_uName:" + _uName + + ",_uPass:" + _uPass + + ",_userID:" + _userID + + ",_loggedIn:" + _loggedIn + ); + } + + /** + * The Message Class type. + * + * @return Message type + */ public static String getType() { return _type; } @@ -54,9 +89,14 @@ public class MessageLogin extends MessageAbstract { this._uPass = null; } + /** + * Construct with a Username Password pair. + * + * @param uName Username + * @param uPass Password + */ public MessageLogin (String uName, String uPass) { this._uName = uName; this._uPass = uPass; - } - + } } diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageMember.java b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageMember.java new file mode 100644 index 0000000..c04648c --- /dev/null +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessageMember.java @@ -0,0 +1,110 @@ +/* + * The MIT License + * + * Copyright 2015 TJ . + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package uk.ac.ntu.n0521366.wsyd.libs.message; + +import uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member; + +/** + * A specialised message containing a WSYD_Member. + * + * @author TJ + */ +public class MessageMember extends MessageAbstract { + + public static enum STATE {UNREGISTERED, REGISTERED}; + private static final String _type = "WSYD_Member"; + + private WSYD_Member _member; + private STATE _registered; + private String _status; + + public static String getType() { + return _type; + } + + @Override + public String getMessageType() { + return _type; + } + + public MessageMember(WSYD_Member member, STATE registered, String status) { + _member = member; + _registered = registered; + _status = status; + } + + public MessageMember(WSYD_Member member) { + this(member, STATE.UNREGISTERED, null); + } + + public MessageMember() { + this(null, STATE.UNREGISTERED, null); + } + + /** + * Get the Member contained in this message. + * + * @return + */ + public WSYD_Member getMember() { + return _member; + } + + /** + * Current registration state of the member record contained in this message. + * + * @return + */ + public STATE getRegistered() { + return _registered; + } + + /** + * Update the registration state of the member record contained in this message. + * + * @param registered + */ + public void setRegistered(STATE registered) { + _registered = registered; + } + + /** + * Get registration status message. + * + * @return + */ + public String getStatus() { + return _status; + } + + /** + * Set registration status message. + * + * ServerSocial may set this with text describing why a registration failed. + * @param status + */ + public void setStatus(String status) { + _status = status; + } +} \ No newline at end of file diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessagePresence.java b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessagePresence.java index 2c6143e..0f2cb37 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessagePresence.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/message/MessagePresence.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -27,7 +27,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.net.WSYD_SocketAddress; /** * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class MessagePresence extends MessageAbstract { private static final String _type = "Presence"; diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/Network.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/Network.java index 0f02897..c899501 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/Network.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/Network.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -28,7 +28,7 @@ import java.net.UnknownHostException; /** * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class Network { public static final int PORTS_SERVER_SOCIAL = 50000; diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.java index a513e72..dac7c9b 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -35,7 +35,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.message.MessageAbstract; * Wraps an object and intent label together for passing over a network. * * @see NetworkMessageEvent - * @author TJ + * @author Eddie Berrisford-Lynch */ public class NetworkMessage implements Serializable, Cloneable { /** @@ -64,7 +64,7 @@ public class NetworkMessage implements Serializable, Cloneable { */ long _key; - Class _class; + // Class _class; MessageAbstract _message; /** @@ -75,10 +75,24 @@ public class NetworkMessage implements Serializable, Cloneable { _intent = null; _serviceSender = null; _serviceTarget = null; - _class = null; + // _class = null; _message = null; } + /** + * Make the NetworkMessage printable. + * + * @return human readable text + */ + @Override + public String toString() { + return new String("_serializedLength:" + _serializeLength + + ",_intent:" + _intent + + ",_serviceSender:" + _serviceSender + + ",_serviceTarget:" + _serviceTarget + + ",_message:" + _message.toString() + ); + } /** * Create a message for passing over the network. * @@ -98,7 +112,7 @@ public class NetworkMessage implements Serializable, Cloneable { _serviceSender = null; _serviceTarget = target; _message = message; - _class = _message.getClass(); + // _class = _message.getClass(); } /** diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEvent.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEvent.java index 7aae560..7d36958 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEvent.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEvent.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,7 +30,7 @@ import java.util.EventObject; * * @see NetworkMessageEventGenerator * @see NetworkMessageEventListener - * @author TJ + * @author Eddie Berrisford-Lynch */ public class NetworkMessageEvent extends EventObject { /** diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventGenerator.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventGenerator.java index 66ed1c2..3a7c762 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventGenerator.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventGenerator.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,7 +30,7 @@ package uk.ac.ntu.n0521366.wsyd.libs.net; * @see NetworkMessageEvent * @see NetworkMessageEventListener * @see NetworkMessageEventListenerManager - * @author TJ + * @author Eddie Berrisford-Lynch */ public interface NetworkMessageEventGenerator { /** diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListener.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListener.java index 72508b2..dda2718 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListener.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListener.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,7 @@ package uk.ac.ntu.n0521366.wsyd.libs.net; * @see NetworkMessage * @see NetworkMessageEvent * @see NetworkMessageEventGenerator - * @author TJ + * @author Eddie Berrisford-Lynch */ public interface NetworkMessageEventListener { public void NetworkMessageReceived(NetworkMessageEvent event); diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListenerManager.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListenerManager.java index 7c18a2c..885ced8 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListenerManager.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListenerManager.java @@ -99,7 +99,8 @@ public class NetworkMessageEventListenerManager implements NetworkMessageEventGe public synchronized void fireNetworkMessageEvent(NetworkMessage message) { NetworkMessageEvent event = new NetworkMessageEvent(this, message); for (NetworkMessageEventListenerWithIntent intentListener : _NetworkMessageEventListeners) { - if (intentListener._intent.equals(message._intent) || intentListener._intent == null) + System.err.println("fireNetworkMessageEvent() intent: " + intentListener._intent); + if (intentListener._intent == null || (intentListener._intent != null && intentListener._intent.equals(message._intent))) intentListener._listener.NetworkMessageReceived(event); } } diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.java index 3a866df..9dd6445 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -63,7 +63,7 @@ import javax.swing.SwingWorker; * * @see javax.swing.SwingWorker * - * @author TJ + * @author Eddie Berrisford-Lynch */ public abstract class NetworkServerAbstract extends SwingWorker { diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerTCP.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerTCP.java index e68987f..fc1744b 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerTCP.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerTCP.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -41,7 +41,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.message.MessageNetworkStream; * or in a Swing GUI application as a background worker thread. * * @see NetworkServerAbstract - * @author TJ + * @author Eddie Berrisford-Lynch */ public class NetworkServerTCP extends NetworkServerAbstract { diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerUDP.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerUDP.java index fda95b5..5763d54 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerUDP.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerUDP.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -42,7 +42,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.message.MessageLogRecord; * or in a Swing GUI application as a background worker thread. * * @see NetworkServerAbstract - * @author TJ + * @author Eddie Berrisford-Lynch */ public class NetworkServerUDP extends NetworkServerAbstract { /** diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerUDPMulticast.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerUDPMulticast.java index 4f8bf03..fe008aa 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerUDPMulticast.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerUDPMulticast.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,7 +34,7 @@ import java.util.Enumeration; /** * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class NetworkServerUDPMulticast extends NetworkServerUDP { diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkStream.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkStream.java index 48da1b7..d451e89 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkStream.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkStream.java @@ -89,8 +89,12 @@ public class NetworkStream extends SwingWorker { boolean expectingLong = true; while (!this.isCancelled()) { - // check for incoming message + if (_socketIS.available() > 0) { + System.err.println(" reading Object from stream, bytes available: " + _socketIS.available()); + read(); + } + /* if (_ois.available() >= expectedLength) { if (expectingLong == true) { expectedLength = _ois.readLong(); @@ -105,6 +109,7 @@ public class NetworkStream extends SwingWorker { expectingLong = true; } } + */ // send a queued message NetworkMessage temp = this.sendMessage(); if (temp != null) { @@ -140,12 +145,10 @@ public class NetworkStream extends SwingWorker { try { _oos.writeObject(message); _oos.flush(); - long messageSize = _baos.size(); - _socketOS.writeLong(messageSize); - System.err.println(" bytes to write: " + messageSize); - _baos.writeTo(_socketOS); + Long messageSize = new Long(_baos.size()); + _socketOS.writeObject(message); _socketOS.flush(); - System.err.println("baos.size()=" + _baos.size()); + System.err.println(" bytes written: " + messageSize); result = true; @@ -169,17 +172,17 @@ public class NetworkStream extends SwingWorker { try { NetworkMessage message = null; + String s = null; - try { - message = (NetworkMessage)_ois.readObject(); - } catch (java.io.OptionalDataException ex) { - System.err.println("Length: " + ex.length + " EOF: " + ex.eof); - ex.printStackTrace(); - } + message = (NetworkMessage)_ois.readObject(); + System.err.println(" received: " + message.toString()); message.setKey(_key); publish(message); result = true; + } catch (java.io.OptionalDataException ex) { + System.err.println("Length: " + ex.length + " EOF: " + ex.eof); + ex.printStackTrace(); } catch (IOException ex) { Logger.getLogger(NetworkStream.class.getName()).log(Level.SEVERE, null, ex); // FIXME: Replace logger diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java index 80e825f..5a1e1f6 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -33,7 +33,8 @@ import java.util.logging.Logger; /** * Maps service names on hosts to an InetAddress:Port pair. * - * @author TJ + * @author Eddie Berrisford-Lynch + */ public class ServiceAddressMap { /** diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/WSYD_SocketAddress.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/WSYD_SocketAddress.java index e66d60a..d818632 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/WSYD_SocketAddress.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/WSYD_SocketAddress.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -43,7 +43,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.net.Network; * * Serializable to allow passing between separate local and remote processes * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class WSYD_SocketAddress implements java.io.Serializable { Boolean _multicast; diff --git a/src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.form b/src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.form index 3d3ba16..fd295d9 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.form +++ b/src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.form @@ -28,7 +28,7 @@ - + diff --git a/src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.java b/src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.java index 6f82236..cafcc64 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.java +++ b/src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -56,7 +56,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.net.*; import uk.ac.ntu.n0521366.wsyd.libs.net.ServiceAddressMap.LastSeenHost; /** * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class ServerManagement extends javax.swing.JFrame implements NetworkMessageEventListener, Filter { /** @@ -347,7 +347,7 @@ public class ServerManagement extends javax.swing.JFrame implements NetworkMessa gTextAreaAbout.setEditable(false); gTextAreaAbout.setColumns(20); gTextAreaAbout.setRows(5); - gTextAreaAbout.setText("Server Management client\n© Copyright 2015 TJ \n\n<<< Click on the mug to learn more!\n\nI will disappear in 20 seconds.\n\n"); + gTextAreaAbout.setText("Server Management client\n© Copyright 2015 Eddie Berrisford-Lynch \n\n<<< Click on the mug to learn more!\n\nI will disappear in 20 seconds.\n\n"); gTextAreaAbout.setBorder(null); gDialogAbout.getContentPane().add(gTextAreaAbout); gTextAreaAbout.setBounds(310, 30, 270, 200); diff --git a/src/uk/ac/ntu/n0521366/wsyd/server/ServerSocial.java b/src/uk/ac/ntu/n0521366/wsyd/server/ServerSocial.java index 45c5412..639c482 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/server/ServerSocial.java +++ b/src/uk/ac/ntu/n0521366/wsyd/server/ServerSocial.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2015 TJ . + * Copyright 2015 Eddie Berrisford-Lynch . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -57,6 +57,7 @@ import javax.swing.Timer; import uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member; import uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member_Comparator_UserID; import uk.ac.ntu.n0521366.wsyd.libs.message.MessageLogin; +import uk.ac.ntu.n0521366.wsyd.libs.message.MessageMember; import uk.ac.ntu.n0521366.wsyd.libs.message.MessageMemberState; import uk.ac.ntu.n0521366.wsyd.libs.message.MessagePresence; import uk.ac.ntu.n0521366.wsyd.libs.message.MessageServerControl; @@ -83,7 +84,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.net.ServiceAddressMap.LastSeenHost; * exitRequested and restartRequested. This can be done by an optional * Management GUI application. * - * @author TJ + * @author Eddie Berrisford-Lynch */ public final class ServerSocial implements NetworkMessageEventListener, ConnectionEstablishedEventListener { /** @@ -441,19 +442,47 @@ public final class ServerSocial implements NetworkMessageEventListener, Connecti return result; } + private void notifyMemberPrescence(long userID, boolean state) { + NetworkMessage message = new NetworkMessage("MemberNotification", null, new MessageMemberState(userID, state)); + for (long ID : _membersOnline) { + _tcpStreamManager._tcpStreams.get(ID).write(message); + } + } + + private void memberOnline (long userID) { + if (!_membersOnline.contains(userID)) { + notifyMemberPrescence(userID, true); + _membersOnline.add(userID); + } + } + + private void memberOffline (long userID) { + if (_membersOnline.contains(userID)) { + _membersOnline.remove(userID); + notifyMemberPrescence(userID, false); + } + } + + @Override + public void connectionEstablished(ConnectionEstablishedEvent event) { + System.err.println("connectionEstablished()"); + event.getStream().getEventManager().addNetworkMessageEventListener(this); + } + /** + * Process received network messages. + * + * @param event the network message event + */ @Override public void NetworkMessageReceived(NetworkMessageEvent event) { - System.err.println("NetworkMessage received"); - //TODO: NetworkMessageReceived: Handle Messages NetworkMessage nm = event.getNetworkMessage(); if (nm == null) return; - //Exit or Restart? - System.err.println("Packet Received for intent " + nm.getIntent()); + System.err.println("NetworkMessage received for intent " + nm.getIntent()); String type = nm.getMessage().getMessageType(); switch (nm.getIntent()) { - case "Control": + case "Control": // Exit or Restart? if (type.equals(MessageServerControl.getType())) { // ServerControl if ("ServerManagement".equals(nm.getSender())) { MessageServerControl mp = (MessageServerControl)nm.getMessage(); @@ -464,6 +493,7 @@ public final class ServerSocial implements NetworkMessageEventListener, Connecti break; case "Login": if (type.equals(MessageLogin.getType())) { + NetworkStream ns = _tcpStreamManager._tcpStreams.get(nm.getKey()); MessageLogin ml = (MessageLogin)nm.getMessage(); Set> tempSet = _members.entrySet(); @@ -475,34 +505,54 @@ public final class ServerSocial implements NetworkMessageEventListener, Connecti ml._loggedIn = true; _tcpStreamManager.updateKey(nm.getKey(), element.getKey()); // replace temp key in stream manager _membersOnline.add(element.getKey()); // make the member online + break; + } + } + if (ns != null) + ns.write(nm); + else + System.err.println("Login: cannot find stream for ID:" + nm.getKey()); + } + case "Register": + if (type.equals(MessageMember.getType())) { + NetworkStream ns = _tcpStreamManager._tcpStreams.get(nm.getKey()); + MessageMember mm = (MessageMember)nm.getMessage(); + // assume username can be registered unless username found + mm.setRegistered(MessageMember.STATE.REGISTERED); + + Set> tempSet = _members.entrySet(); + Iterator> tempIter = tempSet.iterator(); + while (tempIter.hasNext()) { + Map.Entry element = tempIter.next(); + if (element.getValue()._userName.equals(mm.getMember()._userName)) { + mm.setRegistered(MessageMember.STATE.UNREGISTERED); + mm.setStatus("Member already registered: " + mm.getMember()._userName); + break; } } - _tcpStreamManager._tcpStreams.get(nm.getKey()).write(nm); + // register the new member + if (mm.getRegistered() == MessageMember.STATE.REGISTERED) { + // get the next unallocated user ID + long newUserID = _members.lastKey().longValue() + 1; + _members.put(newUserID, mm.getMember()); + // update the object so the ID can be returned to the client + mm.getMember()._userID = newUserID; + // update the on-disk membership data + writeMembers(_membersFile); + } + if (ns != null) + ns.write(nm); + else + System.err.println("Login: cannot find stream for ID:" + nm.getKey()); + } + break; + default: + System.err.println("Unhandled NetworkMessage received for intent " + nm.getIntent()); } } - private void notifyMemberPrescence(long userID, boolean state) { - NetworkMessage message = new NetworkMessage("MemberNotification", null, new MessageMemberState(userID, state)); - for (long ID : _membersOnline) { - _tcpStreamManager._tcpStreams.get(ID).write(message); - } - } - - private void memberOnline (long userID) { - if (!_membersOnline.contains(userID)) { - notifyMemberPrescence(userID, true); - _membersOnline.add(userID); - } - } - - private void memberOffline (long userID) { - if (_membersOnline.contains(userID)) { - _membersOnline.remove(userID); - notifyMemberPrescence(userID, false); - } - } /** * Entry point which starts, restarts, and exits the application. @@ -520,12 +570,4 @@ public final class ServerSocial implements NetworkMessageEventListener, Connecti } } } - - @Override - public void connectionEstablished(ConnectionEstablishedEvent event) { - System.err.println("connectionEstablished()"); - event.getStream().getEventManager().addNetworkMessageEventListener(this); - } } - - diff --git a/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_MemberTest.java b/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_MemberTest.java index 364ec84..a493cc5 100644 --- a/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_MemberTest.java +++ b/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_MemberTest.java @@ -36,7 +36,7 @@ import java.util.TreeSet; /** * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class WSYD_MemberTest { public WSYD_Member instance; diff --git a/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserIDTest.java b/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserIDTest.java index a1d6f55..9e73e52 100644 --- a/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserIDTest.java +++ b/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserIDTest.java @@ -33,7 +33,7 @@ import static org.junit.Assert.*; /** * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class WSYD_Member_Comparator_UserIDTest { WSYD_Member_Comparator_UserID instance; diff --git a/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserNameTest.java b/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserNameTest.java index ecbfbae..e6e20c8 100644 --- a/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserNameTest.java +++ b/test/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member_Comparator_UserNameTest.java @@ -32,7 +32,7 @@ import static org.junit.Assert.*; /** * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class WSYD_Member_Comparator_UserNameTest { WSYD_Member_Comparator_UserName instance; diff --git a/test/uk/ac/ntu/n0521366/wsyd/libs/net/WSYD_SocketAddressTest.java b/test/uk/ac/ntu/n0521366/wsyd/libs/net/WSYD_SocketAddressTest.java index 4e5f57e..fb07078 100644 --- a/test/uk/ac/ntu/n0521366/wsyd/libs/net/WSYD_SocketAddressTest.java +++ b/test/uk/ac/ntu/n0521366/wsyd/libs/net/WSYD_SocketAddressTest.java @@ -43,7 +43,7 @@ import uk.ac.ntu.n0521366.wsyd.libs.net.WSYD_SocketAddress.Protocol; /** * - * @author TJ + * @author Eddie Berrisford-Lynch */ public class WSYD_SocketAddressTest {