qr code crystal reports 2008 CASE STUDY 2: A PIPELINED HTTP SERVICE in Font

Printer QR Code JIS X 0510 in Font CASE STUDY 2: A PIPELINED HTTP SERVICE

CHAPTER 12 CASE STUDY 2: A PIPELINED HTTP SERVICE
QR Code Printer In None
Using Barcode drawer for Font Control to generate, create QR Code 2d barcode image in Font applications.
www.OnBarcode.com
Code 3/9 Generator In None
Using Barcode creator for Font Control to generate, create Code 39 Full ASCII image in Font applications.
www.OnBarcode.com
End SyncLock 'let other threads in Return coordinator End Function End Class The Coordinator has two important jobs, both related to threading: 1. To create a thread to run RemoveIdleConnections on 2. To protect the connection pool from concurrent changes Concurrent changes to the pool might occur if two clients call GetConnection at the same time, or if ConnectionPool.GetConnection and ConnectionPool.RemoveIdleConnections are called at the same time. To ensure that concurrent calls are handled correctly (i.e., one at a time), protect the calls to ConnectionPool with a Monitor.
Create UCC.EAN - 128 In None
Using Barcode encoder for Font Control to generate, create EAN / UCC - 13 image in Font applications.
www.OnBarcode.com
Encode Barcode In None
Using Barcode drawer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
The Connection Team
PDF-417 2d Barcode Generator In None
Using Barcode creation for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Drawer In None
Using Barcode generation for Font Control to generate, create Denso QR Bar Code image in Font applications.
www.OnBarcode.com
The connection Coordinator team handles the main tasks of establishing a connection to a remote server, sending requests, and getting responses. When requests are sent in a pipelined mode, meaning that multiple requests are sent before any responses come back, the connection Coordinator team also handles the process of matching responses with requests. Figure 12-9 shows the basic activity diagram of the team. The connection Coordinator team is led by CoordinatorConnection, which handles all the threading logic. The team operates as follows: CoordinatorRequestHandler gets a CoordinatorConnection from the connection pool and calls the Coordinator with a sequence number to use. The Coordinator creates an HttpRequest object and uses it to send the request to the server. The request thread is then blocked, and a semaphore referencing the thread is stored in the blockedRequests collection, using the sequence number as a key. The semaphore is actually an object of class ThreadSemaphore. Figure 12-10 shows the class diagram. When a response arrives, it contains a sequence number. The blockedRequests collection is then checked for a ThreadSemaphore with that sequence number. The response thread then stores the response in the ThreadSemaphore object and unblocks the request thread.
Data Matrix Creator In None
Using Barcode drawer for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
Create GS1 - 12 In None
Using Barcode creation for Font Control to generate, create GS1 - 12 image in Font applications.
www.OnBarcode.com
CHAPTER 12 CASE STUDY 2: A PIPELINED HTTP SERVICE
Printing Denso QR Bar Code In Objective-C
Using Barcode printer for iPhone Control to generate, create QR Code 2d barcode image in iPhone applications.
www.OnBarcode.com
QR Code Creation In None
Using Barcode creation for Office Word Control to generate, create QR image in Word applications.
www.OnBarcode.com
Create Connection
Barcode Creation In VB.NET
Using Barcode printer for .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Make Barcode In Visual Studio .NET
Using Barcode maker for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Create HTTP Request
Code-39 Recognizer In .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
QR Code 2d Barcode Encoder In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
www.OnBarcode.com
Wait for a Response
Make Barcode In .NET Framework
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
DataMatrix Decoder In Visual Studio .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Send Request with SequenceNumber
Read Barcode In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Printing PDF 417 In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
www.OnBarcode.com
Get Response's SequenceNumber
EAN / UCC - 14 Generation In Java
Using Barcode generator for Java Control to generate, create EAN / UCC - 14 image in Java applications.
www.OnBarcode.com
Encoding PDF-417 2d Barcode In None
Using Barcode maker for Office Excel Control to generate, create PDF-417 2d barcode image in Office Excel applications.
www.OnBarcode.com
Add Thread to blockedRequests
Find Request Thread
Return Response
Figure 12-9. The main activity diagram of the connection Coordinator team
ThreadSemaphore
-requestEvent -sequenceNumber -request -response -inUse -startTime +Wait() +Signal()
Figure 12-10. The class used to synchronize the request and response threads
CHAPTER 12 CASE STUDY 2: A PIPELINED HTTP SERVICE
Two fields of ThreadSemaphore are particularly important in the interaction between the request and response threads: requestEvent and response. The former is of type ManualResetEvent, which is a process synchronization class used to block and resume the request thread. The latter is of type string and is used by the response thread to store the response message. When the request thread is resumed, the Send method that was woken up fetches the response from ThreadSemaphore and returns it to the caller. Figure 12-11 shows the class diagram of the connection Coordinator team.
RequestQueue +Add() +Remove() +Get()
blockedRequests CoordinatorConnection receiver +Send() +Disconnect() connection
Receiver +Run() +Stop()
Connection -HostAddress -IsIdle -LastActivity +Connect() +Disconnect()
response
request
HttpRequest +Post()
HttpResponse -SequenceNumber -Body -Header +Get()
Figure 12-11. The class diagram of the connection Coordinator team The five Worker classes on the right side are all independent of each other. When a worker needs to send information back to the Coordinator, events are used. Listing 12-5 and Listing 12-6 show C# and VB .NET implementations of the Coordinator. Listing 12-5. The C# Implementation of CoordinatorConnection public class CoordinatorConnection { RequestQueue blockedRequests; // Receiver receiver; // Connection connection; // TimeSpan rxTimeout; // Thread receiverThread; //
a queue of blocked send threads listens for incoming traffic sends requests and handles responses time to wait for a response background receiver thread
CHAPTER 12 CASE STUDY 2: A PIPELINED HTTP SERVICE
public bool IsIdle { get {return connection.IsIdle;} } public CoordinatorConnection(string theHostAddress, TimeSpan theRxTimeout) { rxTimeout = theRxTimeout; blockedRequests = new RequestQueue(); connection = new Connection(theHostAddress); connection.OnConnected += new HttpService.Connection.ConnectedHandler(ConnectHandler); connection.OnDisconnected += new HttpService.Connection.DisconnectedHandler(DisconnectHandler); } // returns the response public string Send(string theLocalPath, int theSequenceNumber, string theMessage) { ThreadSemaphore semaphore = null; lock(this) { // we use a lock here, to prevent concurrent requests // from getting mixed together on their way out connection.Connect(); connection.LastActivity = DateTime.Now; HttpRequest request = new HttpRequest(connection.Socket); request.Post(theLocalPath, theSequenceNumber, theMessage); semaphore = blockedRequests.Add(theSequenceNumber, theMessage); } // block until the response arrives, or a timeout occurs if (!semaphore.Wait(rxTimeout)) throw new Exception("No response received"); // get the response from the semaphore byte[] response = semaphore.response; blockedRequests.Remove(theSequenceNumber); return Encoding.UTF8.GetString(response); } public void Disconnect() { connection.Disconnect(); } void HandleResponse(Socket theSocket) { // read the response HttpResponse response = new HttpResponse(theSocket); response.Get();
Copyright © OnBarcode.com . All rights reserved.