KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > amq > AmqRequest


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jms.amq;
31
32 import com.caucho.server.connection.Connection;
33 import com.caucho.server.port.ServerRequest;
34 import com.caucho.util.ByteBuffer;
35
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Protocol specific information for each request. ServerRequest
43  * is reused to reduce memory allocations.
44  *
45  * <p>ServerRequests are created by Server.createRequest()
46  */

47 public class AmqRequest extends AmqConnection implements ServerRequest
48 {
49   private static final Logger JavaDoc log =
50     Logger.getLogger(AmqRequest.class.getName());
51
52   private static final byte []AMQP_HEADER = new byte[] {
53     (byte) 'A', (byte) 'M', (byte) 'Q', (byte) 'P',
54     1, 1, 9, 1
55   };
56
57   private Connection _conn;
58   private ClassLoader JavaDoc _loader;
59
60   private ByteBuffer _packet = new ByteBuffer();
61   
62   AmqRequest(Connection conn, ClassLoader JavaDoc loader)
63   {
64     _conn = conn;
65     _loader = loader;
66   }
67   
68   /**
69    * Initialize the connection. At this point, the current thread is the
70    * connection thread.
71    */

72   public void init()
73   {
74   }
75
76   public boolean isWaitForRead()
77   {
78     return false;
79   }
80   
81   /**
82    * Handles a new connection. The controlling TcpServer may call
83    * handleConnection again after the connection completes, so
84    * the implementation must initialize any variables for each connection.
85    *
86    * @param conn Information about the connection, including buffered
87    * read and write streams.
88    */

89   public boolean handleRequest() throws IOException JavaDoc
90   {
91     _is = _conn.getReadStream();
92     _os = _conn.getWriteStream();
93
94     return doRequest();
95   }
96
97   protected boolean doHello()
98     throws IOException JavaDoc
99   {
100     if (_is.read() != 'M'
101     || _is.read() != 'Q'
102     || _is.read() != 'P'
103     || _is.read() != 1
104     || _is.read() != 1
105     || _is.read() != 9
106     || _is.read() != 1) {
107       _os.write(AMQP_HEADER, 0, AMQP_HEADER.length);
108       _os.flush();
109
110       return false;
111     }
112
113     _packet.clear();
114     _packet.addShort(CLASS_CONNECTION);
115     _packet.addShort(ID_CONNECTION_START);
116     _packet.add(9); // major
117
_packet.add(1); // minor
118
addTable(_packet, null); // system properties
119
addLongString(_packet, "PLAIN"); // security mechanisms
120
addLongString(_packet, "en_US"); // locales
121

122     writePacket(FRAME_METHOD, 0, _packet);
123
124     return true;
125   }
126
127   protected boolean doConnectionStartOk(InputStream JavaDoc is)
128     throws IOException JavaDoc
129   {
130     HashMap JavaDoc<String JavaDoc,String JavaDoc> props = readTable(is);
131
132     String JavaDoc auth = readShortString(is);
133     String JavaDoc credentials = readLongString(is);
134     String JavaDoc locale = readShortString(is);
135
136     _packet.clear();
137     _packet.addShort(CLASS_CONNECTION);
138     _packet.addShort(ID_CONNECTION_TUNE);
139     _packet.addShort(256); // max # of channels
140
_packet.addInt(MAX_FRAME); // frame max
141
_packet.addShort(HEARTBEAT); // minor
142

143     writePacket(FRAME_METHOD, 0, _packet);
144     
145     return true;
146   }
147
148   protected boolean doConnectionTuneOk(InputStream JavaDoc is)
149     throws IOException JavaDoc
150   {
151     int channelMax = readShort(is);
152     int frameMax = readInt(is);
153
154     if (frameMax < 4096 || MAX_FRAME < frameMax)
155       return fatalProtocolError(frameMax + " is an invalid frame size");
156
157     int heartbeat = readShort(is);
158     
159     return true;
160   }
161
162   protected boolean doConnectionOpen(InputStream JavaDoc is)
163     throws IOException JavaDoc
164   {
165     String JavaDoc host = readShortString(is);
166
167     System.out.println("VHOST: " + host);
168
169     _packet.clear();
170     _packet.addShort(CLASS_CONNECTION);
171     _packet.addShort(ID_CONNECTION_OPEN_OK);
172     addShortString(_packet, "");
173
174     writePacket(FRAME_METHOD, 0, _packet);
175     
176     return true;
177   }
178
179   protected boolean doChannelOpen(int id, InputStream JavaDoc is)
180     throws IOException JavaDoc
181   {
182     int prefetch = readInt(is);
183     String JavaDoc oob = readShortString(is);
184     
185     synchronized (_channels) {
186       if (_channels[id] != null)
187     return fatalProtocolError(id + " is an existing channel");
188
189       _channels[id] = new AmqServerChannel(this, id);
190
191       // set prefetch
192
}
193
194     _packet.clear();
195     _packet.addShort(CLASS_CHANNEL);
196     _packet.addShort(ID_CHANNEL_OPEN_OK);
197
198     writePacket(FRAME_METHOD, id, _packet);
199     
200     return true;
201   }
202   
203   public void protocolCloseEvent()
204   {
205   }
206 }
207
Popular Tags