Project

General

Profile

« Previous | Next » 

Revision 651

Added by berkley almost 24 years ago

fixed error handling and time out issues. Also fixed thread syncronization problem

View differences:

src/edu/ucsb/nceas/metacat/DataFileServer.java
39 39
  }
40 40
  
41 41
  /**
42
   * attempts to connect a socket, returns null if it is not successful
43
   * returns the connected socket if it is successful.
44
   */
45
  public static Socket getSocket(String host, int port)
46
  {
47
    Socket s = null;
48
    try
49
    {
50
      s = new Socket(host, port);
51
      //we could create a socket on this port so the port is not available
52
      //System.out.println("socket connnected");
53
      return s;
54
    }
55
    catch(UnknownHostException u)
56
    {
57
    }
58
    catch(IOException i)
59
    {
60
      //an ioexception is thrown if the port is not in use
61
      //System.out.println("socket not connected");
62
      return s;
63
    }
64
    return s;
65
  }
66
  
67
  /**
42 68
   * returns true if the port specified is not in use.  false otherwise
43 69
   */
44 70
  public static boolean portIsAvailable(int port)
......
48 74
    try
49 75
    {
50 76
      s = new Socket(host, port);
51
      s.close();
77
      //s.close();
52 78
      //we could create a socket on this port so the port is not available
79
      //System.out.println("socket not available");
53 80
      return false;
54 81
    }
55 82
    catch(UnknownHostException u)
......
59 86
    catch(IOException i)
60 87
    {
61 88
      //an ioexception is thrown if the port is not in use
62
      //System.out.println("port not in use");
89
      //System.out.println("socket available");
63 90
      return true;
64 91
    }
65 92
    return true;
......
77 104
    java.util.Date localtime = new java.util.Date();
78 105
    String dateString = formatter.format(localtime);
79 106
    
80
    String sqlDateString = "to_date('" + dateString + "', 'YY-MM-DD HH:MI:SS')";
107
    String sqlDateString = "to_date('" + dateString + "', 'YY-MM-DD HH24:MI:SS')";
81 108
    
82 109
    StringBuffer sql = new StringBuffer();
83 110
    sql.append("insert into xml_documents (docid, docname, doctype, ");
......
116 143
      //System.out.println("Waiting for sess_id: " + sess_id);
117 144
      server = new ServerSocket((new Integer(port)).intValue());
118 145
      //System.out.println("Waiting");
146
      server.setSoTimeout(30000); //set a 30 second timeout
119 147
      client = server.accept();
120 148
      //System.out.println("Accepted from " + client.getInetAddress());
121 149
    
......
157 185
      }
158 186
      else
159 187
      {
160
        System.out.println("User authenticated on port " + port);
188
        //System.out.println("User authenticated on port " + port);
161 189
      }
162 190
      
163 191
      String restext=null;
......
165 193
      //System.out.println("outfile: " + filedir + filename);
166 194
      boolean nameInUse = outfile.exists();
167 195
      int filenametemp = 1;
196
      String fn = filename;
168 197
      while(nameInUse)
169 198
      {
170
        filename += filenametemp;
199
        filename = fn + filenametemp;
171 200
        outfile = new File (filedir + filename);
172 201
        nameInUse = outfile.exists();
173 202
        filenametemp++;
......
201 230
        e.printStackTrace(System.out);
202 231
      }
203 232
    } 
233
    catch(InterruptedIOException iioe)
234
    {
235
      //the accept timeout passed
236
      //System.out.println("socket on port " + port + " timed out.");
237
    }
204 238
    catch (IOException ex) 
205 239
    {
206 240
      ex.printStackTrace();
......
209 243
    {
210 244
      try
211 245
      {
246
        server.close();
247
        //System.out.println("server socket closed");
212 248
        client.close();
213
        System.out.println("client socket closed");
214
        server.close();
215
        System.out.println("server socket closed");
249
        //System.out.println("client socket closed");
216 250
      } 
217 251
      catch (IOException ex) 
218 252
      {
219 253
        ex.printStackTrace();
220 254
      }
221
      System.out.println("done");
255
      //System.out.println("done");
222 256
    }
223 257
  }
224 258
  
src/edu/ucsb/nceas/metacat/DataStreamTest.java
45 45
                       filename + " cookie: " + cookie);
46 46
    try 
47 47
    {
48
      //while(DataFileServer.portIsAvailable(port)) 
49
      //{//loop until the port is there
50
      //}
51
      echoSocket = new Socket(host, port);
48
      echoSocket = DataFileServer.getSocket(host, port);
49
      while(echoSocket == null) 
50
      {//loop until the port is there
51
        echoSocket = DataFileServer.getSocket(host, port);
52
      }
53
      //echoSocket = new Socket(host, port);
52 54
      out = echoSocket.getOutputStream();
53 55
      in = echoSocket.getInputStream();
54 56
    } 
......
160 162
      //the port number taken from the xml encoding
161 163
      temp2 = temp2.trim();
162 164
      int port = (new Integer(temp2)).intValue();
165
      //while(portIsAvailable(port)) {int x = 1;}
163 166
      int index = cookie.indexOf("JSESSIONID=");
164 167
      index = cookie.indexOf("=", index);
165 168
      int index2 = cookie.indexOf(";", index);
166 169
      //get just the session id information from the cookie
167 170
      cookie = cookie.substring(index+1, index2);
168 171
      
169
      //this for loop is a hack to give the servlet enough time to start
170
      //the socket thread.  Sometimes it doesn't work
171
      //for(int j=0; j<100000000; j++) { int x = 1; }
172
      
173 172
      SendFile(filename, host, port, cookie);
174 173
    }
175 174
    catch(Exception e)

Also available in: Unified diff