Monday, June 11, 2007

Implementation of Server on Grid System: A Super Computer Approach

The Internet technology has already changed the Information Society in profound ways, and will continue to do so. Nowadays many people foresee that there is a similar trajectory for the next generation of Internet - Grid Technology. As an emerging computational and networking infrastructure, Grid Computing is designed to provide pervasive, uniform and reliable access to data, computational and human resources distributed in a dynamic, heterogeneous environment. Also the development of Grid Security provides a secured field for work. That’s why; we have used the Grid Technology for implementing a server that response to many Clients for any database queries or any other database applications. Our design provides single level distribution of Grid Applications that make the faster response time and faster throughput than the normal server application and even that of distributed application. Here we have used the open source software “Alchemi” in all through our work. This document summarizes the design and implementation of a server using Grid Technology and compares its performance.

published at 15th International Conference of Information System and Development, Budapest, Hungary, Aug, 2006.

Runtime Type/Class generator or modifier with System.Reflection.Emit

Hi,
C# gives you so much flexibility to generate runtime type or modify any type at runtime.
Recently i explore runtime type generation or modification.
Hope every body found it interesting.

What i want to do is,
I have a interface actually i have type which is an interface and I want to generate a class implementing that interface.

Original code should be :

//interface
public interface IEcho
{
string Echo(string message);
}
//Implementor
public class EchoProxy : IEcho
{
public string Echo(string message);
}

now I write a method which will take the type of interface as parameter and return the object of dynamic class.


object GetObject(Type interfaceType)
{
AppDomain proxyDomain = Thread.GetDomain();
// Get the domain of the current thread
AssemblyName proxyAssemblyName = new System.Reflection.AssemblyName();
// You must create a runtime assembly for the type, so you need a name for that assembly
// declare assembly name for dynamic types.
proxyAssemblyName.Name = "ProxyAssembly";
// The name of the newly created assembly is ProxyAssembly.
AssemblyBuilder proxyAssemblyBuilder = proxyDomain.DefineDynamicAssembly(proxyAssemblyName, System.Reflection.Emit.AssemblyBuilderAccess.Run);
// Now you have to create assembly builder to build the assembly builder access option here I use just Run
ModuleBuilder proxyModuleBuilder = proxyAssemblyBuilder.DefineDynamicModule("ProxyModule");
// Now I build a module to hold my dynamic type from assembly builder.
TypeBuilder proxyBuilder = proxyModuleBuilder.DefineType("EchoProxy"
);
// And finally I create a type builder from module builder for dynamic type. The name of the new type is EchoProxy.
proxyBuilder.AddInterfaceImplementation(
interfaceType);
// Here I add the interface to the class, now my dynamic type implement the interface
MethodInfo[] methods =
interfaceType.GetMethods();
// Retrive the collection of methods from the interface.
for (int methodIndex = 0; methodIndex <
methods.Length ; methodIndex++)
{
MethodInfo currentMethod = methods[methodIndex];
// Obtain the current method.
Type[] paramType = new Type[realMethod.GetParameters().Length];
// Create an array of type which will passes as parameter.
for (int parameterIndex = 0; parameterIndex <
realMethod.GetParameters().Length; parameterIndex++)
paramType[parameterIndex] = currentMethod.GetParameters()[parameterIndex].ParameterType;
// Assign the type from interface
MethodBuilder proxyMethodBuilder = proxyBuilder.DefineMethod(
currentMethod.Name, currentMethod.Attributes, currentMethod.CallingConvention, currentMethod.ReturnType, paramType);
// Now create a method builder from type builder, with the specific parameter and return type.
// And the most annoying portion of the total work begin here.
// you have to write IL/OpCode here.
ILGenerator codeBlockGenerator = proxyMethodBuilder.GetILGenerator();
// Create a ILGenerator from Method builder
// Which helps you to flush the IL into IL stream.
codeBlockGenerator.Emit(OpCodes.Nop);
// OpCode = NOP
// It is practice to start a method with nop
codeBlockGenerator.EmitWriteLine("This is Proxy");
// OpCode = Print Line
codeBlockGenerator.Emit(OpCodes.Ldarg_0);
// Load the argument into evolution stack
codeBlockGenerator.Emit(OpCodes.Ret);
// Just return
// Return statement always pop last item from evolution stack and return it
}
return System.Activator.CreateInstance(
proxyBuilder.CreateType());
// Create a dynamic type from proxy builder and I create an instance from that with activator
// And return the instance of that type.
}

