Project

General

Profile

« Previous | Next » 

Revision 653

Added by berkley over 23 years ago

added support for the server to return the docid of the newly added data file. Also, now the data stream must be prefixed with the following information: filename <0> sessionid <0> filesize <0>. where the <0> symbol is a byte containing zero. The filesize is sent as an int and the filename and sessionid are sent as strings (in byte form).

View differences:

DataFileServer.java
137 137
  {
138 138
    ServerSocket server = null;
139 139
    Socket client = null;
140
    OutputStreamWriter osw = null;
140 141
    try
141 142
    {
142 143
      //System.out.println("Starting on port " + port);
......
147 148
      client = server.accept();
148 149
      //System.out.println("Accepted from " + client.getInetAddress());
149 150
    
150
      InputStream in = client.getInputStream();
151
      OutputStreamWriter osw = new OutputStreamWriter(client.getOutputStream());
151
      InputStream in = client.getInputStream();      //in from the client
152
      OutputStream sout = client.getOutputStream();  //out to the client
153
      osw = new OutputStreamWriter(sout);
152 154
      //System.out.println("output stream received");
153 155
      // first read to get the file name
154 156
      byte[] str = new byte[1024];
155 157
      int val = -1;
156 158
      int i = 0;
157 159
      //get the filename
158
      while (val!=0) 
160
      while (val != 0) 
159 161
      {
160 162
        //System.out.println("i: " + i);
161 163
        val = in.read();
......
169 171
      i = 0;
170 172
      str = new byte[1024];
171 173
      //get the session id
172
      while (val!=0) 
174
      while (val != 0) 
173 175
      {
174 176
        val = in.read();
175 177
        str[i] = (byte)val;
176 178
        i++;
177 179
      }
178 180
      String session_id = (new String(str,0, i-1)).trim();
179
      //System.out.println("session_id: " + session_id);
180 181
      
182
      //get the length of the file in bytes
183
      int val2 = -1;
184
      long fsize = 0;
185
      while(val2 != 0)
186
      {
187
        val2 = in.read();
188
        if(val2 != 0)
189
        {
190
          fsize = val2;
191
        }
192
      }
193
      //System.out.println("file size: " + fsize);
194
      
181 195
      if(!sess_id.equals(session_id))
182 196
      {//this is an unauthorized connection to this port
183 197
        System.out.println("unauthorized request");
......
208 222
        String accnum = anum.generate(null, "INSERT");
209 223
        //osw.write(restext,0,restext.length());
210 224
        //osw.flush();
225
        
211 226
        FileOutputStream out = new FileOutputStream(outfile);
212 227
        byte[] buf = new byte[1024];
213 228
        int cnt = 0;
214
        while (cnt!=-1) 
229
        int j = 0;
230
        while (j < fsize) 
215 231
        {
232
          //System.out.println("1 j:" + j);
216 233
          cnt = in.read(buf, 0, 1024);
217 234
          if (cnt!=-1) 
218 235
          {
219 236
            out.write(buf, 0, cnt);
220 237
          }
238
          j += cnt;
221 239
        }
240

  
222 241
        out.flush();
223 242
        out.close();
224 243
        //put the new document info into the DB
225 244
        updateDB(accnum, filename, user);
245
        //System.out.println("sending new docid: " + accnum);
246
        osw.write("<?xml version=\"1.0\"?><docid>" + accnum + "</docid>");
247
        osw.flush();
248
        osw.close();
249
        //System.out.println("docid sent");
226 250
      }
227 251
      catch(Exception e)
228 252
      {
229 253
        System.out.println("error in DataFileServer.run(): " + e.getMessage());
254
        try
255
        {
256
          osw.write("<?xml version=\"1.0\"?><error>" + e.getMessage() + 
257
                    "</error>");
258
          osw.flush();
259
          osw.close();
260
        }
261
        catch(Exception ee)
262
        {
263
          System.out.println("error: " + ee.getMessage());
264
        }
230 265
        e.printStackTrace(System.out);
231 266
      }
232 267
    } 
......
238 273
    catch (IOException ex) 
239 274
    {
240 275
      ex.printStackTrace();
276
      try
277
      {
278
        osw.write("<?xml version=\"1.0\"?><error>" + ex.getMessage() + 
279
                  "</error>");
280
        osw.flush();
281
        osw.close();
282
      }
283
      catch(Exception ee)
284
      {
285
        System.out.println("error: " + ee.getMessage());
286
      }
241 287
    } 
242 288
    finally 
243 289
    {

Also available in: Unified diff