001         package com.croftsoft.core.net.http.msg;
002    
003         import java.net.*;
004         import java.util.*;
005    
006         import com.croftsoft.core.io.Encoder;
007         import com.croftsoft.core.io.Parser;
008         import com.croftsoft.core.lang.NullArgumentException;
009         import com.croftsoft.core.lang.lifecycle.Lifecycle;
010    import com.croftsoft.core.util.consumer.Consumer;
011         import com.croftsoft.core.util.queue.ListQueue;
012         import com.croftsoft.core.util.queue.Queue;
013    import com.croftsoft.core.util.queue.QueuePuller;
014    
015         /*********************************************************************
016         * An HttpMessagePoller/Pusher facade.
017         *
018         * @author
019         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
020         * @version
021         *   2003-06-12
022         * @since
023         *   2000-04-23
024         *********************************************************************/
025    
026         public final class  HttpMessageClient
027           implements Lifecycle
028         //////////////////////////////////////////////////////////////////////
029         //////////////////////////////////////////////////////////////////////
030         {
031    
032         private final Queue              incomingQueue;
033    
034         private final Queue              outgoingQueue;
035    
036         private final HttpMessagePoller  httpMessagePoller;
037    
038         private final HttpMessagePusher  httpMessagePusher;
039    
040         private final QueuePuller        incomingQueuePuller;
041    
042         //////////////////////////////////////////////////////////////////////
043         // constructor methods
044         //////////////////////////////////////////////////////////////////////
045    
046         /*********************************************************************
047         * Main constructor method.
048         *
049         * @param  consumer
050         *
051         *   May be null.
052         *********************************************************************/
053         public  HttpMessageClient (
054           URL       url,
055           String    userAgent,
056           Encoder   encoder,
057           Parser    parser,
058           String    contentType,  // "application/x-www-form-urlencoded"
059           byte [ ]  requestBytes,
060           Consumer  consumer,
061           long      pollingPeriodMin,
062           long      pollingPeriodMax,
063           long      pollingPeriodInit,
064           double    pollingPeriodMult,
065           double    pollingPeriodDivi,
066           long      pollingPeriodIncr )
067         //////////////////////////////////////////////////////////////////////
068         {
069           incomingQueue = new ListQueue ( );
070    
071           outgoingQueue = new ListQueue ( );
072    
073           httpMessagePoller = new HttpMessagePoller (
074             url,
075             userAgent,
076             contentType,
077             requestBytes,
078             parser,
079             incomingQueue,
080             pollingPeriodMin,
081             pollingPeriodMax,
082             pollingPeriodInit,
083             pollingPeriodMult,
084             pollingPeriodDivi,
085             pollingPeriodIncr );
086    
087           httpMessagePusher = new HttpMessagePusher (
088             outgoingQueue,
089             incomingQueue,
090             url,
091             userAgent,
092             contentType,
093             encoder,
094             parser );
095    
096           if ( consumer != null )
097           {
098             incomingQueuePuller = new QueuePuller ( incomingQueue, consumer );
099           }
100           else
101           {
102             incomingQueuePuller = null;
103           }
104         }
105    
106         /*********************************************************************
107         * Convenience constructor method.
108         *********************************************************************/
109         public  HttpMessageClient (
110           URL       url,
111           String    userAgent,
112           Encoder   encoder,
113           Parser    parser,
114           String    contentType,  // "application/x-www-form-urlencoded"
115           byte [ ]  requestBytes,
116           Consumer  consumer )
117         //////////////////////////////////////////////////////////////////////
118         {
119           this (
120             url,
121             userAgent,
122             encoder,
123             parser,
124             contentType,
125             requestBytes,
126             consumer,
127             HttpMessagePoller.DEFAULT_POLLING_PERIOD_MIN,
128             HttpMessagePoller.DEFAULT_POLLING_PERIOD_MAX,
129             HttpMessagePoller.DEFAULT_POLLING_PERIOD_INIT,
130             HttpMessagePoller.DEFAULT_POLLING_PERIOD_MULT,
131             HttpMessagePoller.DEFAULT_POLLING_PERIOD_DIVI,
132             HttpMessagePoller.DEFAULT_POLLING_PERIOD_INCR );
133         }
134    
135         //////////////////////////////////////////////////////////////////////
136         // accessor methods
137         //////////////////////////////////////////////////////////////////////
138    
139         public Queue  getIncomingQueue ( ) { return incomingQueue; }
140    
141         public Queue  getOutgoingQueue ( ) { return outgoingQueue; }
142    
143         //////////////////////////////////////////////////////////////////////
144         // mutator methods
145         //////////////////////////////////////////////////////////////////////
146    
147         public void  setRequestBytes ( byte [ ]  requestBytes )
148         //////////////////////////////////////////////////////////////////////
149         {
150           httpMessagePoller.setRequestBytes ( requestBytes );
151         }
152    
153         //////////////////////////////////////////////////////////////////////
154         // lifecycle methods
155         //////////////////////////////////////////////////////////////////////
156    
157         public synchronized void  init ( )
158         //////////////////////////////////////////////////////////////////////
159         {
160           if ( incomingQueuePuller != null )
161           {
162             incomingQueuePuller.init ( );
163           }
164    
165           httpMessagePoller.init ( );
166    
167           httpMessagePusher.init ( );
168         }
169    
170         public synchronized void  start ( )
171         //////////////////////////////////////////////////////////////////////
172         {
173           if ( incomingQueuePuller != null )
174           {
175             incomingQueuePuller.start ( );
176           }
177    
178           httpMessagePoller.start ( );
179    
180           httpMessagePusher.start ( );
181         }
182    
183         public synchronized void  stop ( )
184         //////////////////////////////////////////////////////////////////////
185         {
186           httpMessagePoller.stop ( );
187    
188           httpMessagePusher.stop ( );
189    
190           if ( incomingQueuePuller != null )
191           {
192             incomingQueuePuller.stop ( );
193           }
194         }
195    
196         public synchronized void  destroy ( )
197         //////////////////////////////////////////////////////////////////////
198         {
199           httpMessagePoller.destroy ( );
200    
201           httpMessagePusher.destroy ( );
202    
203           if ( incomingQueuePuller != null )
204           {
205             incomingQueuePuller.destroy ( );
206           }
207         }
208    
209         //////////////////////////////////////////////////////////////////////
210         // Queue methods
211         //////////////////////////////////////////////////////////////////////
212    
213         public boolean  append ( Object  o )
214         //////////////////////////////////////////////////////////////////////
215         {
216           return outgoingQueue.append ( o );
217         }
218    
219         public void  replace ( Object  o )
220         //////////////////////////////////////////////////////////////////////
221         {
222           outgoingQueue.replace ( o );
223         }
224    
225         public Object  poll ( )
226         //////////////////////////////////////////////////////////////////////
227         {
228           return incomingQueue.poll ( );
229         }
230    
231         public Object  pull ( )
232           throws InterruptedException
233         //////////////////////////////////////////////////////////////////////
234         {
235           return incomingQueue.pull ( );
236         }
237    
238         //////////////////////////////////////////////////////////////////////
239         //////////////////////////////////////////////////////////////////////
240         }