24
IEG4180 Tutorial 6 – Overlapping I/O; Project 3 Discussion Bosco, Fong Chi Hang (Acknowledgement: some materials in this tutorial are adopted from previous works by Shing and Zero.)

Tutorial 6

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Tutorial 6

IEG4180 Tutorial 6 – Overlapping I/O; Project 3 DiscussionBosco, Fong Chi Hang

(Acknowledgement: some materials in this tutorial are adopted from previous works by Shing and Zero.)

Page 2: Tutorial 6

Outline

•Traditional Blocking I/O & Overlapped I/O▫Event Object Signaling▫Alertable I/O

Page 3: Tutorial 6

Overlapped I/O•Advantages

▫Non-blocking▫Use application buffers to receive data directly

▫Allow posting multiple receive calls

Page 4: Tutorial 6

Overlapped I/O Create Overlapped Socket

• Use WSASocket() instead of socket()• Use normal bind(), accept(), connect() etc…

Page 5: Tutorial 6

Overlapped I/O Send & Receive Data

•For TCP, use▫WSASend()▫WSARecv()

•For UDP, use▫WSASendTo()▫WSARecvFrom()

Page 6: Tutorial 6

Overlapped I/O Receive• Important parameters for WSARecv and

WSARecvFrom:▫ Socket▫ Array of WSABUF structures▫ Number of elements in WSABUF array▫ WSAOVERLAPPED structure▫ Pointer to I/O completion routine (used for alertable I/O)

Page 7: Tutorial 6

Overlapped I/O Receive

•The return value▫Does not return the number of bytes received.▫Only tell you it success or error.▫SOCKET_ERROR may be returned even there

was no error.▫Use WSAGetLastError() to check, if error code

is WSA_IO_PENDING, it means there is no error!!!

Page 8: Tutorial 6

Overlapped I/O WSABUF

•The definition of buffer for overlapped I/O

• len▫The length of buffer▫Have to be filled in advance

•buf▫The memory space that actually hold the data

typedef struct __WSABUF{ u_long len; char FAR* buf;} WSABUF, *LPWSABUF;

Page 9: Tutorial 6

Overlapped I/O WSAOVERLAPPED structure

• A mean for notification

typedef struct _WSAOVERLAPPED{ DWORD Internal; DWORD InternalHigh; DWORD Offset; DWORD OffsetHigh; WSAEVENT hEvent;} WSAOVERLAPPED, *LPWSAOVERLAPPED;

• hEvent– Function call returns immediately, some mechanisms

are needed to determine the status and the completion of the request

– Used in event object notification

Page 10: Tutorial 6

Overlapped I/O The Model - Use of Event Object

ProcessPrevious Data

WSARecv()

Wait for Packet

Call Lower LayerReceive Routinewith App. Buffer

ApplicationApplication OSOS

Wait forCompletion Event

Need to Figure Out which Buffer is Being Filled (or Returned)

Page 11: Tutorial 6

Overlapped I/O Event Object Notification

• Create an event object

▫ Similar to Mutex and Semaphore, event objects also have signaled or nonsignaled state

▫ Pass this object to hEvent of the WSAOVERLAPPED structure

• To know when the I/O operation complete▫ WSAWaitForMultipleEvents()

• To retrieve the results of overlapped operations▫ WSAGetOverlappedResult()

• Reset the event object to nonsignaled state▫ WSAResetEvent()

• To free resources occupied by the event object▫ WSACloseEvent()

WSAEVENT WSACreateEvent(void);

Page 12: Tutorial 6

Overlapped I/O Alertable I/O-Introduction• Instead of using event object notification, make

the OS calls one of your functions when I/O operations complete

• Completion routines▫ Functions that will be called when I/O complete▫ Specified in the last parameter of WSASend() / WSASendTo() /

WSARecv() / WSARecvFrom()int i = WSARecvFrom(..., lpOverlapped, lpCompletionRoutine);

Page 13: Tutorial 6

Overlapped I/O Alertable I/O

Move Data Processing to the Completion Routine

WSARecv()

Wait for Packet

Call Lower LayerReceive Routinewith App. Buffer

ApplicationApplication OSOS

Sleep & Wait forAny Completion

Process Data

Page 14: Tutorial 6

Overlapped I/O Alertable I/O -Completion Routines

• cbTransferred▫ Number of bytes transferred▫ Equals to zero when connection is closed

• lpOverlapped▫ hEvent can be freely used by your code, just like the LPVOID

parameter in thread procedure• You have to manage the buffer usage yourself!

▫ For example, you issued 10 WSARecv() with 10 buffers▫ The data will be filled in the buffers according to the calling order▫ Reissue WSARecv() on processed buffers

