Your SlideShare is downloading. ×
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Tutorial 6
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Tutorial 6

845

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
845
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 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.)
  • 2. Outline
    • Traditional Blocking I/O & Overlapped I/O
      • Event Object Signaling
      • Alertable I/O
  • 3. Overlapped I/O
    • Advantages
      • Non-blocking
      • Use application buffers to receive data directly
      • Allow posting multiple receive calls
  • 4. Overlapped I/O Create Overlapped Socket
    • Use WSASocket() instead of socket()
    • Use normal bind(), accept(), connect() etc …
  • 5. Overlapped I/O Send & Receive Data
    • For TCP, use
      • WSASend()
      • WSARecv()
    • For UDP, use
      • WSASendTo()
      • WSARecvFrom()
  • 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)
  • 7. 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!!!
  • 8. 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;
  • 9. 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
  • 10. Overlapped I/O The Model - Use of Event Object Need to Figure Out which Buffer is Being Filled (or Returned) Process Previous Data WSARecv() Wait for Packet Call Lower Layer Receive Routine with App. Buffer Application OS Wait for Completion Event
  • 11. 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);
  • 12. 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 );
  • 13. Overlapped I/O Alertable I/O Move Data Processing to the Completion Routine WSARecv() Wait for Packet Call Lower Layer Receive Routine with App. Buffer Application OS Sleep & Wait for Any Completion Process Data
  • 14. 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 );
  • 15. 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 */ );
  • 16. Project 3 Overview
    • Project 3 is divided into three parts.
      • Web Console NetProbe Server
      • SuperNetProbe
      • JavaNetProbe
  • 17. 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
  • 18. SuperNetProbe & JavaNetProbe
    • SuperNetProbe
      • Threading (Project 2)
      • Message-Driven I/O (Project 2)
      • Alertable overlapped I/O
    • JavaNetProbe (with GUI)
      • Threading
      • New I/O
  • 19. 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 …}
  • 20. HTTP Response
    • The server then responds:
    • Status
      • HTTP/1.0 200 OK Date: Fri, 31 Dec 1999 23:59:59 GMT
      • Content-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.
  • 21. 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
  • 22. Idea
    • On specific event (e.g.: onload, onClick,…)
      • Create XMLHttpRequest object
      • Use XMLHttpRequest to submit further requests
      • Handle the response when necessary
  • 23. Example <html> <script type=&quot;text/javascript&quot;> function ajax () { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp= new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;); } catch (e) { try { xmlHttp= new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;); } catch (e) { alert(“XMLHttpRequest object not supported!&quot;); return false; } } } xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4){ document.myForm.time.value= xmlHttp.responseText; } } xmlHttp.open(&quot;GET&quot;,&quot;time.asp&quot;,true); xmlHttp.send(null); } </script> <body> <form name=&quot;myForm“> Name: <input type=&quot;text&quot; onkeyup=&quot;ajax ();“ name=&quot;username&quot; /> Time: <input type=&quot;text&quot; name=&quot;time&quot; /> </form> </body> </html> src: http://www.w3schools.com/ajax/ajax_server.asp
  • 24. 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=&quot;Ajax&quot;> <p>Current Number of Connections: 0</p> <p>Clients' Statistics</p> <form action=&quot;display&quot; method=&quot;post&quot;> <table width=&quot;800&quot; border=&quot;1&quot;> <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=&quot;Refresh&quot; type=&quot;submit&quot; value=&quot;Refresh&quot;> </form> </div> <a href=&quot;control&quot;>Control Panel</a> </body> </html> Add statistics

×