Exemplo n.º 1
0
def client():
	if len(sys.argv) < 4:
		usage()
	count = int(eval(sys.argv[2]))
	host = sys.argv[3]
	if len(sys.argv) > 4:
		port = eval(sys.argv[4])
	else:
		port = MY_PORT
	testdata = 'x' * (BUFSIZE-1) + '\n'
	t1 = time.millitimer()
	s = socket(AF_INET, SOCK_STREAM)
	t2 = time.millitimer()
	s.connect(host, port)
	t3 = time.millitimer()
	i = 0
	while i < count:
		i = i+1
		s.send(testdata)
	s.shutdown(1) # Send EOF
	t4 = time.millitimer()
	data = s.recv(BUFSIZE)
	t5 = time.millitimer()
	print data
	print 'Raw timers:', t1, t2, t3, t4, t5
	print 'Intervals:', t2-t1, t3-t2, t4-t3, t5-t4
	print 'Total:', t5-t1
	print 'Throughput:', int(float(BUFSIZE*count) / float(t5-t1)),
	print 'K/sec.'
Exemplo n.º 2
0
def main():
	filename = 'film2.video'
	if sys.argv[1:]: filename = sys.argv[1]
	f = open(filename, 'r')

	line = f.readline()
	w, h = eval(line[:-1])
	w2, h2 = w/2, h/2
	size = w2 * h2

	data = data2 = t = t0 = t1 = None
	nframes = 0
	t0 = millitimer()
	while 1:
		line = f.readline()
		if not line: break
		t = eval(line[:-1])
		data = None
		data = f.read(size)
		if len(data) <> size:
			raise EOFError
		dostat(w2, h2, data)
		nframes = nframes+1
	t1 = millitimer()
	
	t = 0.001 * (t1-t0)
	fps = 0.1 * int(10*nframes/t)
	print nframes, 'frames in', t, 'sec. =', fps, 'frames/sec.'
Exemplo n.º 3
0
def test_make_tags():
    import time
    f = try_open(TESTFILE)
    t1 = time.millitimer()
    tags = make_tags(f)
    t2 = time.millitimer()
    print 'Making tag table for', ` TESTFILE `, 'took', t2 - t1, 'msec.'
Exemplo n.º 4
0
def test_make_tags():
	import time
	f = try_open(TESTFILE)
	t1 = time.millitimer()
	tags = make_tags(f)
	t2 = time.millitimer()
	print 'Making tag table for', `TESTFILE`, 'took', t2-t1, 'msec.'
Exemplo n.º 5
0
def main():
	s = socket(AF_INET, SOCK_DGRAM)
	s.bind('', PORT)

	foreground()
	wid = winopen('tv')
	RGBmode()
	gconfig()
	qdevice(ESCKEY)

	oldw, oldh = getsize()
	ortho2(0, oldw, 0, oldh)
	testimage()

	t1 = millitimer()

	while 1:
		if qtest():
			dev, val = qread()
			if dev == ESCKEY:
				winclose(wid)
				return
			elif dev == REDRAW:
				oldw, oldh = reshape()
		elif s.avail():
			data = s.recv(17000)
			header = string.strip(data[:HS])
			w, h, pf, x1, y1, x2, y2 = eval(header)
			if (w, h) <> (oldw, oldh):
				x, y = getorigin()
				x, y = x-1, y+21 # TWM correction
				winposition(x, x+w-1, y+oldh-h, y+oldh-1)
				oldw, oldh = reshape()
			data2 = data[HS:]
			dx = (x2-x1+1)/pf
			dy = (y2-y1+1)/pf
			data3 = unpackrect(dx, dy, 1, data2)
			rectzoom(pf, pf)
			lrectwrite(x1, y1, x1+dx-1, y1+dy-1, data3)
			t1 = millitimer()
		else:
			t2 = millitimer()
			if t2-t1 >= 5000:
				testimage()
				t1 = t2
			else:
				millisleep(10)

	winclose(wid)
	return data
Exemplo n.º 6
0
def test():
    import time, __builtin__
    print range(10), range(-10, 10), range(0, 10, 2)
    for i in range(100, -100, -10):
        print i,
    print
    t1 = time.millitimer()
    for i in range(1000):
        pass
    t2 = time.millitimer()
    for i in __builtin__.range(1000):
        pass
    t3 = time.millitimer()
    print t2 - t1, 'msec (class)'
    print t3 - t2, 'msec (built-in)'
