1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
94 TauModel tMod = TauModelLoader.load(modelName,
95 System.getProperty("taup.model.path"));
96
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
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
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
225 Class toolClass = Class.forName("edu.sc.seis.TauP."+toolName);
226
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 }