[comp.windows.news] NeWS protocol tracer

sjs@spectral.ctt.bellcore.com (Stan Switzer) (10/27/89)

I threw this together one evening when I was trying to figure out what
tagprint really does (the manual is none too clear).

It's a dreadfully inefficient NeWS program which interposes itself
between a client and the server and traces the protocol to a file.  I
think someone (Josh Siegel?) posted one a long time ago but I couldn't
find it.

Anyway, it can be mighty useful, and it's a nice example of playing
socket games in NeWS.

Stan  sjs@bellcore.com   "No X is good X"

sjs@spectral.ctt.bellcore.com (Stan Switzer) (10/27/89)

In a previous article, I wrote:
> I threw this together one evening when I was trying to figure out what
> tagprint really does (the manual is none too clear).
> 
> It's a dreadfully inefficient NeWS program which interposes itself
> between a client and the server and traces the protocol to a file.

It would have helped if I had included the program.

> Stan  sjs@bellcore.com   "No X is good X"

Yeah, whatever.

--------------
#!/bin/sh
#
# tracenews: trace the NeWS client-server protocol
#
# Copyright (C) 1989 by Stan Switzer. All rights reserved.
# This program is provided for unrestricted use, provided that this 
# copyright message is preserved. There is no warranty, and no author 
# or distributer accepts responsibility for any damage caused by this 
# program.
#
#	tracenews file cmd ...
#
# Stan Switzer   sjs@bellcore.com

TRACEFILE=`echo $1`	# expand ~, if present
shift

case "$TRACEFILE" in
	/*	) ;;
	*	) TRACEFILE=`pwd`/$TRACEFILE;;
esac

cat <<!! | psh | { read NEWSSERVER; export NEWSSERVER; ("$@"); (psh) </dev/null >/dev/null 2>/dev/null; }

/tracefile ($TRACEFILE) (w) file def
% systemdict /NextSocket known not { systemdict /NextSocket 2011 put } if

/ServerSocket systemdict begin
    % NextSocket /NextSocket NextSocket 1 add def
    7469 % why not?
end def
/FakeServer (NEWSSERVER) getenv
  (;) search pop exch pop (.) search pop exch pop exch % sys addr port
  pop ServerSocket 20 string cvs
  (.) exch append append exch (;) exch append append def

clear (%socketl) //ServerSocket 20 string cvs append (r) file
% accept one connection:
{ FakeServer (\n) append print } fork pop
acceptconnection
/user exch def
/server (NEWSSERVER) getenv (;) search pop exch pop (.) search
pop pop append exch append def
/server (%socketc) server append (rw) file def
/mode (X) def
{ % writer process
    { % writer loop
	server (X) readstring not { exit } if
	dup user exch writestring user flushfile
	tracefile mode (S) ne {
	    dup (\nServer: ) writestring /mode (S) def
	} if
	exch writestring 
    } loop
    currentprocess killprocessgroup
} fork pop
{ % reader process
    { % reader loop
	user (X) readstring not { exit } if
	dup server exch writestring server flushfile
	tracefile mode (U) ne {
	    dup (\nUser: ) writestring /mode (U) def
	} if
	exch writestring
    } loop
    currentprocess killprocessgroup
} fork pop
{ % flusher process
    createevent
    dup /Name /Tick put
    dup expressinterest createevent copy
    { % flusher loop
	dup /TimeStamp currenttime 5 60 div add put
	sendevent
	awaitevent
	tracefile flushfile
    } loop
} fork

!!