Exemplo n.º 7
0
Arquivo: flp.py Projeto: 8Banana/py1.0
def test():
    import time
    t0 = time.millitimer()
    if len(sys.argv) == 2:
        forms = parse_forms(sys.argv[1])
        t1 = time.millitimer()
        print 'parse time:', 0.001 * (t1 - t0), 'sec.'
        keys = forms.keys()
        keys.sort()
        for i in keys:
            _printform(forms[i])
    elif len(sys.argv) == 3:
        form = parse_form(sys.argv[1], sys.argv[2])
        t1 = time.millitimer()
        print 'parse time:', 0.001 * (t1 - t0), 'sec.'
        _printform(form)
    else:
        print 'Usage: test fdfile [form]'
Exemplo n.º 8
0
def grabframe(f, x, y, w, h, pf):
    readsource(SRC_FRONT)
    if pf:
        w = w / pf * pf
        h = h / pf * pf
    data = lrectread(x, y, x + w - 1, y + h - 1)
    t = time.millitimer() - epoch.epoch
    framelist.append(data, t)
    readsource(SRC_FRAMEGRABBER)
Exemplo n.º 9
0
def test():
    import time
    t0 = time.millitimer()
    if len(sys.argv) == 2:
	forms = parse_forms(sys.argv[1])
	t1 = time.millitimer()
	print 'parse time:', 0.001*(t1-t0), 'sec.'
	keys = forms.keys()
	keys.sort()
	for i in keys:
	    _printform(forms[i])
    elif len(sys.argv) == 3:
	form = parse_form(sys.argv[1], sys.argv[2])
	t1 = time.millitimer()
	print 'parse time:', 0.001*(t1-t0), 'sec.'
	_printform(form)
    else:
	print 'Usage: test fdfile [form]'
Exemplo n.º 10
0
def main():
	nworkers = 4
	opts, args = getopt.getopt(sys.argv[1:], '-w:')
	for opt, arg in opts:
		if opt == '-w':
			nworkers = string.atoi(arg)
	if not args:
		args = [os.curdir]

	wq = WorkQ()
	for dir in args:
		wq.addwork(find, (dir, selector, wq))

	t1 = time.millitimer()
	wq.run(nworkers)
	t2 = time.millitimer()

	sys.stderr.write('Total time ' + `t2-t1` + ' msec.\n')
Exemplo n.º 11
0
Arquivo: T.py Projeto: 8Banana/py1.0
def TSTOP(*label):
	global t0, t1
	u, s, cu, cs = os.times()
	t1 = u+cu, s+cs, time.millitimer()
	tt = []
	for i in range(3):
		tt.append(t1[i] - t0[i])
	[u, s, r] = tt
	msg = ''
	for x in label: msg = msg + (x + ' ')
	msg = msg + `u` + ' user, ' + `s` + ' sys, ' + `r*0.001` + ' real\n'
	sys.stderr.write(msg)
Exemplo n.º 12
0
def saveframe(f, x, y, w, h, pf, notime):
    readsource(SRC_FRONT)
    if pf:
        w = w / pf * pf
        h = h / pf * pf
    data = lrectread(x, y, x + w - 1, y + h - 1)
    if pf: data = packrect(w, h, pf, data)
    if notime: t = 0
    else: t = time.millitimer() - epoch.epoch
    f.write( ` t ` + ',' + ` len(data) ` + '\n')
    f.write(data)
    readsource(SRC_FRAMEGRABBER)
Exemplo n.º 13
0
Arquivo: T.py Projeto: 8Banana/py1.0
def TSTART():
	global t0, t1
	u, s, cu, cs = os.times()
	t0 = u+cu, s+cs, time.millitimer()