void CALLBACK CompletionRoutine( IN DWORD dwError, /* the error code */ IN DWORD cbTransferred, /* in bytes */ IN LPWSAOVERLAPPED lpOverlapped, /* the structure of this I/O */ IN DWORD dwFlags);

Page 15: Tutorial 6

Overlapped I/O Alertable Wait state• The thread using alertable I/O has to enter

alertable wait state, so that the completion routines can be called

• To enter alertable wait state

▫ Just like the ordinary Sleep() Return when timeout or completion

DWORD SleepEx( DWORD dwMilliseconds, BOOL bAlertable /* set to true */);

Page 16: Tutorial 6

Project 3 Overview

•Project 3 is divided into three parts.▫Web Console NetProbe Server▫SuperNetProbe▫JavaNetProbe

Page 17: Tutorial 6

Web Console NetProbe Server• Extend the NetProbe Server in Project 2

▫ Web-UI: open a new TCP port to accept HTTP request from a web browser (Not necessary to use select-based I/O for this port)

• Use web browser to connect to the NetProbe Server, returning a webpage(a form) for user to configure▫ Maximum number of connections▫ Start/Stop the Server in receiving Clients’ connections▫ Killing a particular client connection

• Another page (with AJAX) to show▫ Number of concurrent transmissions▫ Statistics of each connection

Page 18: Tutorial 6

SuperNetProbe & JavaNetProbe•SuperNetProbe

▫Threading (Project 2)▫Message-Driven I/O (Project 2)▫Alertable overlapped I/O

•JavaNetProbe (with GUI)▫Threading▫New I/O

Page 19: Tutorial 6

HTTP Request

•Browser normally analyze the URL in:

•If no port given, browser normally determines the port number by protocol

•And send (suppose HTTP is used) the following to {host} at {port}GET /{…path…} HTTP/{…version supported…}

{protocol}://{host}{:port}/{…path…}

Page 20: Tutorial 6

HTTP Response•The server then responds:

•Status

HTTP/1.0 200 OK Date: Fri, 31 Dec 1999 23:59:59 GMTContent-Type: text/html Content-Length: 1354

<html> <body> …</body> </html>

200 OK:The request succeeded, and the resulting resource (e.g. file or script output) is returned in the message body.

Page 21: Tutorial 6

Introduction•AJAX = Asynchronous JavaScript and XML•Mainly based on

▫HTML (DOM)▫JavaScript▫XML

•Goods▫Smoother experience

No need to refresh the whole page Asynchronize request

▫Rich Internet Application•Reference:

▫http://www.w3schools.com

Page 22: Tutorial 6

Idea

•On specific event (e.g.: onload, onClick,…)▫Create XMLHttpRequest object▫Use XMLHttpRequest to submit further

requests▫Handle the response when necessary

Page 23: Tutorial 6

Example<html><script type="text/javascript">function ajax () { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp= new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp= new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert(“XMLHttpRequest object not supported!"); return false; } } }

xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4){ document.myForm.time.value= xmlHttp.responseText; } } xmlHttp.open("GET","time.asp",true); xmlHttp.send(null);}</script>

<body><form name="myForm“>Name: <input type="text" onkeyup="ajax ();“ name="username" />Time: <input type="text" name="time" /></form></body></html>

src: http://www.w3schools.com/ajax/ajax_server.asp

Page 24: Tutorial 6

Project Code<html> <head> <title>Web based console</title> <script type='text/javascript'>var xmlhttp;window.onload=GetStatistic;function GetStatistic(){ var UpdateDiv = document.getElementById('Ajax'); //…object creation in previous page xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4){ if(xmlhttp.status==200){ UpdateDiv.innerHTML = xmlhttp.responseText; setTimeout('GetStatistic()', 2000); }else{ setTimeout('GetStatistic()', 2000); } } }; xmlhttp.open('post','ajax',true); xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); //required by POST xmlhttp.send(null);

} </script> </head><body> <h1>NetProbe Server (Display Panel)</h1> <div id="Ajax"> <p>Current Number of Connections: 0</p> <p>Clients' Statistics</p> <form action="display" method="post"> <table width="800" border="1"> <tr> <td>Client IP Address</td> <td>Protocol</td> <td>Transmission Rate(Bps)</td> <td>Packet Size(Bytes)</td> <td>Bytes Transmitted(Bytes)</td> <td> Time Elapsed(s)</td> </tr> </table><input name="Refresh" type="submit" value="Refresh"> </form> </div> <a href="control">Control Panel</a> </body></html>

Add statistics