1 /* 2 * Copyright (c) 2011 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package jme3test.network; 33 34 import com.jme3.network.Client; 35 import com.jme3.network.Message; 36 import com.jme3.network.MessageListener; 37 import com.jme3.network.Network; 38 import java.awt.Component; 39 import java.awt.Dimension; 40 import java.awt.event.ActionEvent; 41 import java.io.IOException; 42 import javax.swing.*; 43 import jme3test.network.TestChatServer.ChatMessage; 44 45 /** 46 * A simple test chat server. When SM implements a set 47 * of standard chat classes this can become a lot simpler. 48 * 49 * @version $Revision: 8843 $ 50 * @author Paul Speed 51 */ 52 public class TestChatClient extends JFrame { 53 54 private Client client; 55 private JEditorPane chatLog; 56 private StringBuilder chatMessages = new StringBuilder(); 57 private JTextField nameField; 58 private JTextField messageField; 59 TestChatClient(String host)60 public TestChatClient(String host) throws IOException { 61 super("jME3 Test Chat Client - to:" + host); 62 63 // Build out the UI 64 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 65 setSize(800, 600); 66 67 chatLog = new JEditorPane(); 68 chatLog.setEditable(false); 69 chatLog.setContentType("text/html"); 70 chatLog.setText("<html><body>"); 71 72 getContentPane().add(new JScrollPane(chatLog), "Center"); 73 74 // A crude form 75 JPanel p = new JPanel(); 76 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); 77 p.add(new JLabel("Name:")); 78 nameField = new JTextField(System.getProperty("user.name", "yourname")); 79 Dimension d = nameField.getPreferredSize(); 80 nameField.setMaximumSize(new Dimension(120, d.height + 6)); 81 p.add(nameField); 82 p.add(new JLabel(" Message:")); 83 messageField = new JTextField(); 84 p.add(messageField); 85 p.add(new JButton(new SendAction(true))); 86 p.add(new JButton(new SendAction(false))); 87 88 getContentPane().add(p, "South"); 89 90 client = Network.connectToServer(TestChatServer.NAME, TestChatServer.VERSION, 91 host, TestChatServer.PORT, TestChatServer.UDP_PORT); 92 client.addMessageListener(new ChatHandler(), ChatMessage.class); 93 client.start(); 94 } 95 getString(Component owner, String title, String message, String initialValue)96 public static String getString(Component owner, String title, String message, String initialValue) { 97 return (String) JOptionPane.showInputDialog(owner, message, title, JOptionPane.PLAIN_MESSAGE, 98 null, null, initialValue); 99 } 100 main(String... args)101 public static void main(String... args) throws Exception { 102 TestChatServer.initializeClasses(); 103 104 // Grab a host string from the user 105 String s = getString(null, "Host Info", "Enter chat host:", "localhost"); 106 if (s == null) { 107 System.out.println("User cancelled."); 108 return; 109 } 110 111 TestChatClient test = new TestChatClient(s); 112 test.setVisible(true); 113 } 114 115 private class ChatHandler implements MessageListener<Client> { 116 messageReceived(Client source, Message m)117 public void messageReceived(Client source, Message m) { 118 ChatMessage chat = (ChatMessage) m; 119 120 System.out.println("Received:" + chat); 121 122 // One of the least efficient ways to add text to a 123 // JEditorPane 124 chatMessages.append("<font color='#00a000'>" + (m.isReliable() ? "TCP" : "UDP") + "</font>"); 125 chatMessages.append(" -- <font color='#000080'><b>" + chat.getName() + "</b></font> : "); 126 chatMessages.append(chat.getMessage()); 127 chatMessages.append("<br />"); 128 String s = "<html><body>" + chatMessages + "</body></html>"; 129 chatLog.setText(s); 130 131 // Set selection to the end so that the scroll panel will scroll 132 // down. 133 chatLog.select(s.length(), s.length()); 134 } 135 } 136 137 private class SendAction extends AbstractAction { 138 139 private boolean reliable; 140 SendAction(boolean reliable)141 public SendAction(boolean reliable) { 142 super(reliable ? "TCP" : "UDP"); 143 this.reliable = reliable; 144 } 145 actionPerformed(ActionEvent evt)146 public void actionPerformed(ActionEvent evt) { 147 String name = nameField.getText(); 148 String message = messageField.getText(); 149 150 ChatMessage chat = new ChatMessage(name, message); 151 chat.setReliable(reliable); 152 System.out.println("Sending:" + chat); 153 client.send(chat); 154 } 155 } 156 } 157