Exemplo n.º 14
0
def main():
    foreground()
    pf = 2
    ausync = 0
    austart = 0
    optlist, args = getopt.getopt(sys.argv[1:], 'ca:sp:')
    for opt, arg in optlist:
        if opt == '-c':
            pf = 0
        elif opt == '-a':
            ausync = 1
            aumachine = arg
        elif opt == '-s':
            austart = 1
        elif opt == '-p':
            pf = int(eval(arg))
        else:
            usage()
    if args:
        if len(args) > 1:
            print 'Too many arguments'
            usage()
        filename = args[0]
    else:
        filename = 'film.video'
    if austart:
        if not ausync:
            print 'Cannot use -s without -a'
            usage()
        print 'Starting audio recorder...'
        posix.system('rsh ' + aumachine + ' syncrecord ' +
                     socket.gethostname() + ' &')
    if ausync:
        print 'Syncing to audio recorder...'
        globtime = vtime.VTime().init(1, aumachine, SYNCPORT)
        ctl = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        ctl.bind((socket.gethostname(), CTLPORT))
        aua = (socket.gethostbyname(aumachine), CTLPORT)
        print 'Done.'
    vidx, vidy, w, h = getvideosize()
    #prefsize(w,h)
    winx, winy = 1280 - w - 10, 1024 - h - 30
    prefposition(winx, winx + w - 1, winy, winy + h - 1)
    win = winopen(filename)
    f = open(filename, 'w')
    w, h = getsize()
    realw, realh = w, h
    ####doublebuffer()
    RGBmode()
    gconfig()
    qdevice(LEFTMOUSE)
    qdevice(RKEY)
    qdevice(SKEY)
    qdevice(CKEY)
    qdevice(PKEY)
    qdevice(ESCKEY)
    qdevice(WINQUIT)
    qdevice(WINSHUT)
    inrunning = 1
    outrunning = 0
    stop = 'stop'
    readsource(SRC_FRAMEGRABBER)
    mousing = 0
    epoch.epoch = time.millitimer()
    stoptime = epoch.epoch
    sizewritten = 0
    x, y = realw / 4, realh / 4
    w, h = w / 2, h / 2
    prealloc(w, h)
    try:
        drawframe(x, y, w, h, 1)
        nframe = 0
        num = 0
        while 1:
            insingle = 0
            outsingle = 0
            if mousing:
                drawframe(x, y, w, h, 0)
                ox, oy = getorigin()
                if sizewritten:
                    x = getvaluator(MOUSEX) - ox
                    y = getvaluator(MOUSEY) - oy
                else:
                    w = getvaluator(MOUSEX) - x - ox
                    h = getvaluator(MOUSEY) - y - oy
                drawframe(x, y, w, h, 1)
            if qtest() or \
            not (mousing or inrunning or insingle or outrunning or outsingle):
                ev, val = qread()
                if ev == LEFTMOUSE and val == 1:
                    drawframe(x, y, w, h, 0)
                    mousing = 1
                    ox, oy = getorigin()
                    x = getvaluator(MOUSEX) - ox
                    y = getvaluator(MOUSEY) - oy
                elif ev == LEFTMOUSE and val == 0:
                    if h < 0:
                        y, h = y + h, -h
                    if w < 0:
                        x, w = x + w, -w
                    mousing = 0
                    if not sizewritten:
                        wrheader(f, w, h, pf)
                        sizewritten = 1
                        prealloc(w, h)
                elif ev == RKEY and val == 1:
                    if not inrunning:
                        ringbell()
                    else:
                        outrunning = 1
                        wasstopped = time.millitimer() - stoptime
                        epoch.epoch = epoch.epoch + wasstopped
                        nframe = 0
                        starttime = time.millitimer()
                        if ausync:
                            ctl.sendto( ` (1, starttime) `, aua)
                elif ev == PKEY and val == 1 and outrunning:
                    outrunning = 0
                    stoptime = time.millitimer()
                    if ausync:
                        ctl.sendto( ` (0, stoptime) `, aua)
                    fps = nframe * 1000.0 / (time.millitimer() - starttime)
                    print 'Recorded', nframe,
                    print 'frames at', 0.1 * int(fps * 10), 'frames/sec'
                    print 'Saving...'
                    saveframes(f, w, h, pf)
                    print 'Done.'
                elif ev == PKEY and val == 1 and not outrunning:
                    outsingle = 1
                elif ev == CKEY and val == 1:
                    inrunning = 1
                elif ev == SKEY and val == 1:
                    if outrunning:
                        ringbell()
                    elif inrunning:
                        inrunning = 0
                    else:
                        insingle = 1
                elif ev in (ESCKEY, WINQUIT, WINSHUT):
                    if ausync:
                        ctl.sendto( ` (2, time.millitimer()) `, aua)
                    raise stop
                elif ev == REDRAW:
                    drawframe(x, y, w, h, 0)
                    reshapeviewport()
                    drawframe(x, y, w, h, 1)
            if inrunning or insingle:
                if outrunning:
                    rectcopy(vidx + x, vidy + y, vidx + x + w - 1,
                             vidy + y + h - 1, x, y)
                else:
                    rectcopy(vidx, vidy, vidx + realw - 1, vidx + realh - 1, 0,
                             0)
            ####swapbuffers()
            if outrunning or outsingle:
                nframe = nframe + 1
                if not sizewritten:
                    wrheader(f, w, h, pf)
                    sizewritten = 1
                if outrunning:
                    grabframe(f, x, y, w, h, pf)
                else:
                    saveframe(f, x, y, w, h, pf, outsingle)
    except stop:
        pass
    finally:
        drawmode(OVERDRAW)
        color(0)
        clear()
