Prototyping Interact Sys
Prototyping Interact Sys CS 6452
Popular in Course
Popular in ComputerScienence
This 0 page Class Notes was uploaded by Alayna Veum on Monday November 2, 2015. The Class Notes belongs to CS 6452 at Georgia Institute of Technology - Main Campus taught by Warren Edwards in Fall. Since its upload, it has received 12 views. For similar materials see /class/234056/cs-6452-georgia-institute-of-technology-main-campus in ComputerScienence at Georgia Institute of Technology - Main Campus.
Reviews for Prototyping Interact Sys
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 11/02/15
PeertoPeer Networking and Discovery Technologies OOOOOOQ OOOOOQQ COCOSQ QQH Georgia Tech Week 6 Georgia My Tech What s PeertoPeer E 0 A different network architecture than clientserver 0 Each peer is both a client and a server 0 Appropriate for applications that don t require a centralized resource 0 Really good for times when you may have connectivity to each other but not connectivity to the Internet itself Georgia Tech Finding Resources 0 Problem how do you know what machinesservices are available to you at any given time 0 Clientserver you have to know what you re looking for ahead of time and then name it explicitly Q Eg typing googlecom into a browser address eld 0 Peertopeer more problematic 3 May not even know what s available to you Q What s available may change rapidly 1 Hosts may not even have names no DNS if you re off of the managed network for instance Georgia Tech Solution Discovery Protocols 0 Discovery protocols are mechanisms for allowing a program to dynamically acquire references to resources it might want to use 3 Typically IP addresses of available hosts port numbers of available services 0 Address problems common in peertopeer and other networks 9 Tell you what s available 0 Update this information based on current state of the network Q Provide you with handles applications can use to access resources even if they don t have humanreadable names 0 Process happens automatically no enduser intervention Why Discovery Protocols are Gee33 Cool 3 0 Allow the creation of very dynamic applications G Eg iTunes music sharing 0 Allow you to get work done even if you re not on the broader Internet 0 Eg le transfer chat etc when you re not on a hotspot 0 Better enduser easeof use no typing in IP addresses host names etc 0 Eg automatic printer setup automatic discovery of multiple TiVos How They Work Georgia Tech 0 Lots of discovery protocols in use Bluetooth Universal Plugand Play ZeroconfBonjour 0 Lots of variation in details but basics are generally the same H 0 Use a mechanism called multicast Allows a program to send a single message that can be received by any number of other hosts Typically con gured to only work on a single network segment Servicesdevices announce their presence using a periodic multicast message Interested parties set themselves up to listen for these announcements Georgia Tech Example Zeroconf 0 Popularized by Apple 1 Also called Rendezvous Bonjour OpenTalk Q Basis for iTunes music sharing photo sharing iChat 0 Builds atop other Internet technologies 0 Provides a multicast version of DNS to allow name resolution to work in the absence of managed DNS servers 3 Extends DNS to allow service discovery information to be exchanged in DNS records 0 mDNS new domain Iocal Q Names in this domain presumed to be meaningful only on the local link H Analogous to privatenonrouted IP addresses Q Attempt to resolve tabascoJocaI triggers multicast to other computers which can answer if they know the IP address of tabasco Georgia Tech Example Zeroconf cont d 0 DNSSD allows use of DNS for service discovery 1 Clients name the service types they wish to nd 0 Format typeprotocoldomain 0 Example httptcplocal would refer to all HTTP servers in the Iocal domain 0 Types are just strings that name application protocols ftp http ssh ii DNSSD then returns a list of service names that match that type 0 Humanreadable names that identify the service 0 Example Keith s web server on tabasco 0 These service names are then resolved to an IP address and port number 0 Example looking up Keith s web server on tabasco might return I92 I 68 l l 7780 0 This info can then be used to contact the web server Using Zeroconf in Practice Georgia Tech 0 My recommendationzjmdns 0 Pure Java implementation of mDNS and DNSSD callable from Jython 0 Steps to using it 0 Start your service recording its port number 0 Publish the service Choose a humanreadable service name to publish under your user name for example Set the service type to be the name of the protocol we ll agree upon c56452tcplocal Publish its IP address and port number which other apps will use order to connect to it 9 Register for noti cations about peers Tell jmdns that you want to know about services of type c56452tcplocal Provide callbacks that jmdns will invoke when services come or go When a service appears you may need to ask jmdns to resolve that service s name to get its IP address and port number Once you have the address and port you can connect to it using normal mechanisms GotchasTips and Tricks Georgia Tech 0 Make sure your rewall is off if you expect offmachine clients to be able to connect to you 0 You can test and debug locally 3 Publish different instances of your tool under different names 0 Be sure to use different port numbers 0 Graphical mDNS browser D java jar libjmdnsjar browse 5 Replace stuff in red with path to jmdnsjar Data Management OOOOOwu ocean comm 3 Georgia Tech Week I4 Why Focus on Data Management 0 Lots of data to keep track of in many programs 0 Eg map to keep track of your individual chat windows 0 Example of transient data 0 You create it while your program runs 0 When your program stops it goes away 0 Not needed between runs Georgia Tech l Persistent Data Management Georgia Tech l 0 Sometimesyou need to store data across runs of your program or among several programs 0 Example storing preferences information for chat names icons etc 0 Simplest strategy flat les 0 Text les usually containing ASCII that your program reads and writes 0 Flat itte structure 0 What can go wrong when using flat les coo 0000 Pros and Cons of Flat File Data Georggci lg 55339 o 39 PerSIstence E 0 Pros C Quick n easy 0 Doesn t require any fancy libraries to use 0 Cons Errorprone parsing especially if you want to save lots of stuff If you need to nd just one thing in a file or update just one thing in a file can be slow 0 Searches and updates are linear in the length of the file meaning proportional to the length Have to be careful to program defensively 0 File system fills up program crashes half way through writing may result in corrupt file Hard to do concurrent access correctly Tech Georgia Q Other Options 0 Option l structured les G Example XMLbased preferences le 0 ltUSERNFOgt ltNAMEgtJoe SchmoeltNAMEgt ltICONgtUsersSchmoeDocumentsmyiconjpgltCONgt O ltUSERNFOgt 6 Pros O Takes advantage of XML structure to make parsing easier 0 XML library ensures you ve got a wellformatted XML le 0 Cons 0 Doesn t take care of defensive programmingconcurrent access problems just structure problem 0 Searchupdate can still be slow Tech Georgia 4 Other Options 0 Option 2 databases 0 What s a database 0 A database is a service that takes care of the persistent storage management and retrieval of structured data 0 Hides most of the details of les lesystems etc 0 Provides a highly structured way to readwrite data 0 Uses a language to let you do this 0 Can query your data to nd what you want 6 Quick updates constant time meaning same time no matter how big the database gets Q Takes care of much of the defensive programming stuff 0 But you can still corrupt a database if you write crap to it Tech Georgialy When to Use a Database 3 0 You need to manage a lot of data few kilobytes up to a few terabytes that will persist across runs 0 Need to be able to search through it quickly update it quickly or perform complex queries 0 Need more safety than you can get with just files 0 Need to support concurrent access Relational Databases Georgia Tech Most common type of database today Q Other lesscommon type objectoriented databases Basic concepts records are stored in tables G Think of records as a row in a table Generally multiple records in any given table Most complex systems will have multiple tables Each column has a type string integer etc Some columns uniquely identify their records called keys or indices Q Used to crossreference rows in different tables coo l 33 Georgia coo Tech 0 Exam ple 3 Customer Number Customer Name Customer Address Integer KEY String String 52352113 Keith Edwards 85 Fifth Street NW 62352922 Rich DeMillo 801 Atlantic Drive Product Number Description List Price Integer KEY String Floating point 235532 Sterling Martini Shaker 6999 523333 Olives 599 Tech Georgia 4 Keys 0 Important concept key columns Q Special columns that uniquer identify a row Q Contains a special numbertokenidentifier that uniquely identifies the row I 0 Might be a natura key or something we have to make up on our own 0 Eg in a tax database SSN might suffice as a key 0 In previous examples customer number and product number are keys 0 Likely made up by the company itself 0 In some cases multiple columns might be required to uniquely identify a row 0 Take care when choosing keys I994 Brazilian gov t chose father s name mother s name DOB as a key for voting registration 0 Only unique for siblings born on different dates Q Reason why artificial keys are often made up SSN Georgia Tech Database Schemas 0 Schemas are the layout of the data G The set of tables the columns of those tables etc 0 Represents the logical structure of your data 0 Created by a database designer 0 This is the person who creates the schema for a particular application not the person who writes the database itself 0 Once the schema is created it stays pretty much fixed 0 Applications just adddeleteupdate rows not restructure the entire database 0 For this project you ll be taking the role of database designer as well as application programmer 0 I ll be helping with the database design though Database Design 0 Database jargon Q Entities the real world things that are represented in the database 0 Eg people messages chat sessions etc Georgia Tech 0 Attributes aspects of an entity that we want to represent 0 Eg people have names etc 0 RelationshipsAssociations among entities l 0 Eg person A sent message BThis is a relationship between person A and message B Georgia Tech U COO Designing a Schema 0 Entities generally turn into tables 0 Each instance of an entity is a new row in that table 0 Eg a person table with one row for each person Attributes generally turn into columns in tables Q Eg a person table might have columns for rst last names icons etc 0 Relationships are the glue that holds a database together 9 Cross references between entity tables 9 Eg a person is involved in multiple chats while a chat has multiple members 0 Relationships can either be represented as separate columns or as their own tables 0 More on this later 0 Good design guideline minimize redundancy Q Redundancy is just another chance for tables to get out of sync with each other Georgia Tech OnetoOne Relationships 0 What are the ways entities can be related to each other 0 Onetoone relationships 9 Eg accounts receivable system 9 Each AR can have at most one customer associated with it each customer can have at most one AR associated with himher 0 Onetoone relationships best represented as columns in a combined table Account Receivable Date Customer Number 1225498 21505 579923512 1225499 21 605 999922531 000 0000 0000 Georgia 000 Tech 0 OnetoMany Relationships 3 0 Onetomany Many instances of one entity are associated with one instance of another entity Eg library checkout system One person can have many books checked out but each book can only be checked out by one person 0 Very common Usually add column to one table only Customer Customer Number Name Book Checked Out 12255193 Kelth Edwards ID Book Name By 333521 HlSlOry Of 12255193 93321355 Rich DeMlllo Typography 793313 Get Typed 12255193 ManytoMany Relationships 0 Manytomany G EgAmazon productorder database Georgia Tech 0 Any given order might contain multiple distinct products 0 Any given product might be associated with multiple orders l 0 Relationships like this result in the creation of a new table to hold just the relationship Product Description 12 Martini Shaker 53 Olives Order Date B29 21505 033 21505 Product Order 12 B29 53 B29 53 033 Tech Georgialy Database Design Principles 3 O Normalizing data relationships 0 Avoid as much redundancy as possible 0 Structure things so that anomaies can t happen 0 Example Acme Industries INVOICE Customer Number 1454 Order Date 110506 Customer W E Coyote Terms Net 30 General Delivery Ship Via USPS Falling Rocks AZ 84211 599 555 9345 Product No Description Quant Unit Price Ext Amount SPR 2290 Super strength springs 2 2400 4800 STR 67 Foot straps leather 2 250 500 HLM 45 Deluxe crash helmet 1 6788 6788 SFR l Rocket solid fuel 1 12820040 12820040 ELT 7 Emergency location transmitter 1 7988 FREE GIFT Total Order Amount 12832128 An OverlySimple Approach Tech Georgia My Cust Cust Cust Cust Cust Cust Cust Order Prod No Desc Quant Unit Price Ext No Name Addr City State ZIP Phone Data 39 39 39 39 Amount SPR2290 Super strength spr ngs 2 24 00 48 00 STR67 Foot straps eather 2 2 50 5 00 W E F 11 2 1454 C t R203 AZ 84211 23331 0065 HLM45 De uxe Crash He met 1 67 88 67 88 W0 9 y SFR1 Rocket so d fue 1 128 200 40 128 200 40 ELT1 Emergency Transm tter 1 78 88 0 00 Georgia Tech The Invoice Example 0 This simple table structure is too simple 0 Presents possibility for anomalies to arise during use 0 Three types of anomalies 0 Insert anomalies 0 Delete anomalies 0 Update anomalizes O We ll work through an example showing how you normalize tables to avoid anomalies Tech l Georgia Insert Anomalies D Insert anomalies can t insert a new row because of an arti cial dependency on another relation Two different entities are mixed in the same relation table Eg can t insert a new customer into the database unless they bought something because all customer data is embedded in the invoice Cust Cust Cust Cust Cust Cust Cust Order Prod No Desc Quant Unit Price Ext No Name Addr City State ZIP Phone Data 39 39 39 39 Amount SPR2290 Super strength spr ngs 2 24 00 48 00 STR67 Foot straps eather 2 2 50 5 00 1454 go Eote E2023 AZ 84211 23 33652 HLM45 De uxe Crash He met 1 67 88 67 88 y y SFR1 Rocket so d fue 1 128 200 40 128 200 40 ELT1 Emergency Transm tter 1 78 88 0 00 Delete Anomalies Tech l Georgia D Delete anomalies deletion of data about one entity causes unintended loss of data about another D Two different entities are mixed in the same relation Cust No Cust Name Cust Add r Cust City Cust State Cust ZIP Cust Phone Order Data Prod No Desc Quant Unit Price Ext Amount D Eg deleting last invoice for a customer loses all data for that customer Update Anomalies Georgia Tech D Update anomalies update of a single data value requires multiple rows of data to be updated D Too much redundancy in the database D Eg if we wanted to change customer s address have to update every invoice for that customeropportunity for inconsistency Cust Cust No Name 1454 W E Coyote 1454 W E Coyote Cust Order Ext Phone Data Prod No Desc Quant UnIt PrIce Amount SPR229O Super strength spr ngs 2 24 00 48 00 STR67 Foot straps eather 2 2 50 5 00 33652 HLM45 De uxe Crash He met 1 67 88 67 88 39 SFR1 Rocket so d fue 1 128 200 40 128 200 40 ELT1 Emergency Transm tter 1 78 88 0 00 59955 1230 03235 H gh exp os ves 2 249 99 499 98 59345 2006 AJ92 Anv 1 149 99 149 99 Georgia Tech lnClass Exercise 0 Figure out a correct schema for the Invoice example 0 Steps Choose a primary key if one doesn t already exist 2 Get rid of multivalued attributes 0 Why Hard to select out what we want 0 Move multivalued attributes to a new table 0 Copy key from original table to the new one 3 Make sure that nonkey attributes only are determined by the entire key not part of the key 0 Why This is a sign that you re mixing unrelated information about multiple entities in a single table 0 Move attribute to new table where it depends on entire key 4 Make sure that no attributes are determined by nonkey attributes 0 Why Again clean separation of relations 0 Move determined attributes to new table where they depend only on the key 0 Summary make sure any nonkey attribute in a table depends on the key the whole key and nothing but the key O O O O O O O O O O O Georgla o o a Tech 0 0 F39 t St E 39 I Ch 39 39f d d 39 0058 a primary ey one oesn t a rea EXIST Invoice Cust Cust Cust Cust Cust Cust Order Ext Name Addr City State ZIP Phone Data Pmd39 No39 Desc39 Quant39 umt Prlce Amount SPR2290 Super strength 2 24 00 48 00 STR67 spr ngs 2 2 50 5 00 EV Et genera E3 29 AZ 84211 2953 1152005 HLM45 Foot straps eather 1 67 88 57 88 W0 9 9 Very 0quot S 39 SFR1 De uxe Crash He met 1 128 200 40 128 200 4 ELT1 Rocket so d fue 1 78 88 0 None of the elds here do a good job of uniquely identifying this entire row so we ll create one called Invoice Number O C C A O O C O O O C 0 Georgia 0 o 0 Tech 0 0 3 Cl St E 2 Get rid of mutivaued attributes each cell should contain just one data element El Make a new table for multivalued attributes copy the key column Invoice Invoice Cust Cust Cust Cust Cust Cust Cust Order Ext No No Name Addr City State ZIP Phone Date Pmd39 No39 Desc39 Quant39 umt Pnce Amount Super strength SPR2290 spr ngs 2 24 00 28080 STR67 Foot straps eather 2 2 50 W E F 59955 67 88 12345 1454 CO Ote R202 AZ 84211 59345 1152006 HLM45 De uxe Crash He met 1 67 88 128 200 4 y y SFR1 Rocket 50 d me 1 128 200 40 0 ELT1 Emergency 1 78 88 0 00 Transm tter Invoice Invoice Cust Cust Cust Cust Cust Cust C No No Name Addr Cit State ZIP F y Inv0Ice LIne Item W E Genera Fa ng c 12345 1454 AZ 84211 Coyote De very Rocks 5 39quotV 39 e Prod No Desc Quantity Unit Price EXt39 No Amount 12345 SPR2290 Super Strength 2 24 00 48 00 amass 12345 STR67 Foot straps 2 2 50 5 00 12345 HLM45 De uxe He met 1 67 88 67 88 12345 SFR1 Rocket 1 128 200 40 128 200 40 12345 ELT1 Emerg Trans 1 78 88 0 00 Tech I Georgia Third Step 3 Make sure that nonkey attributes only are determined by the entire key not part of the key D In Invoice Line temthe Invoice No and Product No together act as the key El There can be at most one row with that unique combination of Invoice and Product numbers D But other columns description unit price depend solely on the product number D I I Move to Its own table nvoce Line Item Ext Amount Invoice No Prod No Quantity Sales Price Invoice Line Item 12345 SPR2290 24 00 48 00 Invoice No Prod No Desc Quantity Unit Price STR67 12345 2 50 5 00 12345 SPR2290 Super strength 24 00 88 67 88 12345 STR67 Foot straps 2 50 Product 1 28 200 40 12345 HLM45 De uxe He met 67 88 Prod No Desc List Price 000 12345 SFRI Rocket 128 200 40 SPR2290 Super strength spr ngs 24 00 12345 ELT1 Emerg Trans 78 88 STR67 Foot straps 2 50 HLM45 De uxe He met 67 88 SFR1 Rocket 128 200 40 ELT1 78 88 Emerg Trans Fourth Step Georgia Tech 4 Make sure that no attributes are determined by nonkey attributes I D In the Invoice table Customer Name depends on Invoice Number each Invoice Number has only one Customer Name associated with it D but Customer Name also depends on Customer Number D Same is true for rest of customer attributes D Move to its own table Invoice Invoice Cust Cust Cust Cust Cust Cust Cust Order No No Name Addr City State ZIP Phone Date 12345 1454 W E Genera Fa 9 AZ 84211 59965 1152006 Coyote De very Rocks 59345 Invoice Customer Invoice Gust Order CUSt CUSt CUSt CUSt Cust No No Date CUSt39 No39 Name Addr CUSt39 Clty State ZIP Phone W E Genera Fa ng 599555 14 4 AZ 4211 12345 1454 1152006 5 Coyote De very Rocks 8 9345 C C C C 0 Georgia 0 a Tech 0 0 Reca f Normalization 7 0 Process is fairly cookbook a But still dif cult 3 Have to understand the data relationships among them 0 Generally moves toward more smaller tables I I Av01d Insert delete update anomalles lnvmce Lme Item customer anOlce Prod No Quantity Sales Price EXt39 No Amount Cust Cust Cust Cust Cust CUSt39 No39 Name Addr CUSt39 Clty State ZIP Phone 12345 SPR392290 2 24 00 48 00 1454 W E Genera Fa ng AZ 84211 599 555 12345 STR67 2 2 50 5 00 Coyote De very Rocks 9345 12345 HLM45 1 67 88 67 88 Product 12345 SFR1 1 128 200 40 128 200 40 Prod No Desc List Price 12345 ELT l 1 78 88 0 00 SPR2290 Super strength spr ngs 24 00 STR67 Foot straps 2 50 InVOlce Invoice Cust Order HLM45 De uxe He met 67 66 No No Date SFR39l ROCket 128 200 40 12345 1454 1152006 ELT1 Emerg Trans 78 88 Databases in Practice Georgia Tech Q 0Q 00 0 OH Database Creation Queries and a g Updates 2 0 So far just talked about the logical structure of databases and database design 0 Now 0 How to write a program to create that logical structure 0 How to get data into the database 0 How to get data out of a database 0 Queries mechanism for talking to a database 0 Many mechanisms the one we ll focus on here is a common languagebased approach Georgia Tech SQL 0 The Structured Query Language O Textual language for creating database structure updates and queries 0 Pretty much the universal language for relational databases 0 Supported moreorIess the same across all RDBMS systems 0 Minor variances because vendors want to differentiate their products Quick n Dirty Overview Of SQL 0 Basic statements CREATE INSERT UPDATE DELETE SELECT 0 Basic column types INT REAL VARCHAR CHAWCHARn DATE TIME BOOLEAN OBJECT Georgia Tech T OOOO O 0 1g 4 1 Georgia Tech Creating a New Database Table 0 CREATE CACHED TABLE tablename col I col2 GI CACHED means it actually gets saved to disk Important 0 In the parens are the de nitions of the columns separated by commas o columnName type GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY O GENERATED BY DEFAULT AS IDENTITY means that the database automatically generates a value for this column and treats it as a key Issue the SQL statement CALL DENTTY to return the last generated value 0 PRIMARY KEY means that the column is considered a key column 0 Example CI CREATE CACHED TABLE booklnfo boole GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY title CHAR64 Storing New Data in the Georgia My Tech Database 2 O The INSERT statement adds a new row to a table 0 INSERT INTO tablename col I col2 VALUES val va2 0 Example 0 INSERT INTO customerlnfo name ssn VALUES keith l23456789 0 If you leave out the columns you have to provide values for all columns 0 Be sure to put single quotes around strings Georgia Tech Removing Rows from a Database O The DELETE statement removes rows from a database 0 DELETE FROM tablename WHERE condition 0 Example 0 DELETE FROM customerlnfo WHERE name keith O DELETE FROM books WHERE publicationYear lt 99 0 Without aWHERE clause DELETE removes all rows from the table SQL Conditions 0 DELETE and other SQL statements supportWHERE conditions that determine which rows they operate on 0 Common arithmetic operations 0 value lt lt gt gt ltgt value 0 value BETWEEN value AND value 0 Other fancier operations are supported as well Q Probably not needed for this assignment 0 Check SQL docs for more info Georgia Tech l Georgia Tech Updating Cells in Existing Rows 0 Use UPDATE to change cells in existing rows 0 UPDATE tablename SET columnexerHERE condition 0 Example 0 UPDATE booklnfo SET retailPrice 5999 WHERE isbn 078793 l2 0 Will update multiple rows that match the WHERE condition 0 Like DELETE leaving off the WHERE condition updates all rows in the table 0 Can have multiple columnexpr separated by a comma to update multiple fields in matching rows at the same time Querying the Database Georgia Tech SELECT is the workhorse of SQL lets you get data out of your database SELECT FROM tablename 0 Returns all rows from tablename SELECT columns FROM tablename 0 Returns only the speci ed columns from tablename SELECT DISTINCT columns FROM tablename 0 Removes duplicate values and returns the specified columns Tech Georgia Querying the Database cont d Q 0 SELECT columns FROM tablename WHERE condition Q Lets you select out only the rows that match the specified condition then return only the desired columns from those rows 0 SELECT columns FROM tablename WHERE condition ORDER BY column 0 Same as above only returns data ordered by the specified column 0 SELECT avgcolumn FROM tablename Q Can provide a function that applies to the selected column and aggregates the results 0 Other functions max min sum count Querying the Database cont d 0 Examples SELECT FROM books SELECT DISTINCT publicationYear FROM books Georgia Tech l SELECT title publicationYear FROM books WHERE retaiIPrice lt 5000 AND publicationYear gt I994 SELECT avgretailPrice FROM books SELECT isbntite publicationYear FROM books ORDER BY publicationYear 0 Using SELECT in practice O Bad idea do a very general select then piece the data together in your program Good idea et SELECT do the work for you See if you can get exactly the data you need in one SELECT statement Advanced SQL Joins Georgia Tech Everything so far has been about queries over single tables Possible to write queries that span tables Ci Very powerful feature of relational databases SELECT FROM books publishers WHERE boolltspublisherlD publisherspublisherlD AND publisherscity NewYork AND publishersstate NY Gets details of all books published in NY even though the information is spread across multiple tables books publishers bookID publisherID title author publisherID name city state l bookID publisherID title author name city state Tech Georgia Q Databases in the Wild 0 Lots of database products and freeware in the world 3 OracleAccess MySQL etc literally hundreds 0 Most are implemented as server processes your code connects to them over the network 6 Many are superhighend 0 Eg Oracle 0 Some are more consumer oriented 0 Access Filemaker 0 Others are embedded 0 Meaning the database code lives in a library inside your program rather than as a server 6 Pro easy to use easy to manage no need to startstop separate server 6 Con can t easily have multiple programs share the database at once Georgia Tech Hypersonic SQL httphsqldbsourceforgenet Freeware database written in Java Can be embedded or run as a server Download hsqldbjar 0 Available on class website 0 Contains the entire database implementation along with SQL interpreter etc Update your classpath to add this in 0 Same process as for the googleapi JAR file l Georgia Tech Using Databases from Java O Ideally you d like to be able to write your code so that you can swap out whatever database its using 0 To move higherend to move cheaper because of bugs whatever 0 Java has a cool API that works across databases 0 JDBC the Java Database Connector API 0 You initialize JDBC by telling it what specific database you ll be using and it does the rest Hides whether or not you re talking to a database server an embedded database etc 0 Since it s so widely used and easily callable from Java we ll use it in the next project JDBC Basics Load the JDBC driver for your speci c database Connect to the database Georgia Tech Issue queries in the form of plain text SQL statements Shutdown and disconnect i Loading the Driver and Georggci Connecting to the Database 3 import javasq as sql import javaang as Iang try angCassforName orghsqdbjdbcDriver newnstance except angCassNotFoundException print No JDBC driver found check classpath try connection sqDriverManagergetConnection jdbchsqIdbz lez dbname statement connectioncreateStatement except sqISQLException ex print Coudn t connect to database ex Tech Georgiagy Creating the Database 3 if it already exists we don t have to create it try statementexecuteQuery SELECT FROM CHATS print Database already created return except sqlSQLException pass try statementexecuteQuery CREATE CACH ED TABLE CHATS CHATID INT GENERATED BY DEFAULT AS IDENTITY STARTWITH I PRIMARY KEY INITIATOR CHAR64 START REAL except sqlSQLException ex print TroubIe creating CHATS table ex Tech Georgia My Adding Data to the Database try initiator Keith startTime timetime statementexecuteQuery NSERT INTO CHATS INITIATOR START VALUES quot initiator strstartTime except sqlSQLException ex print Troube inserting new row ex 0 Note use of quotes around string values and use of str to concatenate nonstring values into a string How do you know the key of theGegrr gci Q data you just added 5 0 Special trick 0 CALL DENTTY 0 Returns the key value for the most recently added row 0 As a ResultSet with one row and one column containing an integer value 0 Example try statementexecuteQuery lNSERT INTO CHATS INITIATOR START VALUES quot initiator strstartTime rs statementexecuteQuery CALL DENTTY if rsnext key rsgetlntl except sqlSQLException ex print Trouble inserting new row ex Tech Georgia Q Updating the Database try newTime timetime statementexecuteQuery UPDATE CHATS START VALUES strnewTime except sqlSQLException ex print Troube updating row ex Tech Georgiaiy Querying the Database E try rs statementexecuteQuery SELECT FROM CHATS while rsnext print Chat lDz rsgetlnt CHATD Initiatorz rsgetString NITIATOR Start Timez rsgetnt START except sqlSQLException ex print Troube executing query ex Georgia Tech Using ResultSets 0 ResultSetszthe Java objects returned from executeQuery 0 Think of it as a data structure with a built in cursor 0 Use next to move to the next row in the returned data 0 next will return a true value as long as there is more data 0 So while rsnext will work great 0 Need to use it once to position it on the rst row 0 If no rows are returned the rst call to rsnext will return a false value 0 Operations to get data out of the current row Q getlnt getString etc You need to know what the data type is for the row you want Q Can pass in the name of the column 9 Can pass in the integer index of the column 0 IMPORTANT SQL indices start with not 0 Tech Georgia Q Shutting Down Cleanly 0 Important You need to shut down cleanly to make sure all data gets written out 0 If you just ControlC your program you may lose the last updates 0 To be safe follow the following steps statementexecuteQuery SHUTDOWN statementcose connectioncose 0 SHUTDOWN writes everything to disk and cleans up nicely 0 If you want to make sure everything is flushed out each time you do something you can also call statementexecuteQuery CHECKPOI NT Georgia Tech Q r 0 There s often a path from user input all the way down into an SQL statement m DU CIeaning Your Input Frontend User Interface Main Program 4 Backend Database searchString abetext stmtexecuteQuery SELECT FROM PRODUCTS WHERE Name searchString CleaningYour Input cont d Tech Georgia Q stmtexecuteQuery SELECT FROM PRODUCTS WHERE Name searchString This line produces a string of SQL that embeds the contents of searchString SELECT FROM PRODUCTSWHERE Name Canon Powershot SD300 But what if the user enters a string that contains an apostrophe 0 Example user enters Canon s Powershot SD300 0 Concatenating this into the SQL query yields an illformed string 0 SELECT FROM PRODUCTS WHERE Name Canon s Powershot SD300 The apostrophe single quote mark prematurely terminates the string leaving random garbage after Solution 0 Prefilter out any special characters especially quotations that might screw up an SQL statement when concatenated in This is actually a common security hole What if the user enters Powershot DROP TABLE PRODUCTS 0 The string and single quote terminate the string prematurely 0 The semicolon tells SQL that the SELECT statement is over and a new statement is starting DROP TABLE PRODUCTS is interpreted as a new SQL command that deletes the products table SQL Injection Attack 0 Solution Use JDBC PreparedStatement class which automatically filters out special characters a Not required for this assignment Tech Georgia Q Hypersonic SQL Pragmatics O Debugging when using a database G Exceptions exceptions exceptions 0 Put every call to the database in a tryexcept block 0 Don t just swallow the exception print it 0 Print statements are your friend 0 Lots of building long strings of concatenated text variables etc 0 Print out the SQL strings you send to the database to check for correctness G Print the database contents itself 0 Feel like you re doing the right thing but getting the wrong results 0 You might have stored weird stuff in your database 0 Take the time to write a function that prints the entire database Tech Georgia Q Hypersonic SQL Pragmatics 0 Where does my data live 0 HSQLDB uses a number of les to store the data 0 These live in the directory from which you run the programl 9 For a database named foo 0 fooproperties the database s record of its parameters and con guration 0 fooscript the set of SQL statements executed up until the last session 0 foodata the actual data for any cached tables 0 foobackup complete backup of the database from the last session 0 fooog the set of SQL statements executed in the current session 0 After a clean shutdown 0 foodata is fully updated foobackup contains what s in foodata fooscript contains the statements executed in the last sessionthere is no fooog fooproperties contains modi ed no 0 After a nonclean shutdown 9 foodata may be corrupt foobackup is ok fooscript contains the SQL commands needed to create that backup fooog contains the info needed to get produce foodata fooproperties contains modi ed yes Georgia Tech Hypersonic SQL Pragmatics 0 Starting Over 0 Sometimes you just need to recreate the whole database 0 Two solutions 0 DROP TABLE tablename 0 By hand remove all of the database les mentioned previously l The Next Programming Project Georgia Tech l O We ll modify the client to store information about past chats in a database 0 Create dbpy to hold the code that interacts with the database 0 Connect to the database create the necessary structure update it as chats come and go 0 Will liker be called from both guipy and netpy 0 Augment guipy to add a history button 0 Should show all of the people you ve ever chatted with 0 Selecting one should show a history of all chats their text etc 0 Create one database per user 0 Name should match the logged in user Suggested Chat Database Georgia Q Tech MembersinC hats ChatID INT Member CHAR64 MessagesinChats Chats ChatlD Initiator Start INT CHAR64 REAL Key automatically Use name of Use time time generated NOTE participant different than server s Chat ID Messages MessagelD Sender Text Time INT CHAR64 CHAR256 REAL Key Use name of Sent text Time at automatically participant which text is generated sent Use time time ChatID INT MessagelD NT Georgia Tech Extra Tips 0 Doing the timerelated stuff import time get a floating point number with the number of seconds since January I I970 the start of UNIX timeThis value is suitable for storing in the database in columns of type REAL now timetime convert it to a more reasonable format for printing debugging etc timelocaltime takes one of the floating point things and returns a tuple year month day hour minute second dayOfVVeek dayOerar daylightSavings timelocaltimenow Suggested Coding Strategy Tech Georgia My Make a new le dbpy Create functions for all of the basic operations 0 connect 0 create 3 disconnect Create some lowlevel functions to access the database 0 getAllMembers 0 getChatlDsByMembermembername 0 getMembersByChatlDchatid 0 getMessagesByChatlDchatid 0 addChatinitiator starttime G addUserchatid newuser 9 add Messagechatid initiator text time Create a getHistory function that your GUI will call out to to get the data to build the history window Wrap everything in exception handling Print exceptions print SQL statements before you send them Create some debugging functions to print result sets print the entire database Asynchronous Programming OOOOOOG 00030 OOBQ oooogg Georgia Tech Week 3 Georgia Tech Turnin Instructions 0 A main le called guipy Q See last week s slides for how to make it main 0 I ll run it from the command line 0 Put in a ZIP le along with any additional needed files 0 Name the ZIP file yourIastnamezip 0 Send to me in email along with details on 0 Anything special I need to do to run it C What platform you developedtested it on 0 Anything else you think I should know 0 PROGRAM DUE I I59 TUESDAY 9I905 l Asynchronous Programming Georgia Tech Q 0 00 DQ 00 Georgia Tech Asynchronous Programming O Probably the most used idiom for interactive systems 0 Why Interactions with the real world 0 Must be prepared to respond to events external to your program 0 You don t know when these might occur Q They may come from multiple sources a user remote users sensors hardware devices 0 Also the single biggest mindshift away from doing simple straight line programs 0 A few canonical examples C GUls responsive to mice keyboard 0 Systems that interact with hardware interrupts 6 Collaborative tools multiple users each doing their own thing Tech Georgia Q Asynchrony and Modularity O Firsttime programmers 0 Try to do everything in line in one flow of control 0 Works only for trivial problems 0 How would you do an in line program that needs to respond to multiple event sources 0 NB It s actually possible In fact it s one of the ways that asynchronous programming works under the hood We ll talk about it later in the semester 0 Asynchronous programming requires that you break your program down into pieces that are invoked independently whenever any external event happens 0 Modularity Tech Georgia 4 Modularity is a Good Thing 0 Fortunately modularity is a good goal anyway 8 Break apart code into more manageable chunks abstraction 0 Keep the entanglements between chunks as simple as possible encapsulation 0 Corollary keep as few things global as possible 0 Treat each chunk as a back box that does a simple thing and does it well information hiding 0 Objectoriented programming is modularity on steroids an oversimpli cation O Modularity is important when even one person is working on it Q Easier to conceptualize the entire system chunk behavior into building blocks etc You can t make complexity go away completely but you can learn techniques to manage it Thinking Asynchronously Georgia Tech Asynchronouszthings can happen at arbitrary times Your program will probably have two types of code in it 0 Setup code that gets the initial windows on the screen does initialization etc W A collection of program chunks that respond to particular types of events that occur Some terminology W An event is some external occurrence Q The asynchronouslycallable program chunks are event handlers Q An event dispatcher is the thing that calls your event handlers it is typically provided by the system language library OS Your setup code will install your various event handlers so that the event dispatcher will know which ones to call Much of your program s logic will reside in the event handlers Tech Georgia Common Idiom GUI Callbacks Q O In many GUI systems event handlers are called callbacks C These are just functions that will be invoked when an event occurs 0 Typically they take a prede ned set of arguments what event happened etc 0 They are parts of your program that get called back when something happens 0 How you associate your specific callback with a particular type of event depends on the particulars of the dispatch system Example GUI Callbacks in Jython Gegggci with Swing import javaxswing as swing clef callbackevent print Button was pressed event window swingJFrame C56452 button swingJButton Press Me buttonactionPerformedcaback windowcontentPaneaddbutton windowpack windowshow 1 COO r 3 i 7 El 7 4 HE 515 Me Resu s Button was pressed javaawteventActionEvent ACTIONPERFORMEDcmdPress Mewhen72985371modifiersButton1 on javaxswingJButton 0087x29ayoutjavaxswingOverlayLayoutalignmentX 00alignmentYO5borderappleafAquaButtonBorde reb1670fags296maximumSizeminimumSizepre ferredSizedefaultlcondisabledcondisabedSelecte dIconmarginjavaxswingplafInsetsUlResource top3 eft1 4bottom3 rig ht1 4 paintBordertruepaint FocustruepressedIconrolloverEnabledfalserolloverl conrooverSelectedconseectedIcontextPress MedefaultCapabletrue The Details of EventBased Georr gci ly Programming in Swing 0 In Swing events are generated based on user input 0 Mouse clicks movement release 0 Key presses releases 0 Combinations of all of the above 0 Each widget gets to define what constitutes an event for it and how callbacks will be associated with it C buttonactionPerformed U istvaueChanged 0 Any given widget may allow multiple kinds of callbacks to be associated with it 0 panemousePressed Q panemouseReeased 0 panemouseCicked The Details of EventBased Georr gci ly Programming in Swing cont d E 0 Event dispatcher calls your code when the appropriate combination of user inputs occurs 0 Passes an event argument to your code 0 Specific details contained in the event depend on type of callback 0 buttonactionPerformed ActionEvent 0 source the widget that generated the event 0 timestamp when the event occurred 0 modi ers which keys were held down when the event occurred 0 istvaueChanged ListSeIectionEvent O rstlndex rst index of changed item 0 lastlndex last index of changed item 0 To get speci c details of any given event type look at the Java documentation httpjavasuncomj2e50docsapi or ask me or the TA The Details of EventBased Georr gci ig Programming in Swing cont d E 0 You can call your callbacks yourself 0 They re just normal functions 0 Simulate what happens when user input occurs 0 Make sure you return quickly from your event handlers 0 The program is waiting until you finish so that it can continue running 0 Common signs of a nonreturning callback 0 Program appears to freeze 0 Program window doesn t redraw 0 Buttons become inactive 00 and Asynchronous Gegggci lg Programming 3 0 Simple callbacks are a perfectly acceptable idiom they re the baseline of asynchronous programming 0 If you do much callback programming thoughyou begin to notice some common patterns 0 Often need to share some data across several related callbacks 0 Often need to keep track of what happened the last time you ran the caHback 0 There s a group of variables and related functions that are used only by the callback An Example import javaxswing as swing startx O starty 0 def pressCallbackevent global startx starty startx eventx starty eventy def releaseCallbackevent global startx starty graphics eventsourcegraphics graphicsdrawLinestartx starty eventx eventy if name main frame swingJFramequotSimple Drawing Programquot canvas swingJPane canvaspreferredSize 400 400 framecontentPaneaddcanvas framepack frameshow canvasmousePressed pressCallback canvasmouseReleased releaseCallback Georgia Tech l 00 and Asynchronous Georr gci ly Programming cont d S 0 Last mousedown position needs to be remembered until the next time the callback is invoked 0 Can t save in a local variable as it will be reset each time the callback is invoked 0 Option l keep all of this crosscallback information in global variables 0 Why is this a bad idea 0 The information is speci c to the drawing callbacks nothing else should use it 0 By making it globalyou increase program clutter and the mental cycles needed to manage it D Worse you run the risk that someone you will misunderstand what the global variables are for and reuse them for something else 00 and Asynchronous Georr gci ly Programming cont d S O The principle of data hiding 8 Keep data as close to the behavior it controls as possible 0 Keep it inaccessible to everything else that doesn t need to use it 0 The more of the inner workings of something you exposethe more likely it is to be used in the wrong way 0 Option 2 objectoriented programming provides a nice way to handle this 0 Each handler is an object that contains whatever information is necessary for it to execute properly 0 Internal state is not visible outside the handler object 0 Welldesigned objects will allow the user to use them only in the way they were intended Example of 00 Event Handling import javaxswing as swing class Draw def initsef frame swingJFramequotSimpIe Drawing Programquot canvas swingJPane canvaspreferredSize 400 400 framecontentPaneaddcanvas framepack frameshow eventHandIer EventHandIer canvasmousePressed eventHandIerpressCaIIback canvasmouseReleased eventHandIerreleaseCaIIback class EventHandIer def pressCaIIbacksef event selfstartx eventx selfstarty eventy def releaseCaIIbacksef event graphics eventsourcegraphics graphicsdrawLinesefstartx selfstarty eventx eventy if name main draw Draw Georgia Tech l Tech Georgia My Example of 00 Event Handling import javaxswing as swing class Draw def initsef frame swingJFramequotSimpIe Drawing Programquot canvas swingJPane canvaspreferredSize 400 400 framecontentPaneaddcanvas framepack frameshow eventHandIer EventHandIer canvasmousePressed eventHandIerpressCaIIback canvasmouseReleased eventHandIerreleaseCaIIback class EventHandIer def pressCaIIbacksef event selfstartx eventx selfstarty eventy Record of last XY positions are stored in the EventHandIer object Not easily visible outside the object easily shared among just these callbacks def releaseCaIIbacksef event graphics eventsourcegraphics graphicsdrawLinesefstartx selfstarty eventx eventy if name main draw Draw Objects As a Structuring Georgia My 0 Tech PrInCIple E 0 Very often the data in your program will have a natural structure 0 In a drawing program each drawing window will have its own contents current mode etc that is not shared by any other open windows 0 All of this information can be grouped together into a DrawingWindow object C One DrawingWindow object per open window 0 No need to make the information needed by it global 0 In a chat program each ongoing chat has its own list of users and its own message history 0 The user list history etc could be grouped into a Chat object 0 One Chat object per ongoing chat G No need to make all of this information global Tech Georgia My Creating Objects E 0 Where do new objects come from O In an eventdriven programthey usually are created in response to events 0 Example 0 User clicks New Chat button in GUI 0 Callback creates a new Chat object to represent the details of that chat def newChatevent chat Chat chatusers me buddyListselectedValue Managing Global State Effectively Tech Georgia My 0 Some times you really do need to store some stuff globally allMyChats def newChatevent chat Chat chatusers me buddyListselectedValue allMyChatsappendchat 0 Useful idiom keep track of objects through global data structure Q Q Lists and Dictionaries are very helpful here Use Lists for simple ordered collection of stuff Use Dictionaries when there s a natural identi er for stored objects Extra bonus since you update a collection by invoking a method on it and not assigning to it you avoid some of the scoping problems we talked about last week Distributed Applications Networking Basics OOOOOOG OOOOOQ li 0041 oooooo Georgia Tech Week 6 What is a Network 0 Depends on what level you re at 0 One person s network is another person s application 0 OSI Seven Layer Model Q The physical wire itself 0 Ethernet 802 lb 9 Routing protocols ooo oooo oooo Georgia ooo Tech 00 7 Application FTP HTTP SMTP etc 6 Presentation 5 Session 4 Transport TOP 3 Network IP 2 Data Link ARP RARP 1 Physical Ethernet For Our PurposeszThe Internet 0 We re application programmers O In terms of OSI we re de ningusing our own applicationlayer protocol 0 Sits atop TCPIP the lingua franca of the Internet 0 For almost every networked application you will ever want to buildthis will be the lowest layer in the stack you ll need to care about 000 0000 0000 Georgia 000 Tech 00 7 Application FTP HTTP SMTP etc 6 Presentation 5 Session 4 Transport TOP 3 Network IP 2 Data Link ARP RARP 1 Physical Ethernet Topology of the Internet googlecom gatechedu My Home Network Georgia Tech Q 400000 0 w yquot equot re 392 93 Tech Georgia Q Some Terminology Protocols 0 Protocols rules that facilitate information exchange among programs on a network 0 Example from human world roger and over for radio geeks 0 Similar to how you design the interfaces between objects in your program 0 A callback expects to get a certain set of parameters in a certain order G You need to know this in order to use the callback 0 Likewise 0 A networked program expects you to communicate with it in certain ways using certain messages in a known format 0 You need to know this in order to use the program Georgia Tech i 00 Some Terminology Servers 0 Server a generally longlived program that sits around waiting for connections to it 0 Examples web server mail server le server IM server 0 Server implies that it does something useful delivers a service 0 Web server provides access to HTML documents 0 Mail server allows retrieval sending organization of email messages 8 File server provides remote access to les and directories 0 IM server provides info about online users passes messages between them Tech Georgialy Some Terminology Clients 3 0 Client a program that connects to a server to use whatever service it provides 0 Examples 0 Web browser connects to web servers to accessview HTML documents 0 Mail client Outlook etc connects to mail servers for mail storage transmission 0 IM clients connect to IM servers to access info about who is on etc 0 Most servers can be connected to by multiple clients at the same time Some Terminology Host Georgia Tech 0 Host Simply a machine that s connected to the network 0 Generally running clients andor servers 0 The machine hosts a server l Georgia Tech The Next Phase of the Project 0 We ll be building the networking part of the IM and Social Networking programs 0 Enhancing the GUI code to talk to an either an IM server or peers on the networking 0 For the IM assignment 0 I ll provide a sample IM server and documentation on its protocol 0 For the Social Networking assignment 0 I ll provide a protocol spec and a lot of tips on how to get the thing working 0 Important concept understanding a protocol speci cation G Useful for when you want to write a program that talks to an existing server and thus has its own existing documented protocol 9 Side concept designing your own protocols 0 We ll talk about this but won t do it for the project unless you want to go nuts and get all fancy 0 Should give you experience in using basic Internetstyle networking debugging etc Tech Georgia Q WhatWillYou Have to Do Connect to the other machines 0 Know how to refer to it which machine do you want to connect to U Know how to perform the connection G Know how to deal with errors server is down etc 2 Send messages to it eg l m online now 0 Know how to marshall arguments 0 Know how to do the transmission 390 Know how to deal with errors server crashed while sending etc 3 Receive messages from it eg list of online users 0 Know how to unmarshall arguments 0 Know how to read data 0 Know how to deal with errors eg got unexpected data from server etc 4 Disconnect from it D This is the easy part Georgia Tech l Why All the Focus on Errors Networking in inherently errorprone Different than single application programming 0 Errors generally result from a bug and just crash entire program Networking errors may be caused by reasons outside of your control 0 Network is down server has crashed server slow to respond etc G During a chat I could shut my laptop and walk away 0 Someone could trip over the power cord for an access point 0 Networks can t even guarantee that messages will get from A to B Good goal robustness 0 Your program should survive the crash of another program on the network receiving malformed data etc Defensive programming