KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > udp > DatagramHeaderMarshaller


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.udp;
19
20
21 import org.apache.activemq.command.Command;
22 import org.apache.activemq.command.Endpoint;
23
24 import java.io.DataInputStream JavaDoc;
25 import java.io.DataOutputStream JavaDoc;
26 import java.net.DatagramPacket JavaDoc;
27 import java.net.SocketAddress JavaDoc;
28 import java.nio.ByteBuffer JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  *
34  * @version $Revision: 426366 $
35  */

36 public class DatagramHeaderMarshaller {
37
38     // TODO for large dynamic networks
39
// we may want to evict endpoints that disconnect
40
// from a transport - e.g. for multicast
41
private Map JavaDoc endpoints = new HashMap JavaDoc();
42     
43     /**
44      * Reads any header if applicable and then creates an endpoint object
45      */

46     public Endpoint createEndpoint(ByteBuffer JavaDoc readBuffer, SocketAddress JavaDoc address) {
47         return getEndpoint(address);
48     }
49
50     public Endpoint createEndpoint(DatagramPacket JavaDoc datagram, DataInputStream JavaDoc dataIn) {
51         return getEndpoint(datagram.getSocketAddress());
52     }
53
54     public void writeHeader(Command command, ByteBuffer JavaDoc writeBuffer) {
55         /*
56         writeBuffer.putLong(command.getCounter());
57         writeBuffer.putInt(command.getDataSize());
58         byte flags = command.getFlags();
59         //System.out.println("Writing header with counter: " + header.getCounter() + " size: " + header.getDataSize() + " with flags: " + flags);
60         writeBuffer.put(flags);
61         */

62     }
63
64     public void writeHeader(Command command, DataOutputStream JavaDoc dataOut) {
65     }
66
67     /**
68      * Gets the current endpoint object for this address or creates one if not available.
69      *
70      * Note that this method does not need to be synchronized as its only ever going to be
71      * used by the already-synchronized read() method of a CommandChannel
72      *
73      */

74     protected Endpoint getEndpoint(SocketAddress JavaDoc address) {
75         Endpoint endpoint = (Endpoint) endpoints.get(address);
76         if (endpoint == null) {
77             endpoint = createEndpoint(address);
78             endpoints.put(address, endpoint);
79         }
80         return endpoint;
81     }
82
83     protected Endpoint createEndpoint(SocketAddress JavaDoc address) {
84         return new DatagramEndpoint(address.toString(), address);
85     }
86 }
87
Popular Tags