Exemplo n.º 15
0
def main():
	QSIZE = 16
	TIME = 5
	audio = 0

	opts, args = getopt.getopt(sys.argv[1:], 'aq:t:')
	for opt, arg in opts:
		if opt == '-a':
			audio = 1
		elif opt == '-q':
			QSIZE = string.atoi(arg)
		elif opt == '-t':
			TIME = string.atoi(arg)

	if args:
		filename = args[0]
	else:
		filename = 'film.video'

	if audio:
		if args[1:]:
			audiofilename = args[1]
		else:
			audiofilename = 'film.aiff'

	gl.foreground()

	x, y = SV.PAL_XMAX / 4, SV.PAL_YMAX / 4
	print x, 'x', y

	gl.minsize(40, 30)
	gl.stepunit(8, 6)
	gl.maxsize(SV.PAL_XMAX, SV.PAL_YMAX)
	gl.keepaspect(SV.PAL_XMAX, SV.PAL_YMAX)
	win = gl.winopen(filename)
	x, y = gl.getsize()
	print x, 'x', y

	v = sv.OpenVideo()
	v.BindGLWindow(win, SV.IN_REPLACE)
	v.SetSize(x, y)
	v.BindGLWindow(win, SV.IN_REPLACE)

	v.SetCaptureFormat(SV.RGB_FRAMES)
	v.SetCaptureMode(SV.BLOCKING_CAPTURE)
	v.SetQueueSize(QSIZE)
	v.InitCapture()
	if v.GetQueueSize() != QSIZE:
		QSIZE = v.GetQueueSize()
		print 'Warning: QSIZE reduced to', QSIZE

	gl.qdevice(DEVICE.LEFTMOUSE)
	gl.qdevice(DEVICE.WINQUIT)
	gl.qdevice(DEVICE.WINSHUT)
	gl.qdevice(DEVICE.ESCKEY)

	print 'Click left mouse to start recording', TIME, 'seconds'
	ofile = None
	afile = None
	# Mouse down opens the file & freezes window
	# Mouse up starts recording frames

	while 1:
		dev, val = gl.qread()
		if dev == DEVICE.LEFTMOUSE:
			# Start recording
			if val == 1:
				# Mouse down -- preparations
				if ofile == None:
					ofile = VFile.VoutFile().init(filename)
					ofile.format = 'rgb8'
					ofile.width = x
					ofile.height = y
					ofile.writeheader()
					# XXX other format bits?
				# The window can't be resized from now
				gl.prefsize(x, y)
				gl.winconstraints()
				gl.wintitle('* ' + filename)
				if audio:
					afile = initaudio(audiofilename)
				continue
			# Mouse up -- start actual recording
			global recording, stop_recording
			if audio:
				stop_recording = 0
				recording.release()
			t0 = time.millitimer()
			v.StartCapture()
			while 1:
				t = time.millitimer() - t0
				if t >= TIME*1000:
					break
				if v.GetCaptured() > 2:
					doframe(v, ofile, x, y, t)
			v.StopCapture()
			stop_recording = 1
			while v.GetCaptured() > 0:
				doframe(v, ofile, x, y, t)
				t = time.millitimer() - t0
			gl.wintitle(filename)
		elif dev == DEVICE.REDRAW:
			# Window resize (or move)
			x, y = gl.getsize()
			print x, 'x', y
			v.SetSize(x, y)
			v.BindGLWindow(win, SV.IN_REPLACE)
		elif dev in (DEVICE.ESCKEY, DEVICE.WINQUIT, DEVICE.WINSHUT):
			# Quit
			if ofile:
				ofile.close()
			if afile:
				afile.destroy()
			posix._exit(0)
			# EndCapture dumps core...
			v.EndCapture()
			v.CloseVideo()
			gl.winclose(win)
