001 package com.croftsoft.core.net.http.msg; 002 003 import java.io.*; 004 import java.net.*; 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.net.http.HttpLib; 011 import com.croftsoft.core.util.loop.FixedDelayLoopGovernor; 012 import com.croftsoft.core.util.loop.Loopable; 013 import com.croftsoft.core.util.loop.Looper; 014 import com.croftsoft.core.util.queue.Queue; 015 016 /********************************************************************* 017 * Pushes outgoing messages to the server using HTTP. 018 * 019 * @author 020 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 021 * @version 022 * 2003-05-26 023 * @since 024 * 2000-04-23 025 *********************************************************************/ 026 027 public final class HttpMessagePusher 028 implements Lifecycle 029 ////////////////////////////////////////////////////////////////////// 030 ////////////////////////////////////////////////////////////////////// 031 { 032 033 public static final long MINIMUM_DELAY = 100; 034 035 public static final String THREAD_NAME = "HttpMessagePusher"; 036 037 public static final int THREAD_PRIORITY = Thread.MIN_PRIORITY; 038 039 public static final boolean USE_DAEMON_THREAD = true; 040 041 // 042 043 private final Queue outgoingQueue; 044 045 private final Queue incomingQueue; 046 047 private final Encoder encoder; 048 049 private final Looper looper; 050 051 private final URL url; 052 053 private final String userAgent; 054 055 private final String contentType; 056 057 private final Parser parser; 058 059 ////////////////////////////////////////////////////////////////////// 060 ////////////////////////////////////////////////////////////////////// 061 062 public HttpMessagePusher ( 063 Queue outgoingQueue, 064 Queue incomingQueue, 065 URL url, 066 String userAgent, 067 String contentType, 068 Encoder encoder, 069 Parser parser ) 070 ////////////////////////////////////////////////////////////////////// 071 { 072 NullArgumentException.check ( this.outgoingQueue = outgoingQueue ); 073 074 NullArgumentException.check ( this.incomingQueue = incomingQueue ); 075 076 NullArgumentException.check ( this.userAgent = userAgent ); 077 078 NullArgumentException.check ( this.contentType = contentType ); 079 080 NullArgumentException.check ( this.encoder = encoder ); 081 082 NullArgumentException.check ( this.parser = parser ); 083 084 NullArgumentException.check ( this.url = url ); 085 086 looper = new Looper ( 087 new Loopable ( ) 088 { 089 public boolean loop ( ) 090 { 091 return HttpMessagePusher.this.loop ( ); 092 } 093 }, 094 new FixedDelayLoopGovernor ( MINIMUM_DELAY, 0 ), 095 null, 096 THREAD_NAME, 097 THREAD_PRIORITY, 098 USE_DAEMON_THREAD ); 099 } 100 101 ////////////////////////////////////////////////////////////////////// 102 ////////////////////////////////////////////////////////////////////// 103 104 public void init ( ) 105 ////////////////////////////////////////////////////////////////////// 106 { 107 looper.init ( ); 108 } 109 110 public void start ( ) 111 ////////////////////////////////////////////////////////////////////// 112 { 113 looper.start ( ); 114 } 115 116 public void stop ( ) 117 ////////////////////////////////////////////////////////////////////// 118 { 119 looper.stop ( ); 120 } 121 122 public void destroy ( ) 123 ////////////////////////////////////////////////////////////////////// 124 { 125 looper.destroy ( ); 126 } 127 128 ////////////////////////////////////////////////////////////////////// 129 // private methods 130 ////////////////////////////////////////////////////////////////////// 131 132 private boolean loop ( ) 133 ////////////////////////////////////////////////////////////////////// 134 { 135 try 136 { 137 Object request = outgoingQueue.pull ( ); 138 139 Object response = HttpLib.post ( 140 url, 141 encoder.encode ( request ), 142 userAgent, 143 contentType, 144 parser ); 145 146 if ( response != null ) 147 { 148 incomingQueue.append ( response ); 149 } 150 } 151 catch ( InterruptedException ex ) 152 { 153 } 154 catch ( Exception ex ) 155 { 156 ex.printStackTrace ( ); 157 158 return false; 159 } 160 161 return true; 162 } 163 164 ////////////////////////////////////////////////////////////////////// 165 ////////////////////////////////////////////////////////////////////// 166 }