package yoonforh.generic; /* * Copyright (c) 1996-1998 Yoon Kyung Koo. All Rights Reserved. * * HelloCounter.java (GenericClient version) * * Created at 04/18/96 * Version 1.25b (for JDK 1.0.2) * VERSION HISTORY * Updated 06/09/98 * Version 1.25b (for JDK 1.0.2) * fixed a bug of not notifying the kicker thread when wait state * Updated 02/09/98 * Version 1.25a (for JDK 1.0.2) * removed unreliable methods[Thread.stop(), Thread.suspend(), Thread.resume()] * Updated 01/06/98 * Version 1.25 (for JDK 1.0.2) * bug fixed about threads handling * Updated 10/31/97 * Version 1.21 (for JDK 1.0.2) * Added endSession call * Added waiting graph * Updated 10/30/97 * Version 1.2 (for JDK 1.0.2) * Fixed delayed display while connecting to server * Improved cgi URL invoke routine * Updated 10/30/97 * Version 1.1 (for JDK 1.0.2) * Added border line * * ---- HTML parameters ---- * HOST : Server host's name * DATA : data-storing file name * SINCE : mm/dd/yy * DELAY : time to delay (mili-sec) (default=5) * MESSAGE : messages to append * INITDIR : to right or to left (default=LEFT) * CHGDIR : direction change when string ends (default=FALSE) * FGCOLOR : colors to display text e.g) "255, 255, 0" (that is, yellow) * BGCOLOR : colors to display background * SHCOLOR : colors to display text's shade * FONTNAME : font name (TimesRoman(d), Helvetika) * FONTSTYLE : font style (0:PLAIN, 1:BOLD(d), 2:ITALIC, 3:BOLD|ITALIC) * FONTSIZE : font size (default = 15) * CGIURL : cgi URL to invoke server process if not responding * SOUND : audio files (delimeter '|') * */ import java.io.*; import java.awt.*; import java.net.*; import java.util.StringTokenizer; import java.util.Hashtable; import java.util.Enumeration; import java.applet.AudioClip; public class HelloCounter extends java.applet.Applet implements Runnable { private final int nServerPort = 6561; private Thread kicker=null; private boolean threadSuspended = false; private boolean initialized = false; private Graphics gbuffer; private Image offscreen; private Color fgColor; private Color bgColor; private Color shColor; private boolean bChangeDir = false; private boolean bRightDir = false; private int dir=-1; private int pos_x = 0; private int pos_y; private String displayString= "Connecting to the counter server..."; private int strlen=0; private FontMetrics fm; private Dimension bound; private int delay; private int strheight, strbaseline; // used by loader thread private int loadPerCent = 0; private Thread loader=null; // play audioclip private AudioClip audio; private String sounds; private int audioIndex; public void init() { bound=size(); String fontName=null; // font name int fontStyle=1; // font style int fontSize=15; // font size if (getParameter("CHGDIR")=="TRUE") bChangeDir=true; if (getParameter("INITDIR")=="RIGHT") { /* to right */ bRightDir=true; dir=1; } String color=getParameter("FGCOLOR"); fgColor=StrToColor(color, Color.white); color=getParameter("BGCOLOR"); bgColor=StrToColor(color, Color.blue); color=getParameter("SHCOLOR"); shColor=StrToColor(color, Color.black); if (getParameter("DELAY")==null) // delay in mili-seconds delay=5; else delay=Integer.valueOf(getParameter("DELAY")).intValue(); if ((fontName=getParameter("FONTNAME"))==null) // font name fontName="TimesRoman"; if (getParameter("FONTSTYLE")!=null) // font style fontStyle=Integer.valueOf(getParameter("FONTSTYLE")).intValue(); if (getParameter("FONTSIZE")!=null) // font size fontSize=Integer.valueOf(getParameter("FONTSIZE")).intValue(); Font font=new Font(fontName, fontStyle, fontSize); setFont(font); fm=getFontMetrics(font); int strHeight=fm.getHeight(); int strBaseline=fm.getAscent(); pos_y=(bound.height-strHeight)/2+strBaseline; pos_x=(bound.width-fm.stringWidth(displayString))/2; strlen=fm.stringWidth(displayString); sounds = getParameter("SOUND"); if (sounds == null) { sounds = "harp.au"; } /* at the cost of speed, increase stability */ offscreen=createImage(bound.width, bound.height); gbuffer=offscreen.getGraphics(); gbuffer.setFont(font); gbuffer.setColor(bgColor); gbuffer.fillRect(0,0,bound.width, bound.height); // resize(200,30); } public void paint(Graphics g) { g.drawImage(offscreen,0,0,this); } public void update(Graphics g) { if (!initialized) { int value=bound.width * loadPerCent/100; int steps = value/2; int y=1, height=bound.height-2, cx=0, x=0; gbuffer.setColor(bgColor); gbuffer.fillRect(0,0,bound.width, bound.height); gbuffer.setColor(Color.black); gbuffer.fillRect(x+3,y+3, value, height); gbuffer.setColor(fgColor); for (int i=0; i"+cgiURL.getContentType()); int len=cgiURL.getContentLength(); System.out.println("CONTENT_LEN>"+len); if (len<0) { InputStream is=cgiURL.getInputStream(); b=new byte[1024]; is.read(b,0,1024); } else { BufferedInputStream is=(BufferedInputStream)cgiURL.getContent(); b=new byte[len]; is.read(b); } System.out.println("CONTENT>"); // System.out.println(new String(b)); // this is only supported in JDK 1.1 and above System.out.println("END>"); } catch (Exception e) { System.out.println("Re-run server connection error"+e.toString()); } if (client.init("Counter")!=0) // try again count=0; else count=getCount(client, dataFile); } else count=getCount(client, dataFile); if ((count%100)/10==1) suffix="th"; else { switch(count%10) { case 1: suffix="st";break; case 2: suffix="nd";break; case 3: suffix="rd";break; default : suffix="th"; } } try{ String visitorHostName=InetAddress.getLocalHost().getHostName().toUpperCase(); if (visitorHostName.equals("LOCALHOST")) // depends upon browser's security policy displayString="You are the "+count+suffix+" visitor"+since+msg; else displayString=" Hello, "+visitorHostName+ ". You are the "+count+suffix+" visitor"+since+msg; } catch(UnknownHostException e){ displayString="You are the "+count+suffix+" visitor"+since+msg; } int strHeight=fm.getHeight(); int strBaseline=fm.getAscent(); pos_y=(bound.height-strHeight)/2+strBaseline; pos_x=bound.width/2; //-fm.stringWidth(displayString); strlen=fm.stringWidth(displayString); loader=null; initialized=true; } // end of if Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (kicker == Thread.currentThread() ) { try { Thread.sleep(delay); synchronized(this) { // if suspend state don't update while (threadSuspended) wait(); } } catch (InterruptedException e){} pos_x+=dir; if ((pos_x<=-strlen) || (pos_x>=bound.width)) { if ( bChangeDir ) dir*=-1; else if ( bRightDir ) pos_x=-strlen; else pos_x=bound.width; } repaint(); } } // end of run() public synchronized boolean mouseDown(Event evt, int x, int y) { // bRefresh=true; nextAudio(); if (initialized) { threadSuspended = !threadSuspended; if (!threadSuspended) notify(); } return true; } private Color StrToColor(String str, Color c) { if ( str != null ) { int r,g,b; StringTokenizer t = new StringTokenizer( str, ", "); try { r = Integer.valueOf(t.nextToken()).intValue(); g = Integer.valueOf(t.nextToken()).intValue(); b = Integer.valueOf(t.nextToken()).intValue(); return new Color(r,g,b); } catch ( Exception e ) { return c; } } return c; } private int getCount(GenericClient client, String fileName){ String data = null; try { DataInputStream input = client.getInput(); DataOutputStream output = client.getOutput(); if ( !GenericTool.sendMessage(output, "FILENAME", fileName) ) { System.out.println("send filename error"); return 0; } if ( (data=GenericTool.getMessage(input, "COUNT")).equals(null) ) return 0; } catch(Exception e) { System.err.println(e.toString()); return 0; } finally { client.endSession(); } int count=Integer.parseInt( data ); return (count>0?count:0); } // end of getCount public void nextAudio() { try { if (audio != null) { audio.stop(); audio = null; } String url = sounds; if (sounds.indexOf('|') >= 0) { int start = audioIndex; if ((audioIndex = sounds.indexOf('|', audioIndex)) < 0) { url = sounds.substring(start); audioIndex = start; } else { url = sounds.substring(start, audioIndex++); } } if (url.length() > 0) { audio = getAudioClip(new URL(getCodeBase(), url)); audio.play(); } } catch(Exception e) { } } }