SnTT: When Distance is important.

hootsThursday_BLU.jpgOccasionally, you'll be in a situation where your application on your notes client needs to make a value judgement as to the "best" server to open an application on. Usually these are set up as tables within the application, and so forth. What I hope to show you today is a quick and painless way of doing this programmatically.

Now this is a fairly interesting discussion. Which is the best server ? Out of all the servers with our target application, the closest one is usually a good start. And out of the "closest" servers, the one that performs best (has least load, etc) might be best.

This SNTT shows you how to find the servers that are closest to you by measuring latency between you and your target servers, and choosing the server with the least latency. This does NOT allow you to choose between servers with the same latency.

Lotusscript - as we all know - is fairly cool stuff. However, sometimes you need to dive a little deeper, and call "C-API" Notes calls (or windows calls or unix calls, whatever) directly. So far so good. And by far the best resouce for calling C-API from Lotusscript is Normunds LS2CAPI web site. He also sells a book - for a very low sum of money - which I heartily recommend you purchase.

How do you find out which C-API calls you can make ? Well, you should download the Lotus Notes C-API Toolkit. Dont worry - we're not coding in C - we're just using the excellent reference material. (A good article on C-API coding can be found here, and remember MS is now allowing you to get Visual Studio 2005 express for *free*...). You can find the C-API toolkit here. Or if your extra-specially lazy, just look at the v6 reference guide here. I do recommend downloading the real kit, as you get the reference material in a Notes database, which is far easier than beating your head against a web browser all the time.

One of the calls within the C-API is NSFGetServerLatency call.

This is a "simple" C-API call, in that you dont need any crazy handle, memory locks or whatnot to track. Its a simple one-hit wonder.

Now, how do I use this call? As Normunds book explains in great detail, the C-API representation of this call is very platform specific. On some platforms integers are a particular size and order in memory, and on others, a different size and shape. It boils down to having to declare different types of calls for different platforms. Please buy Normunds book as this explains this in far greater detail and clarify than I can manage here. Oh - and you get a reference area which gives you all the relevant delaration calls..

So in terms of Lotusscript, we end up making a declaration such as:

Declare Function W32_NSFGetServerLatency Lib LIB_W32 Alias {NSFGetServerLatency} (_

Byval ServerName As Lmbcs String, _

Byval Timeout As Long, _

retClientToServerMS As Long, _

retServerToClientMS As Long, _

ServerVersion As Integer) As Integer

somewhere in your code. The Delclarations section is pretty good.

How do we invoke this ? Well, a sample piece of script to do this would be:

Public Function getServerLatency(strServer As String) As Long

   Dim nnServer As New NotesName(strServer)

   Dim ToServer As Long, fromServer As Long, ver As Integer

   Dim timeout As Long

   timeout = 1000

   Call W32_NSFGetServerLatency(nnServer.Canonical, timeout, toServer, fromServer, ver)

   getServerLatency = fromServer + ToServer

End Function

So you can see you call call this function, and get the latency (in milliseconds) on the network connection between you and the target server. Its then fairly easy to use a list to capture all your servers, and build up a table of latency, and therefore pick the one with the least latency..

(Please note that this code is specific in terms of platform to Windows 32-bit. If you want this on Mac, Linux, pSeries, iSeries, zSeries - go get Normunds book!)

In terms of the above code, I tested this against our development lab. Servers in the same location as I scored a latency of 0ms, servers in the same country (Scotland, London, etc) scored 30-40ms latency, and our servers in the US scored a latency of 50ms...

I have worked with very large organisations who had global links, and latency between Europe and South East Asia sometimes crept up to the 500ms level. Scary. And of course I've worked with offshore oil firms, who tend to use satellite to contact vessels and rigs, and there the latency could be anything up to "go for a coffee".

Technorati Tags: Show-n-Tell Thursday, Lotus Notes,

Updated to actually put the correct definition.. sigh...