Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sheet 1XML Technology in E-Commerce 2001Lecture 4 XML Technology in E-Commerce Lecture 4 Case Study: XmlMessenger.

Similar presentations


Presentation on theme: "Sheet 1XML Technology in E-Commerce 2001Lecture 4 XML Technology in E-Commerce Lecture 4 Case Study: XmlMessenger."— Presentation transcript:

1 Sheet 1XML Technology in E-Commerce 2001Lecture 4 XML Technology in E-Commerce Lecture 4 Case Study: XmlMessenger

2 Sheet 2XML Technology in E-Commerce 2001Lecture 4 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; Lecture Outline

3 Sheet 3XML Technology in E-Commerce 2001Lecture 4 Overview XML Technologies and Applications XML Docs Logical Structure Physical Structure DTD XML Schema Meta-Modeling XML Parser Well-formedness Validity SAXDOM Applications E-Commerce

4 Sheet 4XML Technology in E-Commerce 2001Lecture 4 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; Instant Messaging An E-Commerce Application

5 Sheet 5XML Technology in E-Commerce 2001Lecture 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;

6 Sheet 6XML Technology in E-Commerce 2001Lecture 4 XML Messenger User 1 User 2 User 3 User 4 Messenger Server Message to User 3 Message to User 1

7 Sheet 7XML Technology in E-Commerce 2001Lecture 4 XML Messenger Login User 1 User 2 User 3 LoginName In UseCurrent Users New User User 1 User 2 User 3 User 2 User 3 User 1

8 Sheet 8XML Technology in E-Commerce 2001Lecture 4 XML Messenger Messages User 1 User 2 User 3 Message to User 2 <message to=“User 2” from=“User 1”> Hello! <message to=“User 2” from=“User 1”> Hello! Messenger Server

9 Sheet 9XML Technology in E-Commerce 2001Lecture 4 XML Messenger Logout User 1 User 2 User 3 Logout Logout User 1 User 1 Logout User 1 User 1 Messenger Server

10 Sheet 10XML Technology in E-Commerce 2001Lecture 4 XML Messenger Class Diagram

11 Sheet 11XML Technology in E-Commerce 2001Lecture 4 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;

12 Sheet 12XML Technology in E-Commerce 2001Lecture 4 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;

13 Sheet 13XML Technology in E-Commerce 2001Lecture 4 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 Messenger Demo

14 Sheet 14XML Technology in E-Commerce 2001Lecture 4 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;

15 Sheet 15XML Technology in E-Commerce 2001Lecture 4 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.

16 Sheet 16XML Technology in E-Commerce 2001Lecture 4 Implementation User Login

17 Sheet 17XML Technology in E-Commerce 2001Lecture 4 Implementation User Login

18 Sheet 18XML Technology in E-Commerce 2001Lecture 4 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); } User Login

19 Sheet 19XML Technology in E-Commerce 2001Lecture 4 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();} User Login

20 Sheet 20XML Technology in E-Commerce 2001Lecture 4 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 User Login

21 Sheet 21XML Technology in E-Commerce 2001Lecture 4 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); } …………………………………………………………………. User Login

22 Sheet 22XML Technology in E-Commerce 2001Lecture 4 Initialization of the document with user names (MessengerServer.java): private Document initUsers() { Document init = builder.newDocument(); init.appendChild(init.createElement("users")); return init; } User Login

23 Sheet 23XML Technology in E-Commerce 2001Lecture 4 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 );} User Login

24 Sheet 24XML Technology in E-Commerce 2001Lecture 4 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 );} User Login

25 Sheet 25XML Technology in E-Commerce 2001Lecture 4 Message Sending

26 Sheet 26XML Technology in E-Commerce 2001Lecture 4 Message Sending

27 Sheet 27XML Technology in E-Commerce 2001Lecture 4 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 ); ……………………………… Message Sending

28 Sheet 28XML Technology in E-Commerce 2001Lecture 4 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); ……………………………………… } Message Sending

29 Sheet 29XML Technology in E-Commerce 2001Lecture 4 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); Message Sending

30 Sheet 30XML Technology in E-Commerce 2001Lecture 4 Demonstration of XML technology for a simple E- Commerce 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. Summary


Download ppt "Sheet 1XML Technology in E-Commerce 2001Lecture 4 XML Technology in E-Commerce Lecture 4 Case Study: XmlMessenger."

Similar presentations


Ads by Google