States - CS 580 Client-Server Programming Spring Semester,...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 580 Client-Server Programming Spring Semester, 2010 Doc 14 States 11 March, 2010 Copyright , All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http:// www.opencontent.org/opl.shtml ) license defines the copyright on this document. References 2 Design Patterns: Elements of Reusable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, Addison-Wesley, 1995 Selected Patterns for Implementing Finite State Machines, Paul Adamczyk, http://hillside.net/plop/2004/papers/padamczyk0/PLoP2004_padamczyk0_0.doc , referenced from http://hillside.net/plop/2004/final_submissions.html References 2 Design Patterns: Elements of Reusable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, Addison-Wesley, 1995 Selected Patterns for Implementing Finite State Machines, Paul Adamczyk, http://hillside.net/plop/2004/papers/padamczyk0/PLoP2004_padamczyk0_0.doc , referenced from http://hillside.net/plop/2004/final_submissions.html States 3 Some Servers are stateful or have modes Each connection has different states Some commands are only legal in some states How to deal with states? If (case) statements Table of function pointers State Objects (State pattern) Finite Automata - State Machines 4 NoAuth HaveUser Process Invald Quit USER QUIT QUIT QUIT PASS LIST RETR PASS (successful) USER LIST RETR PASS (fail) USER PASS LIST RETR Using Switch Statements 5 int state = 0; while (true) { command = input.read(); switch (state) { case 0: if (command.isUser()) { username = command.argument(); state = 1; } else if (command.isQuit()) state = 4; else error("Illegal command: " + command); break; case 1: if (command.isPassword()) { if (valid(username, command.argument())) state = 2; else { error("Unauthorized User"); state = 3; } } else error("Unknown: " + command); break; NoAuth 1 HaveUser 2 Process 3 Invalid 4 Quit More Readable Version 6 int state = NO_AUTH; while (true) { command = input.read(); switch (state) { case NO_AUTH: noAuthorizationStateHandle( command ); break; case HAVE_USER: haveUserStateHandle( command ); break; case PROCESS: processStateHandle( command ); break; case INVALID: invalidStateHandle( command ); break; case QUIT: quitStateHandle( command ); break; } void noAuthorizationStateHandle(PopCommand a Command) { if (command.isUser()) { username = command.argument(); state = HAVE_USER ; } else if (command.isQuit()) state = QUIT ; else error("Illegal command: " + command); } Switch Method Analysis...
View Full Document

This note was uploaded on 10/16/2011 for the course CS 580 taught by Professor Rogerwhitney during the Spring '11 term at Community college of RI.

Page1 / 26

States - CS 580 Client-Server Programming Spring Semester,...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online