Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (c) 2009-2010 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 
     33 package jme3test.network;
     34 
     35 import com.jme3.network.*;
     36 import com.jme3.network.serializing.Serializable;
     37 import com.jme3.network.serializing.Serializer;
     38 import java.io.IOException;
     39 import java.util.*;
     40 
     41 public class TestSerialization implements MessageListener<HostedConnection> {
     42 
     43     @Serializable
     44     public static class SomeObject {
     45 
     46         private int val;
     47 
     48         public SomeObject(){
     49         }
     50 
     51         public SomeObject(int val){
     52             this.val = val;
     53         }
     54 
     55         public int getVal(){
     56             return val;
     57         }
     58 
     59         @Override
     60         public String toString(){
     61             return "SomeObject[val="+val+"]";
     62         }
     63     }
     64 
     65     public enum Status {
     66         High,
     67         Middle,
     68         Low;
     69     }
     70 
     71     @Serializable
     72     public static class TestSerializationMessage extends AbstractMessage {
     73 
     74         boolean z;
     75         byte b;
     76         char c;
     77         short s;
     78         int i;
     79         float f;
     80         long l;
     81         double d;
     82 
     83         int[] ia;
     84         List<Object> ls;
     85         Map<String, SomeObject> mp;
     86 
     87         Status status1;
     88         Status status2;
     89 
     90         Date date;
     91 
     92         public TestSerializationMessage(){
     93             super(true);
     94         }
     95 
     96         public TestSerializationMessage(boolean initIt){
     97             super(true);
     98             if (initIt){
     99                 z = true;
    100                 b = -88;
    101                 c = 'Y';
    102                 s = 9999;
    103                 i = 123;
    104                 f = -75.4e8f;
    105                 l = 9438345072805034L;
    106                 d = -854834.914703e88;
    107                 ia = new int[]{ 456, 678, 999 };
    108 
    109                 ls = new ArrayList<Object>();
    110                 ls.add("hello");
    111                 ls.add(new SomeObject(-22));
    112 
    113                 mp = new HashMap<String, SomeObject>();
    114                 mp.put("abc", new SomeObject(555));
    115 
    116                 status1 = Status.High;
    117                 status2 = Status.Middle;
    118 
    119                 date = new Date(System.currentTimeMillis());
    120             }
    121         }
    122     }
    123 
    124     public void messageReceived(HostedConnection source, Message m) {
    125         TestSerializationMessage cm = (TestSerializationMessage) m;
    126         System.out.println(cm.z);
    127         System.out.println(cm.b);
    128         System.out.println(cm.c);
    129         System.out.println(cm.s);
    130         System.out.println(cm.i);
    131         System.out.println(cm.f);
    132         System.out.println(cm.l);
    133         System.out.println(cm.d);
    134         System.out.println(Arrays.toString(cm.ia));
    135         System.out.println(cm.ls);
    136         System.out.println(cm.mp);
    137         System.out.println(cm.status1);
    138         System.out.println(cm.status2);
    139         System.out.println(cm.date);
    140     }
    141 
    142     public static void main(String[] args) throws IOException, InterruptedException{
    143         Serializer.registerClass(SomeObject.class);
    144         Serializer.registerClass(TestSerializationMessage.class);
    145 
    146         Server server = Network.createServer( 5110 );
    147         server.start();
    148 
    149         Client client = Network.connectToServer( "localhost", 5110 );
    150         client.start();
    151 
    152         server.addMessageListener(new TestSerialization(), TestSerializationMessage.class);
    153         client.send(new TestSerializationMessage(true));
    154 
    155         Thread.sleep(10000);
    156     }
    157 
    158 }
    159