TestChatClient.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. import java.awt.Dimension;
  34. import java.awt.event.ActionEvent;
  35. import java.awt.Component;
  36. import java.io.IOException;
  37. import javax.swing.*;
  38. import com.jme3.network.AbstractMessage;
  39. import com.jme3.network.Client;
  40. import com.jme3.network.HostedConnection;
  41. import com.jme3.network.Message;
  42. import com.jme3.network.MessageListener;
  43. import com.jme3.network.Network;
  44. import com.jme3.network.Server;
  45. import com.jme3.network.serializing.Serializable;
  46. import com.jme3.network.serializing.Serializer;
  47. import jme3test.network.TestChatServer.ChatMessage;
  48. /**
  49. * A simple test chat server. When SM implements a set
  50. * of standard chat classes this can become a lot simpler.
  51. *
  52. * @version $Revision$
  53. * @author Paul Speed
  54. */
  55. public class TestChatClient extends JFrame
  56. {
  57. private Client client;
  58. private JEditorPane chatLog;
  59. private StringBuilder chatMessages = new StringBuilder();
  60. private JTextField nameField;
  61. private JTextField messageField;
  62. public TestChatClient( String host ) throws IOException
  63. {
  64. super( "jME3 Test Chat Client - to:" + host );
  65. // Build out the UI
  66. setDefaultCloseOperation( DISPOSE_ON_CLOSE );
  67. setSize( 800, 600 );
  68. chatLog = new JEditorPane();
  69. chatLog.setEditable(false);
  70. chatLog.setContentType("text/html");
  71. chatLog.setText( "<html><body>" );
  72. getContentPane().add( new JScrollPane(chatLog), "Center" );
  73. // A crude form
  74. JPanel p = new JPanel();
  75. p.setLayout( new BoxLayout(p, BoxLayout.X_AXIS) );
  76. p.add( new JLabel( "Name:" ) );
  77. nameField = new JTextField( System.getProperty( "user.name", "yourname" ) );
  78. Dimension d = nameField.getPreferredSize();
  79. nameField.setMaximumSize( new Dimension(120, d.height + 6) );
  80. p.add( nameField );
  81. p.add( new JLabel( " Message:" ) );
  82. messageField = new JTextField();
  83. p.add( messageField );
  84. p.add( new JButton(new SendAction(true)) );
  85. p.add( new JButton(new SendAction(false)) );
  86. getContentPane().add( p, "South" );
  87. client = Network.connectToServer( TestChatServer.NAME, TestChatServer.VERSION,
  88. host, TestChatServer.PORT, TestChatServer.PORT + 1 );
  89. client.addMessageListener( new ChatHandler(), ChatMessage.class );
  90. client.start();
  91. }
  92. public static String getString( Component owner, String title, String message, String initialValue )
  93. {
  94. return (String)JOptionPane.showInputDialog( owner, message, title, JOptionPane.PLAIN_MESSAGE,
  95. null, null, initialValue );
  96. }
  97. public static void main( String... args ) throws Exception
  98. {
  99. TestChatServer.initializeClasses();
  100. // Grab a host string from the user
  101. String s = getString( null, "Host Info", "Enter chat host:", "localhost" );
  102. if( s == null ) {
  103. System.out.println( "User cancelled." );
  104. return;
  105. }
  106. TestChatClient test = new TestChatClient( s );
  107. test.setVisible( true );
  108. }
  109. private class ChatHandler implements MessageListener<Client>
  110. {
  111. public void messageReceived( Client source, Message m )
  112. {
  113. ChatMessage chat = (ChatMessage)m;
  114. System.out.println( "Received:" + chat );
  115. // One of the least efficient ways to add text to a
  116. // JEditorPane
  117. chatMessages.append( "<font color='#00a000'>" + (m.isReliable() ? "TCP" : "UDP") + "</font>" );
  118. chatMessages.append( " -- <font color='#000080'><b>" + chat.getName() + "</b></font> : " );
  119. chatMessages.append( chat.getMessage() );
  120. chatMessages.append( "<br />" );
  121. String s = "<html><body>" + chatMessages + "</body></html>";
  122. chatLog.setText( s );
  123. // Set selection to the end so that the scroll panel will scroll
  124. // down.
  125. chatLog.select( s.length(), s.length() );
  126. }
  127. }
  128. private class SendAction extends AbstractAction
  129. {
  130. private boolean reliable;
  131. public SendAction( boolean reliable )
  132. {
  133. super( reliable ? "TCP" : "UDP" );
  134. this.reliable = reliable;
  135. }
  136. public void actionPerformed( ActionEvent evt )
  137. {
  138. String name = nameField.getText();
  139. String message = messageField.getText();
  140. ChatMessage chat = new ChatMessage( name, message );
  141. chat.setReliable(reliable);
  142. System.out.println( "Sending:" + chat );
  143. client.send( chat );
  144. }
  145. }
  146. }