001 package com.croftsoft.apps.client;
002
003 import java.io.*;
004 import java.net.*;
005 import java.util.*;
006 import java.util.concurrent.*;
007
008 import com.croftsoft.core.io.StreamLib;
009 import com.croftsoft.core.lang.*;
010 import com.croftsoft.core.lang.lifecycle.*;
011 import com.croftsoft.core.util.loop.*;
012
013 /*********************************************************************
014 * Network communications.
015 *
016 * @version
017 * $Id: ClientNet.java,v 1.7 2006/12/16 05:05:27 croft Exp $
018 * @since
019 * 2006-10-30
020 * @author
021 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
022 *********************************************************************/
023
024 public final class ClientNet
025 implements Lifecycle, Loopable
026 //////////////////////////////////////////////////////////////////////
027 //////////////////////////////////////////////////////////////////////
028 {
029
030 private final ClientConfig clientConfig;
031
032 private final Queue<ClientMessage>
033 modelQueue,
034 netQueue;
035
036 private final Looper looper;
037
038 //
039
040 private URL codeBase;
041
042 //////////////////////////////////////////////////////////////////////
043 //////////////////////////////////////////////////////////////////////
044
045 public ClientNet (
046 final ClientConfig clientConfig,
047 final BlockingQueue<ClientMessage> netQueue,
048 final Queue<ClientMessage> modelQueue )
049 //////////////////////////////////////////////////////////////////////
050 {
051 NullArgumentException.checkArgs (
052 this.clientConfig = clientConfig,
053 this.netQueue = netQueue,
054 this.modelQueue = modelQueue );
055
056 looper = new Looper (
057 this, // loopable
058 new NanoTimeLoopGovernor ( ),
059 null, // exceptionHandler
060 getClass ( ).getName ( ), // threadName
061 Thread.MIN_PRIORITY,
062 true ); // useDaemonThread
063 }
064
065 //////////////////////////////////////////////////////////////////////
066 // interface Lifecycle methods
067 //////////////////////////////////////////////////////////////////////
068
069 public void init ( )
070 //////////////////////////////////////////////////////////////////////
071 {
072 codeBase = clientConfig.getCodeBase ( );
073
074 if ( codeBase == null )
075 {
076 try
077 {
078 final String defaultCodeBaseName
079 = clientConfig.getDefaultCodeBaseName ( );
080
081 codeBase = new URL ( defaultCodeBaseName );
082 }
083 catch ( Exception ex )
084 {
085 ex.printStackTrace ( );
086
087 try
088 {
089 codeBase = new URL ( "http://localhost:8080/" );
090 }
091 catch ( Exception ex2 )
092 {
093 ex2.printStackTrace ( );
094 }
095 }
096 }
097
098 LifecycleLib.init ( looper );
099 }
100
101 public void start ( )
102 //////////////////////////////////////////////////////////////////////
103 {
104 LifecycleLib.start ( looper );
105 }
106
107 public void stop ( )
108 //////////////////////////////////////////////////////////////////////
109 {
110 LifecycleLib.stop ( looper );
111 }
112
113 public void destroy ( )
114 //////////////////////////////////////////////////////////////////////
115 {
116 LifecycleLib.destroy ( looper );
117 }
118
119 //////////////////////////////////////////////////////////////////////
120 //////////////////////////////////////////////////////////////////////
121
122 public boolean loop ( )
123 //////////////////////////////////////////////////////////////////////
124 {
125 ClientMessage message = null;
126
127 while ( ( message = netQueue.poll ( ) ) != null )
128 {
129 final ClientMessage.Type type = message.getType ( );
130
131 switch ( type )
132 {
133 case SEND_TEXT_REQUEST:
134
135 doSendTextRequest ( message );
136
137 break;
138
139 default:
140
141 System.out.println ( getClass ( ).getName ( ) + ": "
142 + "unknown message type: " + type );
143 }
144 }
145
146 return true;
147 }
148
149 //////////////////////////////////////////////////////////////////////
150 //////////////////////////////////////////////////////////////////////
151
152 private void doSendTextRequest ( final ClientMessage clientMessage )
153 //////////////////////////////////////////////////////////////////////
154 {
155 try
156 {
157 final String text = ( String ) clientMessage.getContent ( );
158
159 String botIdParam = "";
160
161 final String botId = clientConfig.getBotId ( );
162
163 if ( botId != null )
164 {
165 botIdParam = "botid=" + botId + "&";
166 }
167
168 final URL url = new URL (
169 codeBase,
170 "/programd/GetBotResponse?"
171 + botIdParam
172 + "input=" + URLEncoder.encode ( text, "UTF-8" ) );
173
174 final HttpURLConnection httpURLConnection
175 = ( HttpURLConnection ) url.openConnection ( );
176
177 final InputStream inputStream
178 = httpURLConnection.getInputStream ( );
179
180 final String content = StreamLib.toString ( inputStream );
181
182 inputStream.close ( );
183
184 modelQueue.offer ( new ClientMessage (
185 ClientMessage.Type.TEXT_EVENT,
186 "bot: " + content.toString ( ) ) );
187
188 modelQueue.offer ( new ClientMessage (
189 ClientMessage.Type.SPEECH_EVENT,
190 content.toString ( ) ) );
191 }
192 catch ( final Exception ex )
193 {
194 ex.printStackTrace ( );
195
196 modelQueue.offer ( new ClientMessage (
197 ClientMessage.Type.TEXT_EVENT, ex.getMessage ( ) ) );
198 }
199 }
200
201 //////////////////////////////////////////////////////////////////////
202 //////////////////////////////////////////////////////////////////////
203 }