Wednesday 19 November 2008

A Simple Multi-threaded Logger

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace SudhirMurthy.Logger {

public interface ILog {

void LogToDisk(string msg);

}

interface ILoggerService {
ILog ILog {
get;
}
}

public class LogFile : ILog {

static List<string> messages;

//WriteToDisk delegate
private delegate void WriteToDisk(object sender, string fileName);

//WriteToDisk event
private static event WriteToDisk writeToDisk;

public static void WriteLog(string fileName) {
//Raise the event
writeToDisk(new object(), (fileName));
}

public void SendToDisk(object sender, string fileName) {

StreamWriter sw = File.CreateText(fileName);
foreach (string s in messages) {
sw.WriteLine(s);
}
sw.Close();
}

public LogFile() {
messages = new List<string>();
WriteToDisk mydelegate = new WriteToDisk(SendToDisk);
writeToDisk = new WriteToDisk(SendToDisk);
}

public List<string> Messages {
get {
return messages;
}
}

#region ILog Members

public void LogToDisk(string msg) {
Monitor.Enter(this);
messages.Add(msg);
Monitor.Exit(this);
}

#endregion
}

public class LoggerService : ILoggerService {

//TODO: Need to make it threadsafe..
private static ILog logInstance = null;

public LoggerService() {
logInstance = new LogFile();
}

#region ILoggerService Members

public ILog ILog {
get {
return logInstance;
}
}

#endregion
}

public class LoggerClient1 {

LoggerService logService;

public LoggerClient1() {
logService = new LoggerService();
}

public void Log() {
logService.ILog.LogToDisk(string.Format(
"I am Logging: {0}", this.ToString()));
}
}

public class LoggerClient2 {
LoggerService logService;
public LoggerClient2() {
logService = new LoggerService();

}
public void Log() {
logService.ILog.LogToDisk(string.Format(
"I am Logging: {0}", this.ToString()));
}
}

public class TestApp {

LoggerClient1 a;
LoggerClient2 b;

public TestApp() {

a = new LoggerClient1();
b = new LoggerClient2();
}

public void logClient1() {
for (int i = 0; i < 1000000; i++) {
a.Log();
}
}

public void logClient2() {
for (int i = 0; i < 1000000; i++) {
b.Log();
}
}
}

class Program {

static void Main(string[] args) {

TestApp app = new TestApp();
Thread t1 = new Thread(new ThreadStart(app.logClient1));
Thread t2 = new Thread(new ThreadStart(app.logClient2));


try {
t1.Start();
t2.Start();
} catch (Exception) {
throw;
}
t1.Join();
t2.Join();

//The main thread should technically wait until
//t1 and t2 should have finished and then execute
//the following
LogFile.WriteLog("log.txt");
}

}

}



I have demonstrated a simple multi-threaded logging mechanism here. The threads t1 and t2 share the static instance variable 'List<string> messages' while logging. The LoggerClient classes LoggerClient1 and LoggerClient2 each use the LoggerService to log messages. The log is finally written to disk by the the main thread. The LogFile class uses the Monitor.Enter(this) and Monitor.Exit(this) methods to allow locking while shared access to writing the log. This synchronizes the threads t1 and t2 to perform shared access writes without corrupting or overlapping the write operations. Another important thing to notice here is the methods t1.Join() and t2.Join() which is used to make the main thread wait until the threads t1 and t2 have finished executing.


