Private Const SERVER_NAME As String = "LEAD JPIP Server - Test Server"
Private Const ENUMERATION_SERVER_PORT As Integer = 109
Private Const JPIP_SERVER_PORT As Integer = 108
Private Const LOCAL_IP_ADDRESS As String = "127.0.0.1"
Private CLIENT_CACHE_DIRECTORY As String = LeadtoolsExamples.Common.ImagesPath.Path + "jpeg2000"
Private SERVER_CACHE_DIRECTORY As String = LeadtoolsExamples.Common.ImagesPath.Path + "jpeg2000"
Private SERVER_IMAGES_FOLDER As String = LeadtoolsExamples.Common.ImagesPath.Path + "jpeg2000"
Private IMAGE_FILE_EXTENSIONS As String = "*.j2k;*.jp2;*.jpx"
Private CLIENT_SAVED_FILE_NAME As String = LeadtoolsExamples.Common.ImagesPath.Path + "zzzzzzz.bmp"
Private _jpipServer As JpipServer
Private _imageFileExtensions As List(Of String)
Private _listener As HttpListener
Public _serverImagesList As List(Of String) = New List(Of String)()
Public Sub New()
Leadtools.Examples.Support.Unlock()
_imageFileExtensions = New List(Of String)()
_jpipServer = New JpipServer()
End Sub
Public Sub ClientRequestReceived(ByVal ar As IAsyncResult)
Dim context As HttpListenerContext
If Nothing Is _listener OrElse (Not _listener.IsListening) Then
Return
End If
Try
context = _listener.EndGetContext(ar)
Catch
Return
End Try
Try
Dim formattedImages As String
Dim sendBuffer As Byte()
_listener.BeginGetContext(AddressOf ClientRequestReceived, Nothing)
formattedImages = GetFormattedServerImagesString()
sendBuffer = Encoding.ASCII.GetBytes(formattedImages)
context.Response.OutputStream.Write(sendBuffer, 0, sendBuffer.Length)
context.Response.Close()
Catch e1 As Exception
If Not Nothing Is context Then
context.Response.Close()
End If
End Try
End Sub
Public Function GetFormattedServerImagesString() As String
Dim serverImageFile As String
Dim formattedServerImagesString As String = String.Empty
Dim searchImages As List(Of String) = New List(Of String)()
Dim serverImages As List(Of String) = New List(Of String)()
For Each extension As String In _imageFileExtensions
searchImages.AddRange(Directory.GetFiles(_jpipServer.Configuration.ImagesFolder, extension, SearchOption.AllDirectories))
Next extension
For Each file As String In searchImages
serverImageFile = file.Replace(_jpipServer.Configuration.ImagesFolder, String.Empty)
serverImageFile = serverImageFile.TrimStart("\"c)
formattedServerImagesString &= serverImageFile & ";"
Next file
searchImages.Clear()
For Each aliasFolder As KeyValuePair(Of String, String) In _jpipServer.Configuration.AliasFolders
If (Not Directory.Exists(aliasFolder.Value)) Then
Continue For
End If
For Each extension As String In _imageFileExtensions
searchImages.AddRange(Directory.GetFiles(aliasFolder.Value, extension, SearchOption.AllDirectories))
Next extension
For Each imageFile As String In searchImages
serverImageFile = imageFile.Replace(aliasFolder.Value, aliasFolder.Key)
serverImageFile = serverImageFile.TrimStart("\"c)
formattedServerImagesString &= serverImageFile & ";"
Next imageFile
searchImages.Clear()
Next aliasFolder
Return formattedServerImagesString.TrimEnd(";"c)
End Function
Public Sub StartFileEnumerationServer()
Try
_imageFileExtensions.Clear()
_imageFileExtensions.AddRange(IMAGE_FILE_EXTENSIONS.Split(";"c))
If Not _listener Is Nothing Then
Return
End If
_listener = New HttpListener()
_listener.Prefixes.Add(String.Format("http://{0}:{1}/", LOCAL_IP_ADDRESS, ENUMERATION_SERVER_PORT))
_listener.Start()
_listener.BeginGetContext(AddressOf ClientRequestReceived, Nothing)
Catch ex As Exception
Debug.Fail("Error in server enumeration: " & Constants.vbLf + ex.Message)
End Try
End Sub
Public Sub StopFileEnumerationServer()
Try
If Not _listener Is Nothing Then
_listener.Stop()
End If
Catch ex As Exception
Debug.Fail("Error in server enumeration: " & Constants.vbLf + ex.Message)
End Try
End Sub
Public Sub GetEnumeratedFiles(ByVal filename As String)
Dim request As HttpWebRequest = CType(HttpWebRequest.Create(String.Format("http://{0}:{1}/", LOCAL_IP_ADDRESS, ENUMERATION_SERVER_PORT)), HttpWebRequest)
If Not Nothing Is request.Proxy Then
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
End If
request.UseDefaultCredentials = True
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Dim receivedStream As System.IO.Stream = response.GetResponseStream()
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(receivedStream)
Dim read As Char() = New Char(255) {}
Dim count As Integer = reader.Read(read, 0, 256)
Dim imageNames As String = ""
Do While count > 0
Dim temp As String = New String(read, 0, count)
imageNames &= temp
count = reader.Read(read, 0, 256)
Loop
response.Close()
reader.Close()
Dim imageFileNames As String() = imageNames.Split(";"c)
Dim counter As Integer = 0
For Each image As String In imageFileNames
counter += 1
Debug.Assert(image.Trim().Length > 0, "There is no image to enumerate")
Debug.WriteLine("Image " & counter & ": " & image)
_serverImagesList.Add(image)
Next image
File.WriteAllLines(filename, _serverImagesList.ToArray())
End Sub
Public Sub StartJpipServer()
If (Not _jpipServer.IsRunning) Then
_jpipServer.Configuration.ServerName = SERVER_NAME
_jpipServer.Configuration.IPAddress = LOCAL_IP_ADDRESS
_jpipServer.Configuration.Port = JPIP_SERVER_PORT
_jpipServer.Configuration.ImagesFolder = SERVER_IMAGES_FOLDER
_jpipServer.Configuration.CacheFolder = SERVER_CACHE_DIRECTORY
_jpipServer.Configuration.MaxServerBandwidth = _jpipServer.Configuration.MaxServerBandwidth
_jpipServer.Configuration.CacheSize = 200
_jpipServer.Configuration.DivideSuperBoxes = True
_jpipServer.Configuration.ChunkSize = 512
_jpipServer.Configuration.MaxClientCount = 5
_jpipServer.Configuration.ConnectionIdleTimeout = New TimeSpan(0, 0, 9100)
_jpipServer.Configuration.MaxSessionLifetime = New TimeSpan(0, 0, 9900)
_jpipServer.Configuration.MaxConnectionBandwidth = _jpipServer.Configuration.MaxConnectionBandwidth
_jpipServer.Configuration.MaxTransportConnections = 15
_jpipServer.Configuration.HandshakeTimeout = New TimeSpan(0, 0, 9600)
_jpipServer.Configuration.RequestTimeout = New TimeSpan(0, 0, 920)
_jpipServer.Configuration.ChunkSize = 512
_jpipServer.Configuration.ImageParsingTimeout = New TimeSpan(0, 0, 9180)
_jpipServer.Configuration.PartitionBoxSize = 40
_jpipServer.Configuration.DivideSuperBoxes = True
_jpipServer.Configuration.LogInformation = False
_jpipServer.Configuration.LogWarnings = False
_jpipServer.Configuration.LogDebug = False
_jpipServer.Configuration.LogErrors = False
_jpipServer.Start()
Debug.Assert(_jpipServer.IsRunning, "Server did not start.")
End If
End Sub
Public Sub StopJpipServer()
If _jpipServer.IsRunning Then
_jpipServer.Stop()
End If
End Sub
Public Sub SetJpipViewer(ByVal viewer As JpipRasterImageViewer)
viewer.CacheDirectoryName = CLIENT_CACHE_DIRECTORY
viewer.PortNumber = JPIP_SERVER_PORT
viewer.IPAddress = LOCAL_IP_ADDRESS
viewer.PacketSize = 16384
viewer.RequestTimeout = New TimeSpan(0, 1, 0)
viewer.ChannelType = JpipChannelTypes.HttpChannel
End Sub
Public Sub SaveJpipViewerImageToFile(ByVal viewer As JpipRasterImageViewer, ByVal fileName As String)
RasterCodecs.Startup()
Dim rc As RasterCodecs = New Leadtools.Codecs.RasterCodecs()
rc.Save(viewer.Image, fileName, RasterImageFormat.Bmp, 24)
RasterCodecs.Shutdown()
End Sub
Private Sub OnClientError(ByVal sender As Object, ByVal e As ErrorEventArgs)
Debug.WriteLine(e.GetException().Message)
End Sub
Private Sub OnBytesLoaded(ByVal sender As Object, ByVal e As TotalBytesLoadedEventArgs)
Debug.WriteLine(" Total Bytes: " & e.ByteCount.ToString())
End Sub
Private Sub FileOpened(ByVal sender As Object, ByVal e As EventArgs)
Dim jpipViewer As JpipRasterImageViewer = DirectCast(sender, JpipRasterImageViewer)
Dim deleteCacheFilePriorTo As DateTime = New DateTime(2008, 8, 8)
Debug.WriteLine(Constants.vbLf & " FullImageSize: " & jpipViewer.FullImageSize.ToString())
Debug.WriteLine(" NumberOfColorComponents: " & jpipViewer.NumberOfColorComponents.ToString())
Debug.WriteLine(" NumberOfResolutions: " & jpipViewer.NumberOfResolutions.ToString())
Debug.WriteLine(" InteractiveMode: " & jpipViewer.InteractiveMode.ToString())
Debug.WriteLine(" PaintProperties: " & jpipViewer.PaintProperties.ToString())
Debug.WriteLine(" current CodeStreamIndex " & jpipViewer.CodeStream)
Debug.WriteLine(" NumberOfCodeStreams " & jpipViewer.CodeStreamCount)
Debug.WriteLine(" Available Resolutions")
Dim i As Integer = 0
Do While i < jpipViewer.NumberOfResolutions
Debug.WriteLine(" Resolution (" & i.ToString() & ") Size" & jpipViewer.GetResolutionSize(i).ToString())
i += 1
Loop
Debug.WriteLine(Constants.vbLf & "Open Image")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
Debug.WriteLine(" ComponentIndex: " & jpipViewer.ComponentIndex.ToString())
Debug.WriteLine(" CurrentResolutionIndex: " & jpipViewer.CurrentResolutionIndex.ToString())
jpipViewer.ZoomIn()
Debug.WriteLine(Constants.vbLf & " Zoomed in")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
Debug.WriteLine(" ComponentIndex: " & jpipViewer.ComponentIndex.ToString())
Debug.WriteLine(" CurrentResolutionIndex: " & jpipViewer.CurrentResolutionIndex.ToString())
jpipViewer.ZoomIn()
Debug.WriteLine(Constants.vbLf & " Zoomed in")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
Debug.WriteLine(" ComponentIndex: " & jpipViewer.ComponentIndex.ToString())
Debug.WriteLine(" CurrentResolutionIndex: " & jpipViewer.CurrentResolutionIndex.ToString())
jpipViewer.ComponentIndex = 0
Debug.WriteLine("Component Index value set to 1 ")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
Debug.WriteLine(" ComponentIndex: " & jpipViewer.ComponentIndex.ToString())
Debug.WriteLine(" CurrentResolutionIndex: " & jpipViewer.CurrentResolutionIndex.ToString())
jpipViewer.ZoomOut()
Debug.WriteLine(Constants.vbLf & " ZoomOut ")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
Debug.WriteLine(" ComponentIndex: " & jpipViewer.ComponentIndex.ToString())
Debug.WriteLine(" CurrentResolutionIndex: " & jpipViewer.CurrentResolutionIndex.ToString())
jpipViewer.ComponentIndex = -1
Debug.WriteLine("Component Index value set to 1 ")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
Debug.WriteLine(" ComponentIndex: " & jpipViewer.ComponentIndex.ToString())
Debug.WriteLine(" CurrentResolutionIndex: " & jpipViewer.CurrentResolutionIndex.ToString())
jpipViewer.Zoom(jpipViewer.GetResolutionSize(2))
Debug.WriteLine(" Resolution size set to 2")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
Debug.WriteLine(" ComponentIndex: " & jpipViewer.ComponentIndex.ToString())
Debug.WriteLine(" CurrentResolutionIndex: " & jpipViewer.CurrentResolutionIndex.ToString())
SaveJpipViewerImageToFile(jpipViewer, CLIENT_SAVED_FILE_NAME)
Dim count As Integer = jpipViewer.DeleteCacheFiles(deleteCacheFilePriorTo)
Debug.WriteLine(count.ToString() & " Cache File Deleted....")
If jpipViewer.IsActive Then
Debug.WriteLine(" JpipRasterImageViewer.IsActive Test ")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
End If
jpipViewer.Close()
If jpipViewer.IsActive Then
Debug.WriteLine(" JpipRasterImageViewer.IsActive Test (this should not happen becaue no image is loaded at this point")
Debug.WriteLine(" CurrentImageSize: " & jpipViewer.CurrentImageSize.ToString())
End If
StopJpipServer()
End Sub
Public Sub ClientExample()
StartJpipServer()
StartFileEnumerationServer()
GetEnumeratedFiles(LeadtoolsExamples.Common.ImagesPath.Path + "Client_Enumerated_Images.txt")
StopFileEnumerationServer()
Dim jpipViewer As JpipRasterImageViewer = New JpipRasterImageViewer()
SetJpipViewer(jpipViewer)
AddHandler jpipViewer.StreamingError, AddressOf OnClientError
AddHandler jpipViewer.TotalBytesLoaded, AddressOf OnBytesLoaded
AddHandler jpipViewer.FileOpened, AddressOf FileOpened
Debug.WriteLine(Constants.vbLf & " Open File: " & _serverImagesList(0))
jpipViewer.Open(_serverImagesList(0))
End Sub |