Home | History | Annotate | Download | only in network
      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.*;
     35 import com.jme3.network.serializing.Serializable;
     36 import com.jme3.network.serializing.Serializer;
     37 
     38 /**
     39  *  A simple test chat server.  When SM implements a set
     40  *  of standard chat classes this can become a lot simpler.
     41  *
     42  *  @version   $Revision: 8843 $
     43  *  @author    Paul Speed
     44  */
     45 public class TestChatServer {
     46     // Normally these and the initialized method would
     47     // be in shared constants or something.
     48 
     49     public static final String NAME = "Test Chat Server";
     50     public static final int VERSION = 1;
     51     public static final int PORT = 5110;
     52     public static final int UDP_PORT = 5110;
     53 
     54     public static void initializeClasses() {
     55         // Doing it here means that the client code only needs to
     56         // call our initialize.
     57         Serializer.registerClass(ChatMessage.class);
     58     }
     59 
     60     public static void main(String... args) throws Exception {
     61         initializeClasses();
     62 
     63         // Use this to test the client/server name version check
     64         Server server = Network.createServer(NAME, VERSION, PORT, UDP_PORT);
     65         server.start();
     66 
     67         ChatHandler handler = new ChatHandler();
     68         server.addMessageListener(handler, ChatMessage.class);
     69 
     70         // Keep running basically forever
     71         synchronized (NAME) {
     72             NAME.wait();
     73         }
     74     }
     75 
     76     private static class ChatHandler implements MessageListener<HostedConnection> {
     77 
     78         public ChatHandler() {
     79         }
     80 
     81         public void messageReceived(HostedConnection source, Message m) {
     82             if (m instanceof ChatMessage) {
     83                 // Keep track of the name just in case we
     84                 // want to know it for some other reason later and it's
     85                 // a good example of session data
     86                 source.setAttribute("name", ((ChatMessage) m).getName());
     87 
     88                 System.out.println("Broadcasting:" + m + "  reliable:" + m.isReliable());
     89 
     90                 // Just rebroadcast... the reliable flag will stay the
     91                 // same so if it came in on UDP it will go out on that too
     92                 source.getServer().broadcast(m);
     93             } else {
     94                 System.err.println("Received odd message:" + m);
     95             }
     96         }
     97     }
     98 
     99     @Serializable
    100     public static class ChatMessage extends AbstractMessage {
    101 
    102         private String name;
    103         private String message;
    104 
    105         public ChatMessage() {
    106         }
    107 
    108         public ChatMessage(String name, String message) {
    109             setName(name);
    110             setMessage(message);
    111         }
    112 
    113         public void setName(String name) {
    114             this.name = name;
    115         }
    116 
    117         public String getName() {
    118             return name;
    119         }
    120 
    121         public void setMessage(String s) {
    122             this.message = s;
    123         }
    124 
    125         public String getMessage() {
    126             return message;
    127         }
    128 
    129         public String toString() {
    130             return name + ":" + message;
    131         }
    132     }
    133 }
    134