Public Class RunServerExample
Private _server As JpipServer
Private _logWriter As WindowsLogWriter
Private _responseCount As Integer
Private _loggingFileName As String
Private _loggingFileEnabled As Boolean
Private Const LOCAL_IP_ADDRESS As String = "127.0.0.1"
Private Const SERVER_NAME As String = "LEAD JPIP Server - Test Server"
Private Const PORT_107 As Integer = 107
Private CACHE_DIRECTORY As String = LeadtoolsExamples.Common.ImagesPath.Path + "jpeg2000"
Private IMAGES_DIRECTORY As String = LeadtoolsExamples.Common.ImagesPath.Path + "jpeg2000"
Private IMAGE_NAME As String = LeadtoolsExamples.Common.ImagesPath.Path + "Earth8000_Precint_4_.j2k"
Public Sub New()
Leadtools.Examples.Support.Unlock()
_server = New JpipServer()
_logWriter = New WindowsLogWriter()
_responseCount = 0
_loggingFileEnabled = True
_loggingFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Server Logs\log.txt"
End Sub
Public Sub StartServer()
ResetServerConfiguration()
AddHandler _server.RequestReceived, AddressOf _server_RequestReceived
AddHandler _server.ResponseSending, AddressOf _server_ResponseSending
AddHandler EventLogger.EventLog, AddressOf EventLogger_EventLog
_server.ClearCacheContents()
If (Not EventLogger.ContainsLoggingChannel(_logWriter)) Then
EventLogger.AddLoggingChannel(_logWriter)
End If
EventLogger.LogEvent(SERVER_NAME, LOCAL_IP_ADDRESS, PORT_107.ToString(), "", "", DateTime.Now, Leadtools.Jpip.Logging.EventLogEntryEventType.Information, "", "LEAD Example Started")
_server.Start()
Debug.Assert(_server.IsRunning, "Server did not start.")
Dim jpipViewer As JpipRasterImageViewer = New JpipRasterImageViewer()
SetViewer(jpipViewer)
AddHandler jpipViewer.FileOpened, AddressOf jpipViewer_FileOpened
jpipViewer.Open(IMAGE_NAME)
End Sub
Private Sub jpipViewer_FileOpened(ByVal sender As Object, ByVal e As EventArgs)
Dim jpipViewer As JpipRasterImageViewer = DirectCast(sender, JpipRasterImageViewer)
jpipViewer.ZoomIn()
jpipViewer.Close()
If _server.IsRunning Then
_server.Stop()
End If
EventLogger.LogEvent(SERVER_NAME, LOCAL_IP_ADDRESS, PORT_107.ToString(), "", "", DateTime.Now, Leadtools.Jpip.Logging.EventLogEntryEventType.Information, "", "LEAD Example Ended")
End Sub
Private Sub EventLogger_EventLog(ByVal sender As Object, ByVal e As Leadtools.Jpip.Logging.EventLogEntry)
Console.WriteLine("Server Name: {0}; Server IP: {1}; Server Port: {2}; Client IP: {3}; Client Port: {4}; Channel ID: {5}; Event Type: {6}; Description: {7}", e.ServerName, e.ServerIPAddress, e.ServerPort, e.ClientIPAddress, e.ClientPort, e.ChannelID, e.Type.ToString(), e.Description)
End Sub
Private Sub _server_ResponseSending(ByVal sender As Object, ByVal e As ResponseSendingEventArgs)
Dim dataFolder As String
Dim dumpClientIP As String
Dim dumpClientPort As Integer
dataFolder = LeadtoolsExamples.Common.ImagesPath.Path + "ServerResponses"
dumpClientIP = "127.0.0.1"
dumpClientPort = 121
If (Not Directory.Exists(dataFolder)) Then
Directory.CreateDirectory(dataFolder)
End If
If (e.ClientIpAddress = dumpClientIP) AndAlso (e.ClientPort = dumpClientPort) Then
Dim subDirectoryPath As String
Dim responseDataFile As String
_responseCount += 1
subDirectoryPath = String.Concat(dataFolder, "\", dumpClientIP)
responseDataFile = String.Concat(subDirectoryPath, "\response_", _responseCount)
If (Not Directory.Exists(subDirectoryPath)) Then
Directory.CreateDirectory(subDirectoryPath)
End If
File.WriteAllBytes(responseDataFile, e.Data)
End If
End Sub
Private Sub _server_RequestReceived(ByVal sender As Object, ByVal e As RequestReceivedEventArgs)
Dim denyClientIpAddress As String = "127.0.0.1"
Dim denyClientPort As Integer = 120
If (e.ClientIpAddress = denyClientIpAddress) AndAlso (e.ClientPort = denyClientPort) Then
e.Deny = True
e.StatusCode = CInt(HttpStatusCode.Forbidden)
e.Description = "Server refused to process request."
End If
End Sub
Public Sub ResetServerConfiguration()
If (Not _server.IsRunning) Then
Dim appConfigSettings As JpipConfiguration
appConfigSettings = New JpipConfiguration()
_server.Configuration.ServerName = SERVER_NAME
_server.Configuration.IPAddress = LOCAL_IP_ADDRESS
_server.Configuration.Port = PORT_107
_server.Configuration.ImagesFolder = IMAGES_DIRECTORY
_server.Configuration.CacheFolder = CACHE_DIRECTORY
_server.Configuration.MaxServerBandwidth = ConfigurationDefaults.MaxServerBandwidth
_server.Configuration.CacheSize = appConfigSettings.CacheSize
_server.Configuration.DivideSuperBoxes = True
_server.Configuration.ChunkSize = 512
_server.Configuration.MaxClientCount = 5
_server.Configuration.ConnectionIdleTimeout = TimeSpan.FromSeconds(ConfigurationDefaults.ConnectionIdleTimeout)
_server.Configuration.MaxSessionLifetime = TimeSpan.FromSeconds(ConfigurationDefaults.MaxSessionLifetime)
_server.Configuration.MaxConnectionBandwidth = ConfigurationDefaults.MaxConnectionBandwidth
_server.Configuration.MaxTransportConnections = 15
_server.Configuration.HandshakeTimeout = TimeSpan.FromSeconds(ConfigurationDefaults.HandshakeTimeout)
_server.Configuration.RequestTimeout = TimeSpan.FromSeconds(ConfigurationDefaults.RequestTimeout)
_server.Configuration.ChunkSize = ConfigurationDefaults.ChunkSize
_server.Configuration.ImageParsingTimeout = TimeSpan.FromSeconds(ConfigurationDefaults.ImageParsingTimeout)
_server.Configuration.PartitionBoxSize = 30
_server.Configuration.DivideSuperBoxes = appConfigSettings.DivideSuperBoxes
_server.Configuration.LoggingFile = _loggingFileName
_server.Configuration.EnableLogging = _loggingFileEnabled
_server.Configuration.LogInformation = True
_server.Configuration.LogWarnings = True
_server.Configuration.LogDebug = True
_server.Configuration.LogErrors = True
AddAliasFolders()
End If
End Sub
Private Sub AddAliasFolders()
Dim alias_Renamed As String = "LeadImages"
Dim physicalPath As String = LeadtoolsExamples.Common.ImagesPath.Path
If _server.Configuration.IsAliasExists(alias_Renamed) Then
If _server.Configuration.GetAliasPath(alias_Renamed) <> physicalPath Then
_server.Configuration.RemoveAliasFolder(alias_Renamed)
Else
Return
End If
End If
_server.Configuration.AddAliasFolder(alias_Renamed, physicalPath)
End Sub
Public Sub SetViewer(ByVal viewer As JpipRasterImageViewer)
viewer.CacheDirectoryName = CACHE_DIRECTORY
viewer.PortNumber = PORT_107
viewer.IPAddress = LOCAL_IP_ADDRESS
viewer.PacketSize = 16384
viewer.ChannelType = JpipChannelTypes.HttpChannel
End Sub
Private Class WindowsLogWriter : Implements ILogginChannel
Public Sub WriteLog(ByVal logEntry As Leadtools.Jpip.Logging.EventLogEntry) Implements ILogginChannel.WriteLog
Dim message As String
message = String.Format("Server Name: {0}; Server IP: {1}; Server Port: {2}; Client IP: {3}; Client Port: {4}; Channel ID: {5}; Event Type: {6}; Description: {7}", logEntry.ServerName, logEntry.ServerIPAddress, logEntry.ServerPort, logEntry.ClientIPAddress, logEntry.ClientPort, logEntry.ChannelID, logEntry.Type.ToString(), logEntry.Description)
System.Diagnostics.EventLog.WriteEntry("JPIP LEAD Server", message)
End Sub
End Class
End Class |