Merge pull request #7 from informatik-ag-ngl/f/startup

senderID
This commit is contained in:
DieGurke 2019-10-13 18:06:57 +02:00 committed by GitHub
commit b079f7e605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 103 additions and 44 deletions

View File

@ -17,6 +17,7 @@ import envoy.schema.Message;
import envoy.schema.Message.MetaData.MessageState;
import envoy.schema.Messages;
import envoy.schema.ObjectFactory;
import envoy.schema.User;
import envoy.schema.Users;
/**
@ -26,6 +27,7 @@ import envoy.schema.Users;
*
* @author Kai S. K. Engelbart
* @author Maximilian Käfer
* @author Leon Hofmeister
* @since Envoy 0.1
*/
@ -34,14 +36,17 @@ public class Client {
private ObjectFactory objectFactory = new ObjectFactory();
private DatatypeFactory datatypeFactory;
private Config config;
private User user;
public Client(Config config) {
public Client(Config config, String username) {
this.config = config;
try {
datatypeFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
}
user = getUser(username);
System.out.printf("Mein Name ist %s und ich habe die ID %d%n", user.getName(), user.getID());
}
/**
@ -49,9 +54,9 @@ public class Client {
* Because sending a request is a blocking operation, it is executed
* asynchronously.
*
* @param sender Name of the sender
* @param recipient Name of the recipient
* @param textContent Content (text) of the message
* @param sender name of the sender
* @param recipient name of the recipient
* @param textContent content (text) of the message
*/
public void sendMessage(Message message) {
new Thread(() -> {
@ -87,7 +92,7 @@ public class Client {
* @param senderID The ID of the sender
* @param recipientID The ID of the recipient
* @param textContent The content (text) of the message
* @return Prepared {@link Message} object
* @return prepared {@link Message} object
*/
public Message createMessage(long senderID, long recipientID, String textContent) {
Message.MetaData metaData = objectFactory.createMessageMetaData();
@ -124,4 +129,29 @@ public class Client {
client.close();
return users;
}
/**
* Returns a {@link User} with a specific name by name.
*
* @param name - the name of the {@link User}
* @return a {@link User} with the specified name
* @since Envoy 0.1
*/
private User getUser(String name) {
javax.ws.rs.client.Client client = ClientBuilder.newClient();
WebTarget target = client.target(String
.format("%s:%d/envoy-server/rest/user/sender?name=" + name, config.getServer(), config.getPort()));
Response response = target.request("application/xml").get();
Users users = response.readEntity(Users.class);
System.out.println("Response code: " + response.getStatus());
response.close();
client.close();
return users.getUser().get(0);
}
/**
* @return the user id of this client
* @since Envoy 0.1
*/
public long getSenderID() { return user.getID(); }
}

View File

@ -2,13 +2,10 @@ package envoy.client.ui;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.util.Properties;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
@ -23,7 +20,6 @@ import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import envoy.client.Client;
import envoy.client.Config;
import envoy.schema.Message;
import envoy.schema.User;
import envoy.schema.Users;
@ -35,6 +31,7 @@ import envoy.schema.Users;
*
* @author Kai S. K. Engelbart
* @author Maximilian Käfer
* @author Leon Hofmeister
* @since Envoy 0.1
*/
public class ChatWindow extends JFrame {
@ -44,11 +41,13 @@ public class ChatWindow extends JFrame {
private long recipientID = 0;
private JPanel contentPane = new JPanel();
private static Client client;
private Client client;
private DefaultListModel<String> messageListModel = new DefaultListModel<>();
public ChatWindow() {
public ChatWindow(Client client) {
this.client = client;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
setTitle("Envoy");
@ -135,7 +134,8 @@ public class ChatWindow extends JFrame {
// TODO: Acquire proper sender id
if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
final Message message = client.createMessage(1, recipientID, messageEnterTextfield.getText());
final Message message = client
.createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText());
client.sendMessage(message);
appendMessageToChat(message);
messageEnterTextfield.setText("");
@ -211,36 +211,4 @@ public class ChatWindow extends JFrame {
+ message.getMetaData().getSender() + "</b> </small>" + "<br>" + "<p style=\"color:white\">"
+ getFirstTextContent(message) + "</span></html>");
}
public static void main(String[] args) {
Config config = new Config();
if (args.length > 0) {
config.load(args);
} else {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
Properties configProperties = new Properties();
configProperties.load(loader.getResourceAsStream("server.properties"));
config.load(configProperties);
} catch (IOException e) {
e.printStackTrace();
}
}
if(!config.isInitialized()) {
System.err.println("Server or port are not defined. Exiting...");
System.exit(1);
}
client = new Client(config);
EventQueue.invokeLater(() -> {
try {
ChatWindow frame = new ChatWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}

View File

@ -0,0 +1,61 @@
package envoy.client.ui;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
import envoy.client.Client;
import envoy.client.Config;
/**
* Starts the Envoy client and prompts the user to enter their name.
*
* Project: <strong>envoy-client</strong><br>
* File: <strong>Startup.java</strong><br>
* Created: <strong>12 Oct 2019</strong><br>
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @since Envoy 0.1
*/
public class Startup {
public static void main(String[] args) {
Config config = new Config();
if (args.length > 0) {
config.load(args);
} else {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
Properties configProperties = new Properties();
configProperties.load(loader.getResourceAsStream("server.properties"));
config.load(configProperties);
} catch (IOException e) {
e.printStackTrace();
}
}
if (!config.isInitialized()) {
System.err.println("Server or port are not defined. Exiting...");
System.exit(1);
}
String userName = JOptionPane.showInputDialog("Please enter your username");
if (userName == null || userName.isEmpty()) {
System.err.println("User name is not set or empty. Exiting...");
System.exit(1);
}
Client client = new Client(config, userName);
EventQueue.invokeLater(() -> {
try {
ChatWindow frame = new ChatWindow(client);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}