Sunday, June 15, 2008

Some info about Project Velocity CTP-1

Named caches are universal across the cluster.

Regions are local to any cache host.It may limiti the scalibility to the cache cluster.

There are three ports.
1- Cluster Port
2- Aribitration port
3- Cache port

Your application must implement the Cache Aside pattern. i.e. it may support its operation even if data is not available in the cluster or even if any server is not available.It should be able to access data directly from database.

There are two types of clients.
1- Simple:
2- Routing

A simple client has no routing capabilities. It also can not track where each cached object is stored.It can request data from only one cache host. If that host does not hold the data. It gets data from other cache host (if available) and presents it to the client.


Each client also has the option to store cached data locally.This feature is called Local Cache.The type of client is configured in the configuration file.


The configuration in the application, so that clients may access the cluster, is maintained in Application Configuration file (app.config)

The configuration of cluster is maintained in ClusterConfig.xml file. This file presents a single point of failure in Velocity which is not yet taken care of (Till CTP1)


Objects are serialized before they are stored on cache host. Before it is used by the client, it must be deserialized. So it is recommended to have a local cache.

Concurrency Model:
Velocity supports the following concurrency models.
1- Optimistic
2- Pessimistic

Cache Expiration and Eviction:
The expiration is configured on named cache level in ClusterConfig.xml file. A TTL (Time to Live) may be associated with an object at the time of "Put".

Eviction is done by using LRU (Least Recently Used) algorithm.

Tuesday, June 10, 2008

Sample code for FTP client using System.net (FTPWebRequest)


Imports System.Text
Imports System.Net
Imports System.IO

Public Class FTPClient
Private UserID As String
Private Password As String

Public Sub New(ByVal UserID As String, ByVal Password As String)
Me.UserID = UserID
Me.Password = Password
End Sub

Public Sub uploadFile(ByVal URI As String, ByVal UploadFileName As
String, ByVal LocalPath As String, ByVal LocalFileName As String, Optional
ByVal FTPUserID As String = "", Optional ByVal FTPPassword As String = "")
Dim completePath = LocalPath + "/" + LocalFileName
Dim fileInf As FileInfo = New FileInfo(completePath)

If UploadFileName = "" Then
UploadFileName = LocalFileName
End If

If FTPUserID = "" Then
FTPUserID = Me.UserID
End If

If FTPPassword = "" Then
FTPPassword = Me.Password
End If

Dim MyURI As String = URI + "/" + UploadFileName



Dim reqFTP As FtpWebRequest


Dim buffLength As Integer = 2048
Dim buff(buffLength) As Byte
Dim contentLen As Integer

Dim response As FtpWebResponse = Nothing

Dim fs As FileStream = fileInf.OpenRead()
Dim strm As Stream = Nothing

Try
reqFTP = CType(FtpWebRequest.Create(New Uri(MyURI)),
FtpWebRequest)
reqFTP.Credentials = New NetworkCredential(FTPUserID,
FTPPassword)
reqFTP.KeepAlive = False
reqFTP.Method = WebRequestMethods.Ftp.UploadFile
reqFTP.UseBinary = True
reqFTP.ContentLength = fileInf.Length
reqFTP.UsePassive = True

response = CType(reqFTP.GetResponse(), FtpWebResponse)

strm = reqFTP.GetRequestStream()
contentLen = fs.Read(buff, 0, buffLength)

While (contentLen <> 0)
strm.Write(buff, 0, contentLen)
contentLen = fs.Read(buff, 0, buffLength)
End While
Finally
strm.Close()
fs.Close()
response.Close()
End Try
End Sub


Public Function GetFileList(ByVal URI As String, Optional ByVal
FTPUserID As String = "", Optional ByVal FTPPassword As String = "") As
String()
Dim downloadFiles() As String
Dim result As StringBuilder = New StringBuilder()
Dim reqFTP As FtpWebRequest = Nothing
Dim response As WebResponse = Nothing
Dim reader As StreamReader = Nothing

If FTPUserID = "" Then
FTPUserID = Me.UserID
End If

If FTPPassword = "" Then
FTPPassword = Me.Password
End If

Try
reqFTP = CType(FtpWebRequest.Create(URI), FtpWebRequest)
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPUserID,
FTPPassword)
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory

response = reqFTP.GetResponse()

reader = New StreamReader(response.GetResponseStream())

Dim line As String = reader.ReadLine()

While Not line Is Nothing
result.Append(line)
result.Append("\n")
line = reader.ReadLine()
End While

result.Remove(result.ToString().LastIndexOf("\n"), 1)

downloadFiles = result.ToString().Split("\n")
Catch ex As Exception
downloadFiles = Nothing
Finally
reader.Close()
response.Close()
End Try

Return downloadFiles
End Function


Public Sub downloadFile(ByVal Uri As String, ByVal ToDownLoadFileName
As String, ByVal LocalPath As String, Optional ByVal LocalFileName As
String = "", Optional ByVal FTPUserID As String = "", Optional ByVal
FTPPassword As String = "")
Dim result As StringBuilder = New StringBuilder()
Dim reqFTP As FtpWebRequest = Nothing
Dim response As FtpWebResponse = Nothing
Dim reader As StreamReader = Nothing

Dim ftpStream As Stream = Nothing
Dim outputStream As FileStream = Nothing

If FTPUserID = "" Then
FTPUserID = Me.UserID
End If

If FTPPassword = "" Then
FTPPassword = Me.Password
End If

If LocalFileName = "" Then
LocalFileName = ToDownLoadFileName
End If

Try
outputStream = New FileStream(LocalPath + "\\" + LocalFileName,
FileMode.Create)
reqFTP = CType(FtpWebRequest.Create(Uri + "/" +
ToDownLoadFileName), FtpWebRequest)
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPUserID,
FTPPassword)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile


response = CType(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()

Dim cl As Long = response.ContentLength
Dim bufferSize As Integer = 2048

Dim readCount As Integer
Dim buffer(bufferSize) As Byte
readCount = ftpStream.Read(buffer, 0, bufferSize)

While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
Finally
ftpStream.Close()
outputStream.Close()
response.Close()
End Try
End Sub
End Class