Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
XML Technology in E-Commerce Lecture 4 Case Study: XmlMessenger XML Technology in E-Commerce 2001 Lecture 4 Sheet 1 Lecture Outline • Instant Messaging; • Case Study: XMLMessenger – – – – – Communication between client and server; Application classes; Sending and receiving XML through network; User login; Sending and receiving messages; • Summary; XML Technology in E-Commerce 2001 Lecture 4 Sheet 2 Overview XML Technologies and Applications XML Docs Logical Structure Physical Structure DTD XML Schema Meta-Modeling XML Parser Well-formedness Validity SAX DOM Applications E-Commerce XML Technology in E-Commerce 2001 Lecture 4 Sheet 3 Instant Messaging An E-Commerce Application • One of the most popular communication services; • 175 million users are expected by 2002; • Deployment on new client types is expected: mobiles, PDAs, TVs; • Vendors: – – – – AOL; Yahoo; Infoseek; ICQ; XML Technology in E-Commerce 2001 Lecture 4 Sheet 4 Case Study XMLMessenger • Simple Client/Server E-Commerce application; • Demonstrates some of XML capabilities utilized by the current eCommerce systems: – Platform and language independent data representation; – Standardized data exchange between involved parties; – Communication protocol implementation; XML Technology in E-Commerce 2001 Lecture 4 Sheet 5 XML Messenger User 4 User 3 Messenger Server User 2 User 1 XML Technology in E-Commerce 2001 Lecture 4 Sheet 6 XML Messenger Login <users> <user>User 2</user> <user>User 3</user> </users> User 3 <update type=“login”> <user>User 1</user> </update> <user>User 1</user> <nameInUse/> User 1 XML Technology in E-Commerce 2001 <users> <user>User 2</user> <user>User 3</user> </users> Lecture 4 User 2 Sheet 7 XML Messenger Messages <message to=“User 2” from=“User 1”> Hello! </message> User 3 <message to=“User 2” from=“User 1”> Hello! </message> Messenger Server User 1 User 2 XML Technology in E-Commerce 2001 Lecture 4 Sheet 8 XML Messenger Logout <update type=“logout”> <user>User 1</user> </update> User 3 <update type=“logout”> <user>User 1</user> </update> <disconnect/> Messenger Server User 1 User 2 XML Technology in E-Commerce 2001 Lecture 4 Sheet 9 XML Messenger Class Diagram XML Technology in E-Commerce 2001 Lecture 4 Sheet 10 Class Responsibilities • MessengerServer: – Listens for new client connections; – Maintains an in-memory XML document with online users; – Manages a set of threads (one thread per client); • UserThread: – Communicates with a particular client; • MessengerClient: – Communicates with the corresponding thread on the server; – Manages client-side GUI; XML Technology in E-Commerce 2001 Lecture 4 Sheet 11 Class Responsibilities • ClientStatus: – Displays a list of online users; – Manages conversations with other users; • Conversation: – Displays the messages exchanged with a particular user; – Provides interface for entering a new message; XML Technology in E-Commerce 2001 Lecture 4 Sheet 12 XML Messenger Demo • Demo - Deitel 10, page 263; • Tools: – Java 1.2.2; – Java API for XML Processing (JAXP) 1.0.1; • Classes: jaxp.jar and parser.jar; • Demo files: – – – – – MessengerServer.java UserThread.java MessengerClient.java ClientStatus.java; Conversation.java XML Technology in E-Commerce 2001 Lecture 4 Sheet 13 Implementation Sending XML through network OutputStream output; Document message; try { ((XmlDocument)message).write(output); } catch (IOException e){ e.printStackTrace(); } This fragment is used in: • MessengerClient, method send; • UserThread, method send; XML Technology in E-Commerce 2001 Lecture 4 Sheet 14 Implementation Receiving XML from network InputStream input; int bufferSize = 0; bufferSize = input.available(); if (bufferSize > 0) { byte buf[] = new byte[bufferSize]; input.read(buf); InputSource source = new InputSource( new ByteArrayInputStream(buf)); Document message = builder.parse(source); if (message != null) messageReceived(message);} This fragment is used in UserThread.run and MessengerClient.runMessengerClient. XML Technology in E-Commerce 2001 Lecture 4 Sheet 15 Implementation User Login XML Technology in E-Commerce 2001 Lecture 4 Sheet 16 Implementation User Login XML Technology in E-Commerce 2001 Lecture 4 Sheet 17 User Login • Login message creation (MessengerClient.java): factory=DocumentBuilderFactory.newInstance(); builder=factory.newDocumentBuilder(); ……… public void loginUser(){ Document submitName = builder.newDocument(); Element root = submitName.createElement("user"); submitName.appendChild(root); root.appendChild( submitName.createTextNode(name.getText())); send(submitName); } XML Technology in E-Commerce 2001 Lecture 4 Sheet 18 User Login • Getting output and input streams (MessengerClient.java): public void runMessengerClient(){ ….……………………….. output = clientSocket.getOutputStream(); input = clientSocket.getInputStream();} • Sending an XML message through the network (method send): try { ((XmlDocument)message).write(output); } catch (IOException e) { e.printStackTrace();} XML Technology in E-Commerce 2001 Lecture 4 Sheet 19 User Login • Reading the XML message from the network stream (UserThread.java) public void run(){ …………………………………………….. int bufferSize = 0; bufferSize = input.available(); if ( bufferSize > 0 ) { byte buf[] = new byte[bufferSize]; input.read(buf); InputSource source = new InputSource( new ByteArrayInputStream(buf)); Document message = builder.parse(source); if ( message != null ) messageReceived(message); Class InputSource in package org.xml.sax XML Technology in E-Commerce 2001 Lecture 4 Sheet 20 User Login • Analyzing the message (UserThread.java): public void messageReceived(Document message){ Element root = message.getDocumentElement(); if (root.getTagName().equals("user")) { String enteredName= root.getFirstChild().getNodeValue(); if (server.findUserIndex(enteredName)!= -1) nameInUse(); else { send(server.getUsers()); username = enteredName; server.addUser(this); } } …………………………………………………………………. XML Technology in E-Commerce 2001 Lecture 4 Sheet 21 User Login • Initialization of the document with user names (MessengerServer.java): private Document initUsers() { Document init = builder.newDocument(); init.appendChild(init.createElement("users")); return init; } XML Technology in E-Commerce 2001 Lecture 4 Sheet 22 User Login • Adding new user (MessengerServer.java): public void addUser(UserThread newUserThread){ String userName = newUserThread.getUsername(); updateGUI( "Received new user: " + userName ); // notify all users of user's login updateUsers( userName, "login" ); // add new user element to Document users Element usersRoot = users.getDocumentElement(); Element newUser = users.createElement( "user" ); newUser.appendChild( users.createTextNode( userName ) ); usersRoot.appendChild( newUser ); updateGUI( "Added user: " + userName ); // add to Vector onlineUsers onlineUsers.addElement( newUserThread );} XML Technology in E-Commerce 2001 Lecture 4 Sheet 23 User Login • Notification for new user login (MessengerServer.java): public void updateUsers(String userName, String type){ Document doc = builder.newDocument(); Element root = doc.createElement( "update" ); Element userElt = doc.createElement( "user" ); doc.appendChild( root ); root.setAttribute( "type", type ); root.appendChild( userElt ); userElt.appendChild(doc.createTextNode(userName)); // send to all users for (int i = 0; i < onlineUsers.size(); i++){ UserThread receiver = (UserThread)onlineUsers.elementAt( i ); receiver.send( doc );} XML Technology in E-Commerce 2001 Lecture 4 Sheet 24 Message Sending XML Technology in E-Commerce 2001 Lecture 4 Sheet 25 Message Sending XML Technology in E-Commerce 2001 Lecture 4 Sheet 26 Message Sending • Construction of a message in the conversation window (Conversation.java): public void submitMessage(){ …………………… Document sendMessage; …………………… sendMessage = builder.newDocument(); Element root = sendMessage.createElement("message"); root.setAttribute( "to", target ); root.setAttribute( "from", clientStatus.getUser() ); root.appendChild( sendMessage.createTextNode( messageToSend )); sendMessage.appendChild( root ); client.send( sendMessage ); ……………………………… XML Technology in E-Commerce 2001 Lecture 4 Sheet 27 Message Sending • Message handling in MessengerServer: public void sendMessage(Document message){ Element root = message.getDocumentElement(); String from = root.getAttribute("from"); String to = root.getAttribute("to"); int index = findUserIndex(to); ……………………………………… UserThread receiver = (UserThread) onlineUsers.elementAt(index); receiver.send(message); ……………………………………… } XML Technology in E-Commerce 2001 Lecture 4 Sheet 28 Message Sending • Message handling on the recipient side (MessengerClient.java): public void messageReceived(Document message){ Element root = message.getDocumentElement(); ……………………… if(root.getTagName().equals("message")){ String from = root.getAttribute( "from" ); String messageText = root.getFirstChild().getNodeValue(); …………………… int index = findConversationIndex( from ); …………………… Conversation receiver = (Conversation) conversations.elementAt(index); receiver.updateGUI(from+": "+messageText); XML Technology in E-Commerce 2001 Lecture 4 Sheet 29 Summary • Demonstration of XML technology for a simple ECommerce application; • Implementation of a communication protocol with XML messages; • Handling of in-memory XML documents through DOM calls; • Sending and receiving XML through network connection; Read: Deitel 10; Assignment: • Modify the login message to include information about login time. Update the user interface accordingly; • DeitelEx 10.4 For more detailed explanation and some hints see the course site. XML Technology in E-Commerce 2001 Lecture 4 Sheet 30