/* * Copyright (c) 1996-1998 Yoon Kyung Koo. All Rights Reserved. * * HelloCounter.java (CGI call version) * * Created at 04/18/96 * Version 1.30c (for JDK 1.0.2) * VERSION HISTORY * Updated 02/12/2000 * Version 1.30c (for JDK 1.0.2) * add GETHOSTCGI parameter * Updated 09/18/98 * Version 1.30b (for JDK 1.0.2) * in stop() method, set kicker null before notify * Updated 06/09/98 * Version 1.30a (for JDK 1.0.2) * fixed a bug of not notifying the kicker thread when wait state * Updated 03/12/98 * Version 1.30 (for JDK 1.0.2) * revised into CGI call version * 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 ---- * SINCE : mm/dd/yy * GETHOSTCGI : cgi URL to get host address * COUNTERCGI : cgi URL to get count data * 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) * SOUND : audio files (delimeter '|') * */ import java.io.*; import java.awt.*; import java.net.*; import java.util.*; import java.applet.AudioClip; public class HelloCounter extends java.applet.Applet implements Runnable { 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"+conn.getContentType()); int len = conn.getContentLength(); System.out.println("CONTENT_LEN>"+len); if (len < 0) { InputStream is = conn.getInputStream(); b = new byte[100]; is.read(b); } else { BufferedInputStream is = (BufferedInputStream) conn.getContent(); b = new byte[len]; is.read(b); } System.out.println("CONTENT>"); String data=new String(b, 0).trim(); // trim to remove white spaces System.out.print("DATA>"+data); System.out.println(" length="+data.length()); System.out.println("END>"); return data; } /** * parse host name or address from contents */ private String parseHostName(String data) { StringBufferInputStream is = new StringBufferInputStream(data); Properties prop = new Properties(); try { prop.load(is); } catch (IOException ie) { System.out.println("cannot load properties : " + ie); } String name = prop.getProperty("REMOTE_HOST"); if (name == null || name.length() == 0) name = prop.getProperty("REMOTE_ADDR"); prop.list(System.out); System.out.println("name is " + name); return name; } public void run() { if ( loader==Thread.currentThread() ) { while(!initialized && loader==Thread.currentThread() ) { try {Thread.sleep(delay*3);} /* let loader run slowly */ catch (InterruptedException e){} loadPerCent+=1; loadPerCent%=100; repaint(); } return; } if (!initialized) { // prevent counter increase when browser backed or forwarded int count=0; String suffix="th"; String since=null; String msg=null; repaint(); // getting Parameters if ((since=getParameter("SINCE"))==null) since=". "; else since=" since "+since +". "; if ((msg=getParameter("MESSAGE"))==null) msg="My name is Yoon Kyung Koo. Welcome to this page."; try { String cgi = getParameter("COUNTERCGI"); if (cgi != null) { String data = getContentsFromURL(new URL(cgi)); count=Integer.parseInt(data); if (count <= 0) count=0; } else count = 0; } catch (IOException ie) { System.out.println("get count data error" + ie); count=0; } 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"; } } String visitorHostName = null; try{ visitorHostName = InetAddress.getLocalHost().getHostName().toUpperCase(); if (visitorHostName.equals("LOCALHOST")) { // depends upon browser's security policy try { String cgi = getParameter("GETHOSTCGI"); if (cgi != null) { String data = getContentsFromURL(new URL(cgi)); visitorHostName = parseHostName(data); } else visitorHostName = null; } catch (IOException ie) { System.out.println("get host name error" + ie); visitorHostName = null; } } } catch(UnknownHostException e){ displayString="You are the "+count+suffix+" visitor"+since+msg; } if (visitorHostName == null) displayString="You are the "+count+suffix+" visitor"+since+msg; else displayString=" Hello, "+visitorHostName+ ". 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; } 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) { } } }