inital plugin from webtools project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.core / src / net / sourceforge / phpdt / monitor / core / internal / http / HTTPConnection.java
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.core.internal.http;
12
13 import net.sourceforge.phpdt.monitor.core.IMonitor;
14 import net.sourceforge.phpdt.monitor.core.IRequest;
15 import net.sourceforge.phpdt.monitor.core.internal.Request;
16 import net.sourceforge.phpdt.monitor.core.internal.Trace;
17
18
19 import java.util.List;
20 import java.util.ArrayList;
21 /**
22  * Manages a monitor server connection between two hosts. This
23  * connection may spawn one or more TCP/IP pairs to be displayed
24  * in the monitor server view.
25  */
26 public class HTTPConnection {
27         protected IMonitor monitor;
28
29         protected int req = -1;
30         protected int resp = -1;
31
32         protected List calls = new ArrayList();
33
34         /**
35          * MonitorHTTPConnection constructor comment.
36          */
37         public HTTPConnection(IMonitor monitor) {
38                 super();
39                 this.monitor = monitor;
40                 Trace.trace(Trace.PARSING, "TCP/IP monitor connection opened " + monitor);
41         }
42
43         /**
44          * Add a request.
45          * @param req byte[]
46          * @param isNew boolean
47          */
48         public void addRequest(byte[] request, boolean isNew) {
49                 if (isNew)
50                         req ++;
51                 Request pair = (Request) getRequestResponse(req);
52                 pair.addToRequest(request);
53         }
54
55         /**
56          * Add a response.
57          * @param req byte[]
58          * @param isNew boolean
59          */
60         public void addResponse(byte[] response, boolean isNew) {
61                 if (isNew)
62                         resp ++;
63                 Request pair = (Request) getRequestResponse(resp);
64                 pair.addToResponse(response);
65         }
66
67         /**
68          * 
69          */
70         public void addProperty(String key, Object value) {
71                 IRequest pair = getRequestResponse(req);
72                 pair.addProperty(key, value);
73         }
74
75         /**
76          * 
77          */
78         public IRequest getRequestResponse(boolean isRequest) {
79                 if (isRequest)
80                         return getRequestResponse(req);
81                 else
82                         return getRequestResponse(resp);
83         }
84
85         /**
86          * 
87          */
88         protected IRequest getRequestResponse(int i) {
89                 synchronized (this) {
90                         while (i >= calls.size()) {
91                                 Request rr = new HTTPRequest(monitor.getLocalPort(), monitor.getRemoteHost(), monitor.getRemotePort());
92                                 calls.add(rr);
93                                 return rr;
94                         }
95                         return (Request) calls.get(i);
96                 }
97         }
98
99         /**
100          * Set the title
101          * @param req byte[]
102          * @param isNew boolean
103          */
104         public void setLabel(String title, boolean isNew) {
105                 if (isNew)
106                         req ++;
107                 Request pair = (Request) getRequestResponse(req);
108                 pair.setLabel(title);
109         }
110 }