View Javadoc

1   /*
2     The TauP Toolkit: Flexible Seismic Travel-Time and Raypath Utilities.
3     Copyright (C) 1998-2000 University of South Carolina
4   
5     This program is free software; you can redistribute it and/or
6     modify it under the terms of the GNU General Public License
7     as published by the Free Software Foundation; either version 2
8     of the License, or (at your option) any later version.
9   
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14  
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  
19    The current version can be found at 
20    <A HREF="www.seis.sc.edu">http://www.seis.sc.edu</A>
21  
22    Bug reports and comments should be directed to 
23    H. Philip Crotwell, crotwell@seis.sc.edu or
24    Tom Owens, owens@seis.sc.edu
25  
26  */
27  
28  package edu.sc.seis.TauP;
29  
30  import java.io.BufferedReader;
31  import java.io.DataOutputStream;
32  import java.io.FileNotFoundException;
33  import java.io.IOException;
34  import java.io.InputStreamReader;
35  import java.io.InvalidClassException;
36  import java.io.OptionalDataException;
37  import java.io.StreamCorruptedException;
38  import java.lang.reflect.Constructor;
39  import java.lang.reflect.InvocationTargetException;
40  import java.net.ServerSocket;
41  import java.net.Socket;
42  import java.util.StringTokenizer;
43  
44  /*** Daemon for travel time calculations. Listens to port 6371 by 
45    * default and returns travel time calculations in response to 
46    * client requests. 
47    *
48    * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
49  
50  
51  
52    * @author H. Philip Crotwell
53    *
54    */
55  public class TauPDaemon extends Thread {
56  
57  	Socket theConnection;
58  
59  	protected static TauModel[] tauModels = new TauModel[10];
60  	
61  	public TauPDaemon(Socket s) {
62  		theConnection = s;
63  	}
64  	
65  		/*** loads a TauModel, after checking a cache to see if it is already
66  		 *  loaded. 
67  		 *  @throws FileNotFoundException if file not found
68  		 *  @InvalidClassException if class invalid
69  		 *  @throws OptionalDataException if...
70  		 *  @StreamCorruptedException if...
71  		 *  @ClassNotFoundException if...
72  		 *  @IOException if...
73  		 */
74  	static TauModel getTauModel(String modelName) 
75  	throws FileNotFoundException,
76  	InvalidClassException,
77  	OptionalDataException,
78  	StreamCorruptedException,
79  	ClassNotFoundException ,
80  	IOException
81  	{
82  		for (int i=0;i<tauModels.length;i++) {
83  			if (tauModels[i] != null && 
84  			tauModels[i].getModelName().equals(modelName)) {
85  				TauModel tMod = tauModels[i];
86  				for (int j= i; j> 0; j--) {
87  		         tauModels[j] = tauModels[j-1];
88  				}
89  				tauModels[0] = tMod;
90  				return tMod;
91  			}
92  		}
93  			// not already loaded, so see if we can find it
94  		TauModel tMod = TauModelLoader.load(modelName, 
95  			System.getProperty("taup.model.path"));
96  			// add it to front, so older models fall off the end
97  		for (int j= tauModels.length-1; j> 0; j--) {
98  			tauModels[j] = tauModels[j-1];
99  		}
100 		tauModels[0] = tMod;
101 		return tMod;
102 	}
103 	
104 	public static void main(String[] args) {
105 		int thePort;
106 		ServerSocket server;
107 		
108 			// set port to listen to
109 		try {
110 			thePort = Integer.parseInt(args[0]);
111 			if (thePort < 0 || thePort > 65535) thePort = 6371;
112 		} catch (Exception e) {
113 			thePort = 6371;
114 		}
115 		
116 			// load some more common earth models
117 		String[] modelnames = {"ak135" , "prem" , "iasp91"};
118 		TauModel tempTMod;
119 		for (int i=0; i<modelnames.length;i++) {
120 			try {
121 				tempTMod = getTauModel(modelnames[i]);
122 			} catch (ClassNotFoundException e) {
123 				System.err.println("Couldn't load tau model. "+e.getMessage());
124 			} catch (InvalidClassException e) {
125 				System.err.println("Couldn't load tau model. "+e.getMessage());
126 			} catch (StreamCorruptedException e) {
127 				System.err.println("Couldn't load tau model. "+e.getMessage());
128 			} catch (FileNotFoundException e) {
129 				System.err.println("Couldn't load tau model. "+e.getMessage());
130 			} catch (OptionalDataException e) {
131 				System.err.println("Couldn't load tau model. "+e.getMessage());
132 			} catch (IOException e) {
133 				System.err.println("Couldn't load tau model. "+e.getMessage());
134 			}
135 		}
136 		
137 		try {
138 			server = new ServerSocket(thePort);
139 			System.out.println("Accepting connections on port "+server.getLocalPort());
140 			while (true) {
141 				TauPDaemon taupd = new TauPDaemon(server.accept());
142 				taupd.start();
143 			}
144 		} catch (IOException e) {
145 			System.err.println("Server aborted prematurely. "+e.getMessage());
146 		}
147 	}
148 	
149 	public void run() {
150 		try {
151 			DataOutputStream out = new DataOutputStream(theConnection.getOutputStream());
152 			BufferedReader in = new BufferedReader(
153 				new InputStreamReader(theConnection.getInputStream()));
154 			
155 			out.writeBytes("Welcome to TauP Travel Time Daemon, version 0.92\n");
156 
157 			String modelName="iasp91", phaseString = "P,S,PKP,SKS,PKIKP,SKIKS";
158 			double distance, depth=0.0;
159 			TauModel tMod = null;
160 			String toolString = "TauP_Time";
161 			TauP_Time tool = null;
162 			
163 			String line, word;
164 			StringTokenizer tokenIn;
165 			while ((line = in.readLine()) != null) {
166 				tokenIn = new StringTokenizer(line);
167 				word = tokenIn.nextToken();
168 				if (word.equals("MODEL")) {
169 					try {
170 						String tempModelName = tokenIn.nextToken();
171 						tMod = getTauModel(tempModelName);
172 						modelName = tempModelName;
173 					} catch (ClassNotFoundException e) {
174 						System.err.println("Couldn't load tau model. "+e.getMessage());
175 					}
176 				} else if (word.equals("TOOL")) {
177 					toolString = tokenIn.nextToken();
178 				} else if (word.equals("PHASES")) {
179 					phaseString = tokenIn.nextToken();
180 				} else if (word.equals("DEPTH")) {
181 					depth = Double.valueOf(tokenIn.nextToken()).doubleValue();
182 				} else if (word.equals("DISTANCE")) {
183 					distance = Double.valueOf(tokenIn.nextToken()).doubleValue();
184 					if (tMod == null) {
185 						try {
186 							tMod = getTauModel(modelName);
187 						} catch (ClassNotFoundException e) {
188 							System.err.println("Couldn't load tau model. "+e.getMessage());
189 						}
190 					}
191 					if (tool == null || 
192 					! tool.getClass().getName().endsWith(toolString)) {
193 						tool = loadTool(toolString, tMod);
194 						tool.depthCorrect(depth);
195 					}
196 					if (! tMod.equals(tool.getTauModel())) {
197 						tool.setTauModel(tMod);
198 System.out.println("Changing tmods");
199 					}
200 					if (phaseString != tool.getPhaseNameString()) {
201 						tool.parsePhaseList(phaseString);
202 						tool.depthCorrect(depth);
203 						phaseString = tool.getPhaseNameString();
204 					}
205 					tool.calculate(distance);
206 					tool.printResult(out);
207 				
208 				}
209 			}
210 		} catch (IOException e) {
211 			System.err.println(e.getMessage());
212 		} catch (TauModelException e) {
213 			System.err.println(e.getMessage());
214 		}
215 	}
216 
217 		/*** attempts to create a new tool based on the toolName. 
218 		 *
219 		 *  @returns a subclass of TauP_Time, or null if unsucessful.
220 		 */
221 	TauP_Time loadTool(String toolName, TauModel tMod) {
222 		try {
223 
224 				// create an instance of the tool class
225 			Class toolClass = Class.forName("edu.sc.seis.TauP."+toolName);
226 				// invoke the constructor with single arguement (TauModel)
227 			Class[] argClasses = { Class.forName("edu.sc.seis.TauP.TauModel") };
228 			Object[] argObjects = { tMod };
229 			Constructor toolConstructor = toolClass.getConstructor(argClasses);
230 			TauP_Time tool = (TauP_Time)toolConstructor.newInstance(argObjects);
231 			return tool;
232 		} catch (ClassNotFoundException ex) {
233 		} catch (InstantiationException ex) {
234 		} catch (IllegalAccessException ex) {
235 		} catch (InvocationTargetException ex) {
236 		} catch (NoSuchMethodException ex) {
237 		}
238 		return null;
239 	}
240 }