90 likes | 174 Views
GBK Programming 2 2009-10. Jordan Johnson Week 1, Day 2. Today’s plan. Greeting Networking, Continued: The Universe Clean-up. Goal. Add networking to the “Sea Battle” game. Requirements: Learn to write client (“world”) programs. Learn to write server (“universe”) programs. So far.
E N D
GBK Programming 22009-10 Jordan Johnson Week 1, Day 2
Today’s plan Greeting Networking, Continued: The Universe Clean-up
Goal • Add networking to the “Sea Battle” game. • Requirements: • Learn to write client (“world”) programs. • Learn to write server (“universe”) programs.
So far... • “Butterfly” simulation: • Butterfly moves from one computer to another, left to right on each individual screen. • (A butterfly icon is saved on the student server under Programming 2). • Messages: • Server Client: ‘go(i.e. “You have the butterfly.”) • Client Server: ‘done(“The butterfly has left me.”) • We’ve written the client.
Review: The Client • To write the client: • Handlers sometimes return a package instead of just a world. • (make-packageworldmessage) • “Here’s the new world, and a message for the server.” • big-bang has new clauses: • on-receive – to say how to handle inbound messages. • register – to specify the address of the server. • name – to identify your world to the server by name.
The Server • The server’s job is: • Keep track of the universe. • Respond to communication from worlds. • More specifically: • Maintain a list of connected worlds. • Add new worlds to the list. • Remove old worlds from the list. • When a world’s done with the butterfly, send the butterfly to the next world in line.
The Server(cont.) • New types of data: • iworld – representation of a world (client) • The “universe” (server’s equivalent of “world”) • A universe is a listof[iworld] • (make-mailiworlds-exp) • A message to the given iworld. • bundle – server’s equivalent of a package • (make-bundleuniverse messages goners) • universe is the new state of the universe • messages is a listof[mail], to be sent to the worlds • goners is a listof[iworld], any worlds the server wants to disconnect.
The Server(cont.) • Starting the server: • universe is the equivalent of big-bang: • (universe u0 (on-new hookup) (on-disconnect hangup) (on-msg listen)) • hookup : universe iworld bundle • How to handle a new world. • hangup : universe iworld bundle • How to react when a world disconnects. • listen : universe iworld s-exp bundle • How to behave when a world sends a message.
Clean-up • Before you leave, please . . . • Log out. • Push in your chair. • Make sure you’ve got everything. • Make sure all trash ends up in the trash can.