#!/bin/sh
# \
exec udpwish $0 $*
# RCS: $Id: udptest,v 1.1.1.1 1997/05/15 21:02:59 millerms Exp $
# AUTHOR Michael Miller msmiller@acm.org
#

global RHOST RPORT RMESSAGE ID PORT
set ID ""

proc udp_put {} {
	global ID
	.r configure  -text "[udp_recv $ID]"
}

proc next_bind {c n} {
	bind $c <Tab> "focus $n"
	bind $c <Return> "focus $n"
}

proc ui {} {
	pack [frame .o]
	label .o.l -text Port 
	entry .o.e -textvariable PORT 
	.o.e insert end [pid]
	next_bind .o.e .s.h.e
	button .o.b -text Open -command {
		global PORT ID
		set ID [udp_open $PORT]
		fileevent $ID readable udp_put
		.o.b configure -state disabled
	}
	pack .o.l .o.e .o.b -side left
	focus .o.e

	pack [frame .s]
	pack [frame .s.h]
	label .s.h.l -width 12 -text "Remote Host" 
	entry .s.h.e -textvariable RHOST
	.s.h.e insert end localhost
	next_bind .s.h.e .s.p.e
	pack .s.h.l .s.h.e -side left
	pack [frame .s.p]
	label .s.p.l -width 12 -text "Remote Port" 
	entry .s.p.e -textvariable RPORT
	.s.p.e insert end [pid]
	next_bind .s.p.e .s.m.e
	pack .s.p.l .s.p.e -side left
	pack [frame .s.m]
	label .s.m.l -width 12 -text "Message" 
	entry .s.m.e -textvariable RMESSAGE
	.s.m.e insert end test-[pid]
	bind .s.m.e <Return> ".s.b.b invoke"
	bind .s.m.e <Tab> "focus .o.e"
	pack .s.m.l .s.m.e -side left

	pack [frame .s.b]
	button .s.b.b -text Send -command {
		global RHOST RPORT RMESSAGE ID
		udp_configure $ID $RHOST $RPORT
		udp_send $ID "$RMESSAGE"
	}
	button .s.b.q -text Quit -command {
		global ID
		if {$ID != ""} {
			close $ID
		}
		exit
	}
	pack .s.b.b .s.b.q -side left -expand 1 -fill x

	pack [label .r -relief groove] -expand 1 -fill x -anchor s
}
ui