Exemplo n.º 16
0
        rectzoom(pf * chrompack * mf, pf * chrompack * mf)
        pixmode(PM_SIZE, 16)
        writemask(0x7ff - ((1 << ybits) - 1))
        lrectwrite(0, 0, cw - 1, ch - 1, chromdata)
        writemask((1 << ybits) - 1)
        pixmode(PM_SIZE, 8)
    if pf:
        rectzoom(pf * mf, pf * mf)
    elif mf <> 1:
        rectzoom(mf, mf)
    lrectwrite(0, 0, w - 1, h - 1, data)
    # This is ugly here, but the only way to get the two
    # channels started in sync
    #if af <> None:
    #	playsound(af,spkr)
    ct = time.millitimer() - epoch.epoch
    if epoch.correcttiming and tijd > 0 and ct < tijd:
        time.millisleep(tijd - ct)
    #swapbuffers()
    return tijd


def initcmap(ybits, ibits, qbits, chrompack):
    if ybits + ibits + qbits > 11:
        raise 'Sorry, 11 bits max'
    maxy = pow(2, ybits)
    maxi = pow(2, ibits)
    maxq = pow(2, qbits)
    for i in range(2048, 4096 - 256):
        mapcolor(i, 0, 255, 0)
    for y in range(maxy):
Exemplo n.º 17
0
def main():
    looping = 0
    packfactor = 0
    magfactor = 1
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'm:p:lF')
    except getopt.error:
        sys.stderr.write('usage: video ' + \
       '[-l] [-p pf] [-m mag] [-F] [moviefile [soundfile [skipbytes]]]\n')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-m':
            magfactor = int(eval(arg))
        elif opt == '-p':
            packfactor = int(eval(arg))
        elif opt == '-l':
            looping = 1
        elif opt == '-F':
            epoch.correcttiming = 0
    if args:
        filename = args[0]
    else:
        filename = 'film.video'
    f, w, h, pf, cinfo = openvideo(filename)
    if 0 < packfactor <> pf:
        w = w / pf * packfactor
        h = h / pf * packfactor
        pf = packfactor
    if args[1:]:
        audiofilename = args[1]
        af = open(audiofilename, 'r')
        spkr = openspkr()
        afskip = 0
        if args[2:]:
            afskip = eval(args[2])
        af.seek(afskip)
    else:
        af, spkr = None, None
    foreground()
    prefsize(w * magfactor, h * magfactor)
    win = winopen(filename)
    if pf:
        #doublebuffer()
        cmode()
    else:
        RGBmode()
    #doublebuffer()
    gconfig()
    if pf:
        initcmap(cinfo)
        color(2048)
        clear()
        writemask(2047)
        pixmode(PM_SIZE, 8)  # 8 bit pixels
    qdevice(ESCKEY)
    qdevice(WINSHUT)
    qdevice(WINQUIT)
    running = 1
    epoch.epoch = time.millitimer()
    nframe = 0
    tijd = 1
    if looping:
        looping = f.tell()
    try:
        while 1:
            if running:
                try:
                    tijd = loadframe(f, w, h, pf, af, spkr, cinfo, magfactor)
                    nframe = nframe + 1
                except EndOfFile:
                    running = 0
                    t = time.millitimer()
                    if tijd > 0:
                        print 'Recorded at',
                        print 0.1 * int(nframe * 10000.0 / tijd),
                        print 'frames/sec'
                    print 'Played', nframe, 'frames at',
                    print 0.1 * int(nframe * 10000.0 / (t - epoch.epoch)),
                    print 'frames/sec'
                    if looping:
                        f.seek(looping)
                        epoch.epoch = time.millitimer()
                        nframe = 0
                        running = 1
                        if af <> None:
                            af.seek(afskip)
            if af <> None:
                playsound(af, spkr)
            if not running or qtest():
                dev, val = qread()
                if dev in (ESCKEY, WINSHUT, WINQUIT):
                    raise bye
                elif dev == REDRAW:
                    reshapeviewport()
    except bye:
        pass