Now I am ready to use this method
public UseDynamicInstance()
{
IEcho simpleEcho =
GetObject(typeof(IEcho)) as IEcho;
if(
simpleEcho != null)
{
simpleEcho.Echo("Are you Ok");
}
}

Now what I am trying to show is that you can create runtime type and create an instance of it.
I find this helpful to create dynamic proxy Item
Hope you will find it so.
And you must include System.Reflection.Emit for these classes.

Monday, May 28, 2007

InnoCielo Release Party

This is a part of fun of our spirited team after releasing a successful product :-)

Thursday, April 12, 2007

Easiest way to Serialize/Deserialize in C#

string Serialize(object ObjectData)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, ObjectData);
byte[] bArray;
bArray = stream.ToArray();
stream.Position = 0;
string tempobjectdatastring = Convert.ToBase64String(bArray);
stream.Close();
return tempobjectdatastring;

}
object DeSerialize(string datastring)
{
byte[] bArray;
bArray = Convert.FromBase64String(datastring);
MemoryStream stream = new MemoryStream();
stream.Position = 0;
stream.Write(bArray, 0, bArray.Length);
stream.Position = 0;
BinaryFormatter bformatter = new BinaryFormatter();
object tempobjectdata = bformatter.Deserialize(stream);
stream.Close();
return tempobjectdata;
}

Easiest way to Clone in C#

public object Clone()
{
MemoryStream stream = new MemoryStream();
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, this);
stream.Position = 0;
return bformatter.Deserialize(stream);
}

Saturday, April 7, 2007

Load any class dynamically from DLL

If you wanna load any class or create an instance of any class dynamically from any Dll
You may use "Activator"
System.Activator(,)
Use Unwrap() to typecast into specific class...

Wanna Save something for request life cycle...

Do you want to save something for request life cycle?

ASP.NET 2.0 provide you a solution to save something for the request life cycle.
And you can access it from anywhere.
with httpContext.Current.Items["Key"]
In the previous blog I have showed that how to use control state,
you can save the page state similarly.

When you load it just put it with specified keys,
then any one can access it for that postback cycle,
And this will load before anything so don't worry...

Difficulties With ViewState(Solve . . .)

Are you facing some difficulties with ViewState ?
ASP.NET 2.0 provide you a solution for managing ViewState
Which is called Control State

You just put your private variable in control state and write the code for which variable you want to save and load, ASP.NET will load and save them for you... Wow!

And to do that you have to override write the following code :

1. override void OnInit(EventArgs e)
2. override object SaveControlState()
3. override void LoadControlState(object state)

Typically "override void OnInit(EventArgs e)" method have the following code :

override void OnInit(EventArgs e)

{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}



And I suggest a data structure for the next two one:
Use a dictionary with Dictionary
use your variable name a key and your variable as object
Like the following code:

override object SaveControlState()
{
Pair newState = new Pair();
newState.First = base.SaveControlState();
Dictionary privateMemebrs = new Dictionary();
privateMemebrs["a"] = this.a;
privateMemebrs["b"] = this.b;
privateMemebrs["c"] = this.c;
newState.Second = privateMemebrs;
return newState;
}



protected override void LoadControlState(object state)
{
if (state == null)
return;

Pair newState = (Pair)state;
base.LoadControlState(newState.First);
if (newState.Second != null)
{
Dictionary privateMemebrs = (Dictionary)newState.Second;
this.a = (List)privateMemebrs["a"];
this.b = (int)privateMemebrs["b"];
this.c = (string)privateMemebrs["c"];
}
}

Friday, March 2, 2007

New in .NET

I am new in .NET. I was a C++ programmer. And now I am developing a new Software in .NET. So, there is lot of things out there that does not match directly with my experience. But above all new technology is always interesting. Let see what happens next...

Technical Blogging

At last I have decided to blog my technical experience in this blog site. Hopefully this will be an interesting phenomena for me. Hope for the best