'From Squeak3.7 of ''4 September 2004'' [latest update: #5989] on 15 August 2008 at 12:56:42 pm'!

!OldSimpleClientSocket class methodsFor: 'remote cursor example' stamp: 'jmv 8/15/2008 12:56'!
remoteCursorSender
	"This is the client side of a test that sends samples of the local input sensor state to the server, which may be running on a local or remote host. This method opens the connection, then forks a process to send the cursor data. Data is sent continuously until the user clicks in a 20x20 pixel square at the top-left corner of the display. The server should be started first. Note the server's address, since this method will prompt you for it."
	"OldSimpleClientSocket remoteCursorSender"

	| sock addr stopRect evt type time lastMouseMoveTime lastMouseMoveButtons lastMouseModifiers new5 new6   str lastTime lastWasDummy |
	
	type _ nil.
	time _ 0.
	lastTime _ 0.
	lastMouseMoveTime _ 0.
	lastMouseMoveButtons _ nil.
	lastMouseModifiers _ nil.
	lastWasDummy _ true.
	
	Transcript show: 'starting remote cursor sender'; cr.
	Transcript show: 'initializing network'; cr.
	Socket initializeNetwork.
	addr _ NetNameResolver promptUserForHostAddress.
	Transcript show: 'opening connection'; cr.
	sock _ OldSimpleClientSocket new.
	sock connectTo: addr port: 54323.
	sock waitForConnectionUntil: self standardDeadline.
	(sock isConnected) ifFalse: [self error: 'sock not connected'].
	Transcript show: 'connection established'; cr.

	stopRect _ 0@0 corner: 6@6.  "click in this rectangle to stop sending"
	Display reverse: stopRect.
	evt := Array new: 8.
	"the sending process"
	[
		Sensor primGetNextEvent: evt.
		type _ evt at: 1.		
		new5 _ evt at: 5.
		new6 _ evt at: 6.
		type= 1 and: [(new5 = 0) not and: [(evt at: 3) < 21 and: [(evt at: 4) < 21]]]
	] whileFalse: [
		type = 0 ifTrue: [
			time _ Time millisecondClockValue.
			(lastWasDummy not and: [
					time-lastTime > 200]) ifTrue: [
				str _ evt printString, CrLf.
				sock sendData: str count: str size.
				sock sendData: str count: str size.
				lastWasDummy _ true]].
		type = 1 ifTrue: [
			time _ evt at: 2.
			(lastMouseMoveButtons = new5 and: [
				lastMouseModifiers = new6 and: [
					time-lastMouseMoveTime < 100]]) ifFalse: [		
				str _ evt printString, CrLf.
				sock sendData: str count: str size.
				lastMouseMoveTime _ time.
				lastMouseMoveButtons _ new5.
				lastMouseModifiers _ new6.
				lastTime _ time.
				lastWasDummy _ false]].
		type = 2 ifTrue: [
			time _ evt at: 2.		
			str _ evt printString, CrLf.
			sock sendData: str count: str size.
			lastTime _ time.
			lastWasDummy _ false].

	].
		
	Display reverse: stopRect.
	sock waitForSendDoneUntil: self standardDeadline.
	sock destroy.
	Transcript show: 'remote cursor sender done'; cr.! !

