KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > net > messages > NetworkMessage


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Uri Schneider.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.net.messages;
47
48 import java.net.SocketAddress JavaDoc;
49 import java.nio.ByteBuffer JavaDoc;
50
51 /**
52  * NetworkMessage.java
53  *
54  *
55  * Created: Sun Jan 25 16:04:19 2004
56  *
57  * @author Uri Schneider
58  * @version 1.0
59  */

60
61 public class NetworkMessage {
62     public static int NET_HEADERLEN = 3;
63
64     protected static byte NET_ID = 1;
65     protected static byte NET_KEEPALIVE = 2;
66     
67     private byte type;
68     private short length;
69 // private SocketChannel channel;
70
private SocketAddress JavaDoc source;
71     private SocketAddress JavaDoc dest;
72     private boolean isTCP;
73
74     protected NetworkMessage(byte type, short length) {
75         this.type = type;
76         this.length = length;
77     } // NetworkMessage constructor
78

79     protected NetworkMessage(ByteBuffer JavaDoc buf) {
80         this.type = buf.get();
81         this.length = buf.getShort();
82     }
83
84     public void write(ByteBuffer JavaDoc buf) {
85         buf.put(this.type);
86         buf.putShort(this.length);
87     }
88
89     public int getLength() {
90         return this.length;
91     }
92
93     public static NetworkMessage create(byte[] data, boolean isTCP,
94                                         SocketAddress JavaDoc source,
95                                         SocketAddress JavaDoc dest) {
96         return create(ByteBuffer.wrap(data), isTCP, source, dest);
97     }
98
99     public static NetworkMessage create(ByteBuffer JavaDoc buf, boolean isTCP,
100                                         SocketAddress JavaDoc source,
101                                         SocketAddress JavaDoc dest) {
102         buf.mark();
103         byte type = buf.get();
104         buf.reset();
105
106         NetworkMessage ret = null;
107         if (type == NET_ID) {
108             ret = new NetworkMessageID(buf);
109         } else if (type == NET_KEEPALIVE) {
110             ret = new NetworkMessageKeepalive(buf);
111         }
112
113         if (ret != null) {
114             ret.setTCP(isTCP);
115             ret.setSource(source);
116             ret.setDest(dest);
117         }
118         return ret;
119     }
120
121     public static void create(ByteBuffer JavaDoc buf, boolean isTCP,
122                               SocketAddress JavaDoc source, SocketAddress JavaDoc dest,
123                               NetworkMessageHandler handler) {
124         buf.mark();
125         byte type = buf.get();
126         buf.reset();
127
128         if (type == NET_ID) {
129             NetworkMessageID id = new NetworkMessageID(buf);
130             id.setTCP(isTCP);
131             id.setSource(source);
132             id.setDest(dest);
133             handler.handleNetMessageID(id);
134         }
135     }
136
137     /**
138      * @return Returns the dest.
139      */

140     public SocketAddress JavaDoc getDest() {
141         return dest;
142     }
143
144     /**
145      * @param dest The dest to set.
146      */

147     public void setDest(SocketAddress JavaDoc dest) {
148         this.dest = dest;
149     }
150
151     /**
152      * @return Returns the isTCP.
153      */

154     public boolean isTCP() {
155         return isTCP;
156     }
157
158     /**
159      * @param isTCP The isTCP to set.
160      */

161     public void setTCP(boolean isTCP) {
162         this.isTCP = isTCP;
163     }
164
165     /**
166      * @return Returns the source.
167      */

168     public SocketAddress JavaDoc getSource() {
169         return source;
170     }
171
172     /**
173      * @param source The source to set.
174      */

175     public void setSource(SocketAddress JavaDoc source) {
176         this.source = source;
177     }
178 } // NetworkMessage
179
Popular Tags