1 /**********************************************************************
2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.monitor.core.internal;
13 import java.util.Date;
14 import java.util.Properties;
16 import net.sourceforge.phpdt.monitor.core.IProtocolAdapter;
17 import net.sourceforge.phpdt.monitor.core.IRequest;
19 * A single TCP/IP request/response pair.
21 public class Request implements IRequest {
23 protected long responseTime = -1;
24 protected int localPort;
25 protected String remoteHost;
26 protected int remotePort;
27 protected byte[] request;
28 protected byte[] response;
30 protected String label;
31 protected IProtocolAdapter type;
33 protected Properties properties;
36 * RequestResponse constructor comment.
38 public Request(IProtocolAdapter type, int localPort, String remoteHost, int remotePort) {
41 this.localPort = localPort;
42 this.remoteHost = remoteHost;
43 this.remotePort = remotePort;
45 properties = new Properties();
46 MonitorManager.getInstance().addRequest(this);
49 public IProtocolAdapter getType() {
56 * @param addRequest byte[]
58 public void addToRequest(byte[] addRequest) {
59 if (addRequest == null || addRequest.length == 0)
62 if (request == null || request.length == 0) {
63 setRequest(addRequest);
67 int size = request.length + addRequest.length;
68 byte[] b = new byte[size];
69 System.arraycopy(request, 0, b, 0, request.length);
70 System.arraycopy(addRequest, 0, b, request.length, addRequest.length);
75 * Add to the response.
77 * @param addResponse byte[]
79 public void addToResponse(byte[] addResponse) {
80 if (addResponse == null || addResponse.length == 0)
83 if (response == null || response.length == 0) {
84 setResponse(addResponse);
88 int size = response.length + addResponse.length;
89 byte[] b = new byte[size];
90 System.arraycopy(response, 0, b, 0, response.length);
91 System.arraycopy(addResponse, 0, b, response.length, addResponse.length);
96 * Return the date/time of this request.
98 * @return java.util.Date
100 public Date getDate() {
105 * Returns the local port.
109 public int getLocalPort() {
114 * Returns the remote host.
116 * @return java.lang.String
118 public String getRemoteHost() {
123 * Returns the remote port.
127 public int getRemotePort() {
132 * Returns the request as a byte array.
136 public byte[] getRequest(byte type2) {
141 * Returns the response as a byte array.
145 public byte[] getResponse(byte type2) {
150 * Returns the response time in milliseconds.
154 public long getResponseTime() {
159 * Returns the title, if one exists.
161 * @return java.lang.String
163 public String getLabel() {
165 return getRemoteHost() + ":" + getRemotePort();
173 * @param request byte[]
175 protected void setRequest(byte[] request) {
176 if (request == null || request.length == 0)
179 this.request = request;
181 MonitorManager.getInstance().requestChanged(this);
187 * @param response byte[]
189 protected void setResponse(byte[] response) {
190 if (response == null || response.length == 0)
193 this.response = response;
195 responseTime = System.currentTimeMillis() - date.getTime();
197 MonitorManager.getInstance().requestChanged(this);
203 * @param s java.lang.String
205 public void setLabel(String s) {
206 // property can only be set once
211 MonitorManager.getInstance().requestChanged(this);
217 public void addProperty(String key, Object value) {
219 if (properties.containsKey(key))
220 properties.remove(key);
221 properties.put(key, value);
222 } catch (Exception e) {
223 Trace.trace(Trace.SEVERE, "Could not add property", e);
230 public String getStringProperty(String key) {
232 return (String) properties.get(key);
233 } catch (Exception e) {
241 public Integer getIntegerProperty(String key) {
243 return (Integer) properties.get(key);
244 } catch (Exception e) {
252 public Object getObjectProperty(String key) {
254 return properties.get(key);
255 } catch (Exception e) {
260 public void fireChangedEvent() {
261 MonitorManager.getInstance().requestChanged(this);