package yoonforh.generic; /* * Copyright (c) 1996 Yoon Kyung Koo. All Rights Reserved. * * CounterWork.java * Author: Yoon Kyung Koo (yoonforh@interpia.net, http://interpia.net/~yoonforh) * Creation Date: 10/24/96 * Version: 1.00 * VERSION HISTORY * Update: 10/27/97 * Version: 1.00 (for JDK 1.02 API) * Patched file deadlock problem * Update: 10/25/96 * Version: 0.99 (for JDK 1.02 API) */ // import java.net.*; import java.io.*; class CounterWork implements GenericWork { private final String DATAPATH="/home1/yoonforh/public_html/counter/data/"; // ÀÌ°ÍÀº °¢ÀÚ °íÀ¯ÇÑ µð·ºÅ丮À̸§ÀÔ´Ï´Ù. Ä«¿îÅÍ ÆÄÀÏÀ» ÀúÀåÇÏ´Â µð·ºÅ丮À̹ǷΠ°¢ ½Ã½ºÅÛº°·Î ¸Â°Ô °íÃÄ¾ß ÇÕ´Ï´Ù CounterWork() {} public synchronized void work (DataInputStream input, DataOutputStream output) { RandomAccessFile file=null; String filename=null; int count; byte b[] = new byte[128]; // ½ÇÁ¦ ¼­¹ö°¡ ÇÏ´Â ÀϵéÀÌ ¿©±â¿¡ µé¾î°£´Ù. ÀÌ ÀÏÀ» ¼öÇàÇÑ ÈÄ ¼ÒÄÏÀ» Á¾·áÇÑ´Ù. if ( (filename=GenericTool.getMessage( input, "FILENAME" )) == null ) { GenericServer.log.println("data filename receive error"); return; } GenericServer.log.println("Filename message received from client: " + filename); filename=DATAPATH+filename; try { file=new RandomAccessFile(new File(filename), "rw"); } catch (IOException e) { GenericServer.log.println("File Open Error:"+e); return; } try { // to close finally try { file.seek(0); // set file pointer to beginning } catch (IOException e) { GenericServer.log.println("File Seek Error:"+e); return; } try { count=file.readInt(); } catch (IOException e) { try { file.writeInt(0); } catch (IOException ex) { GenericServer.log.println("File Write Error:"+ex); return; } count=0; GenericServer.log.println(filename+":count=0"); } count++; GenericServer.log.println(filename+":count="+count); try { file.seek(0); // set file pointer to beginning } catch (IOException e) { GenericServer.log.println("File Seek Error"); return; } try { file.writeInt(count); } catch (IOException e) { GenericServer.log.println("File Write Error:"+e); return; } } finally { try { file.close(); } catch (IOException ie) { GenericServer.log.println("File Close Error:"+ie); return; } } // end of try-finally GenericTool.sendMessage(output, "COUNT", String.valueOf(count)); } // end work() } // end class CounterWork