If you try removing the Monitor.Enter(this) and Monitor.Exit(this) lines from the above code, you end up getting weird line numbers in your log.txt file. (enable line no's in visual studio and see log.txt). i.e the count of the log will never be as expected (2 million in the above case). By putting the above lines back, you get the expected value which is 2million lines of log. :). It takes about 5-6 seconds on my dual core for threads t1 and t2 to finish logging and the main thread to write to disk. :).  A bloated 93.4 MB file!!



My thread on MSDN Forums.



C0de

Saturday 15 November 2008

Tricolor on the moon!!

2221255712_0f55253432

The probe, painted with the Indian flag, touched down at 2034 (1504 GMT) Friday the 14th of November 2008, the Indian Space Research Organisation (ISRO) said.

India has become the fourth nation to join the stuff-on-the-Moon club, after the Chandrayaan-1 spacecraft in lunar orbit successfully launched an impact probe at the lunar surface. The 35-kg impactor was blazoned with the Indian flag.

Mission Objectives

Carry out high resolution mapping of topographic features in 3D, distribution of various minerals and elemental chemical species including radioactive nuclides covering the entire lunar surface using a set of remote sensing payloads. The new set of data would help in unravelling mysteries about the origin and evolution of solar system in general and that of the moon in particular.

Realize the mission goal of harnessing the science payloads, lunar craft and the launch vehicle with suitable ground support system including DSN station, integration and testing, launching and achieving lunar orbit of ~100 km, in-orbit operation of experiments, communication/telecommand, telemetry data reception, quick look data and archival for scientific utilization by identified group of scientists.

 

chandrayaan-earth-tmc-2

chandrayaan-earth-tmc-1

chandrayaan-01 

Chandrayaan-1 spacecraft undergoing pre-launch tests

chandrayaan-03

Moon Impact Probe

 12

PSLV 11 Assembled and ready to go at sriharikota.

PSLV-C11-at-VAB

PSLV-C11 at Vehicle Assembly Building

PSLV-C11_Liftoff_ch6

INDIA SHOOTS TO THE MOON!. Photo of PSLV-C11 liftoff launching the Chandrayaan-1. 22 Oct 2008

MERA BHARAT MAHAAAAN! JAI HIND!

Chandrayaan - Payloads

Chandrayaan - Photo Gallery

Tricolor on the moon

Chandrayaan-1

Tuesday 11 November 2008

XHTML - A very simple primer.

XHTML is a reformulation of HTML in XML. XML is mainly made up of generic tags which can be used to define data. XHTML is derived from XML or putting it the other way, XML is the base for XHTML. XHTML is also very similar to HTML 4.1.

According to W3Schools,

“XHTML is a combination of HTML and XML (EXtensible Markup Language). XHTML consists of all the elements in HTML 4.01, combined with the strict syntax of XML.”

Tips to be XHTML 1.0 compliant

1. Start writing your tags and attribute names in lower cases

for eg. <BODY> - is wrong

<body> -  is correct.

<table WIDTH = “100%”> – is wrong

<table width = “100%” > – is correct

2. Always close your tags.

for eg. <p> this is a paragraph – is wrong

<p> this is a paragraph </p> – is correct

Be particularly aware when using tags like <img> <br> which do have a self closing tag in HTML 4.0,

These need to have a self-closing tag which is described as below.

<img src=”c:\\images\\alive.png”> – is wrong

<img src=”c:\\images\\alive.png” /> – is correct.

<br> – is wrong

<br/> – is correct

3. Nest your tags correctly

<ul>
  <li>Coffee</li>
  <li>Tea
    <ul>
      <li>Black tea</li>
      <li>Green tea</li>
    </ul>
  <li>Milk</li>
</ul>

- is wrong

 

<ul>
  <li>Coffee</li>
  <li>Tea
    <ul>
      <li>Black tea</li>
      <li>Green tea</li>
    </ul>
  </li>
  <li>Milk</li>
</ul>

- is correct

4. XHTML documents MUST have a root element - <html>

The document structure should look like

<html>
<head> ... </head>
<body> ... </body>
</html>

5. Attribute values MUST be in quotes

<div id=header >

</div> – is wrong

<div id=”header”>

</div> – is correct

<table width=”100%”> - is correct.

<table width=100%> -  is wrong.

6. Always add a  <!DOCTYPE> declaration to you page.

A DOCTYPE gives the browser information about the kind of HTML it expects.

The DOCTYPE declaration is always the first line in an XHTML document.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title> xhtml document</title>
</head>
<body>
<p> welcome to xhtml standards</p>
</body>
</html>

 

The XHTML DTD (Document type definition) defines the syntax of the XHTML markup.

 

XHTML 1.0 Strict

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

XHTML 1.0 Transitional

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Use the transitional DOCTYPE for valid markup and pages which render the same in all major browsers.

 

XHTML 1.0 Frameset

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

Use the frameset DOCTYPE when you want to use HTML frames.

 

How w3schools was converted to xhtml

 

Useful Links

The XHTML 1.0 markup language (Second Edition) - http://www.w3.org/TR/xhtml1/ 

The XHTML Validator -  http://validator.w3.org/

W3School’s XHTML Tutorial -  http://www.w3schools.com/xhtml/

Monday 3 November 2008

The Numerati – Stephan Baker

They’ve Got Your Number

walker-500

Maybe you’re the kind of person who doesn’t believe that the kind of person you are can be deduced by an algorithm and expressed through shorthand categorizations like “urban youth” or “hearth keeper.”

Maybe I’d agree with you, and maybe we’re right. But the kind of people — “crack mathematicians, computer scientists and engineers” — whom Stephen Baker writes about in “The Numerati” clearly see things differently. NYTimes Article