6/13/20151 CS 160: Lecture 13 Professor John Canny Fall 2004
6/13/20152 Outline 4 Model-view controller *Why do we need it? *Changing the display *Event flow *Dragging at interactive speeds 4 Interactive application programming *Callbacks *Multi-threaded programming
6/13/20153 Model-View-Controller 4 Architecture for interactive apps *introduced by Smalltalk developers at PARC 4 Partitions application in a way that is *scalable *maintainable Model View Controller
6/13/20154 Example Application Blue circles: 4 Cardinal squares: 2
6/13/20155 Model 4 Information the app is trying to manipulate 4 Representation of real world objects *circuit for a CAD program +logic gates and wires connecting them *shapes in a drawing program +geometry and color Model View Controller
6/13/20156 View 4 Implements a visual display of the model 4 May have multiple views *e.g., shape view and numerical view Model View Controller
6/13/20157 Multiple Views Blue circles: 4 Cardinal squares: 2
6/13/20158 View 4 Implements a visual display of the model 4 May have multiple views *e.g., shape view and numerical view 4 Any time the model is changed, each view must be notified so that it can change later *e.g., adding a new shape Model View Controller
6/13/20159 Controller 4 Receives all input events from the user 4 Decides what they mean and what to do *communicates with view to determine which objects are being manipulated (e.g., selection) *calls model methods to make changes on objects +model makes change and notifies views to update Model View Controller
6/13/ Controller Blue circles: 4 Cardinal squares: 2
6/13/ Controller Blue circles: 4 Cardinal squares: 2
6/13/ Relationship of View & Controller “pattern of behavior in response to user events (controller issues) is independent of visual geometry (view issues)” 4 Controller must contact view to interpret what user events mean (e.g., selection)
6/13/ Combining View & Controller 4 View and controller are tightly intertwined *lots of communication between the two 4 Almost always occur in pairs *i.e., for each view, need a separate controller 4 Many architectures combine into a single class Model View Controller
6/13/ Why MVC? 4 Combining MVC into one class or using global variables will not scale *model may have more than one view +each is different and needs update when model changes 4 Separation eases maintenance *easy to add a new view later *new model info may be needed, but old views still work *can change a view later, e.g., draw shapes in 3-d (recall, view handles selection)
6/13/ Presentations next Mon-Weds 4 Monday groups Weds groups 6-10
6/13/ Adding Views Later Blue circles: 4 Cardinal squares: 2
6/13/ Changing the Display 4 How do we redraw when shape moves?
6/13/ Moving Cardinal Square Blue circles: 4 Cardinal squares: 2
6/13/ Erase w/ Background Color and Redraw Blue circles: 4 Cardinal squares: 2
6/13/ Changing the Display 4 Erase and redraw *using background color to erase fails *drawing shape in new position loses ordering 4 Move in model and then redraw view *change position of shapes in model *model keeps shapes in a desired order *tell all views to redraw themselves in order *slow for large / complex drawings +flashing!
6/13/ Damage / Redraw Method 4 View informs windowing system of areas that need to be updated (i.e., damaged) *does not redraw them at this time… 4 Windowing system *batches updates *clips them to visible portions of window 4 Next time waiting for input *windowing system calls Redraw method for win. +passes region that needs to be updated
6/13/ Damage old, Change position in model, Damage new Blue circles: 4 Cardinal squares: 2
6/13/ Event Flow 4 Creating a new shape
6/13/ Event Flow (cont.) 4 Assume blue circle selected Blue circles: 0 Cardinal squares: 0
6/13/ Event Flow (cont.) 4 Press mouse over tentative position 4 Windowing system identifies proper window for event 4 Controller for drawing area gets mouse click event 4 Checks mode and sees “circle” 4 Calls models AddCircle method with new position Blue circles: 0 Cardinal squares: 0
6/13/ Event Flow (cont.) 4 AddCircle adds new circle to model’s list of objects 4 Model then notifies list of views of change *drawing area view and text summary view 4 Views notifies windowing system of damage *both views notify WS without making changes yet! +model may override Blue circles: 0 Cardinal squares: 0
6/13/ Event Flow (cont.) 4 Views return to model, which returns to controller 4 Controller returns to event handler 4 Event handler notices damage requests pending and responds 4 If one of the views was obscured, it would be ignored Blue circles: 0 Cardinal squares: 0
6/13/ Event Flow (cont.) 4 Event handler calls view’s Redraw methods with damaged area 4 Views redraw all objects in model that are in damaged area Blue circles: 1 Cardinal squares: 0
6/13/ Dragging at Interactive Speeds 4 Damage old, move, damage new method may be too slow *must take less than 200 ms to be smooth 4 Solutions *don’t draw object, draw an outline (cartoon) +use XOR to erase fast (problems w/ color) +save portion of frame buffer before dragging
6/13/ Break 4 Next week presentations: 4 Monday: groups Wednesday: groups 6-10
6/13/ Interactive programming 4 Callbacks 4 Threading
6/13/ Callbacks 4 Callbacks: every widget registers itself to receive certain events with the OS. 4 Dangers: UI code using callbacks is usually single-threaded. Control does not return to the event loop until the callback returns. Therefore in a callback routine, never: *Wait for another event. *Sleep. *Perform I/O or other actions that may block.
6/13/ HE Principles: 4 User control and freedom 4 Visibility of system status 4 Help, 4 diagnosing errors
6/13/ Threaded code 4 Multi-threaded code uses distinct threads for distinct widgets. 4 It allows blocking operations (e.g. I/O) to be confined to particular threads that don’t affect interactivity. 4 If your program has any time-intensive parts, they should run in different threads from the UI code.
6/13/ Threaded code 4 Use separate threads for any operations that can occur asynchronously: *UI interactions. *File operations – use separate threads if you need to be updating several files at the same time. *Inter-process communication (sockets): use one thread for each connection. *Use a thread for each other I/O device, e.g. one each for reading from or writing to the sound card.
6/13/ Inter-thread communication 4 The window system and running process in the OS communicate using message passing: *The event queue and sockets are examples of message-passing primitives. 4 Processes in the same address space (i.e. within your program) can use shared memory (i.e. shared variables) which is much more efficient.
6/13/ Inter-thread communication 4 Why can’t thread A call thread B (like a callback?)
6/13/ Synchronization 4 Shared-memory communication poses challenges. If you rely on “mailbox” primitives, things can go wrong: 0 Flag to show this thread is writing new data Data
6/13/ Synchronization Intuitively, threads that want to write should: …. wait until thread_id = 0; write thread_id; write data; 0 Data Flag to show this thread is writing new data
6/13/ Synchronization But thread switching can happen anytime, e.g. …. wait until thread_id = 0; write thread_id; write data; 0 Data Flag to show this thread is writing new data
6/13/ Synchronization A switch between checking the flag and setting it allows both threads to (incorrectly) write the flag and their data. To prevent this, we define critical sections of the code that cannot be interrupted. 0 Data Flag to show this thread is writing new data
6/13/ Synchronization e.g. the critical section in the example is: …. wait until thread_id = 0; write thread_id; write data; 0 Data Critical section, thread can’t be pre-empted. Flag to show this thread is writing new data
6/13/ Semaphores Rather than many critical sections in your code, you can use a single semaphore primitive. A semaphore is initialized to an integer n, and has just two operations: test(); wait until semaphore > 0, then decrement it. increment(); increment the semaphore.
6/13/ Semaphore example Then the example code becomes (with sem1 initialized to 1): …. sem1.test(); write thread_id; write data; sem1.increment(); This section cannot be pre-empted by the other process that is using this semaphore.
6/13/ Summary 4 Model-view controller *Why do we need it? *Changing the display *Event flow *Dragging at interactive speeds 4 Interactive application programming *Callbacks *Multi-threaded programming