Exemplo n.º 18
0
def main():
	centerx, centery = 400, 300

	foreground()
	wid = winopen('cam')
	RGBmode()
	doublebuffer()
	gconfig()
	qdevice(ESCKEY)

	w, h = getsize()
	ortho2(0, w, 0, h)
	w = w/PF*PF
	h = h/PF*PF

	readsource(SRC_FRAMEGRABBER)

	s = socket(AF_INET, SOCK_DGRAM)
	if HOST == '<broadcast>':
		s.allowbroadcast(1)
	addr = HOST, PORT

	bytesperline = w/PF2
	linesperchunk = MAX/bytesperline
	linesperchunk = linesperchunk/PF*PF
	nchunks = (h+linesperchunk-1)/linesperchunk

	print 'MAX=', MAX,
	print 'linesperchunk=', linesperchunk,
	print 'nchunks=', nchunks,
	print 'w=', w, 'h=', h

	x1, x2 = 0, w-1

	t1 = millitimer()
	nframes = 0
	fps = 0

	msg = ''

	while 1:
		while qtest():
			dev, val = qread()
			if dev == REDRAW:
				reshapeviewport()
				w, h = getsize()
				ortho2(0, w, 0, h)
				w = w/PF*PF
				h = h/PF*PF

				bytesperline = w/PF2
				linesperchunk = MAX/bytesperline
				linesperchunk = linesperchunk/PF*PF
				nchunks = (h+linesperchunk-1)/linesperchunk

				print 'MAX=', MAX,
				print 'linesperchunk=', linesperchunk,
				print 'nchunks=', nchunks,
				print 'w=', w, 'h=', h

				x1, x2 = 0, w-1

				fps = 0

			elif dev == ESCKEY:
				winclose(wid)
				return

		readsource(SRC_FRAMEGRABBER)

		nframes = nframes+1
		if nframes >= fps:
			t2 = millitimer()
			if t2 <> t1:
				fps = int(10000.0*nframes/(t2-t1)) * 0.1
				msg = `fps` +  ' frames/sec'
				t1 = t2
				nframes = 0

		RGBcolor(255,255,255)
		cmov2i(9,9)
		charstr(msg)

		swapbuffers()
		rectcopy(centerx-w/2, centery-w/2, centerx+w/2, centery+w/2, 0, 0)

		for i in range(nchunks):
			y1 = i*linesperchunk
			y2 = y1 + linesperchunk-1
			if y2 >= h: y2 = h-1
			data = lrectread(x1, y1, x2, y2)
			data2 = packrect(x2-x1+1, y2-y1+1, PF, data)
			prefix = `w, h, PF, x1, y1, x2, y2`
			prefix = prefix + ' ' * (HS-len(prefix))
			data3 = prefix + data2
			s.sendto(data3, addr)
Exemplo n.º 19
0
class VTime():
    def init(self, (client, host, port)):
        s = socket(AF_INET, SOCK_DGRAM)
        host = gethostbyname(host)
        localhost = gethostbyname(gethostname())
        raddr = (host, port)
        s.bind((localhost, port))
        if client:
            #
            # We loop here because we want the *second* measurement
            # for accuracy
            for loopct in (0, 2):
                curtijd = time.millitimer()
                check = ` (loopct, curtijd, 0) `
                s.sendto(check, raddr)
                while 1:
                    try:
                        if loopct:
                            data, other = s.recvfrom(MSGSIZE)
                        else:
                            data, other = tryrecv(s)
                        newtijd = time.millitimer()
                        if other <> raddr:
                            print 'Someone else syncing to us: ', other
                            raise bad_connect
                        data = eval(data)
                        if data[:2] == (loopct + 1, curtijd):
                            break
                        if data[0] <> 2:
                            print 'Illegal sync reply: ', data
                            raise bad_connect
                    except recv_timeout:
                        curtijd = time.millitimer()
                        check = ` (loopct, curtijd, 0) `
                        s.sendto(check, raddr)
            histime = data[2]
            s.sendto( ` (4, newtijd, histime) `, raddr)
            mytime = timeavg(curtijd, newtijd)
            #mytime = curtijd
            self.timediff = histime - mytime
        else:
            while 1:
                data, other = s.recvfrom(MSGSIZE)
                if other <> raddr:
                    print 'Someone else syncing to us: ', other, ' Wanted ', raddr
                    raise bad_connect
                data = eval(data)
                if data[0] in (0, 2):
                    curtijd = time.millitimer()
                    s.sendto( ` (data[0] + 1, data[1], curtijd) `, raddr)
                elif data[0] == 4:
                    newtijd = time.millitimer()
                    histime = data[1]
                    mytime = timeavg(curtijd, newtijd)
                    #mytime = curtijd
                    self.timediff = histime - mytime
                    break
                else:
                    print 'Funny data: ', data
                    raise bad_connect
        return self