Saturday, December 26, 2009

Workflow Without Windows Workflow

Hello, it has been a while since I wrote my last blog. Anyway, I am back again. I have a nice blog today, I have been working for a while with a team which his main target is to use everything Microsoft has released. It is a good thing sometimes and a very bad thing the other times. They read a "workflow" word in the requirements document, the first came to their mind was WF (Windows Workflow Foundation). The problem was so simple using WF would be over engineering. So, I decided to think of a more simple solution. As I am a Spring.NET Freak and I think we can't build an application without using it, I thought of using it :).

First, Let us create a State class first:

    public class State
    {
        private string _id;
        private string _name;
        private List<link> _links;

        public List NextStates
        {
            get
            {
                return Links.Select(l => l.State).ToList();
            }
        }

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public List<link> Links
        {
            get { return _links; }
            set { _links = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public bool IsEndState
        {
            get
            {
                return Links.Count == 0;
            }
        }
    }

And a Link class:

    public class Link
    {
        //this condition will written used SPEL (Spring Expression Langauge)
        public string Condition { get; internal set; }
        public State State { get; internal set; }
    }

Then create the interface for the state machine:

    public interface IStateMachine
    {
        State InitialState { get; }
        State CurrentState { get; }
        void SetCurrentState(string id);
        bool Advance();
        List NextStates { get; }
    }
Then comes spring to the rescue, Spring here will used for 2 main tasks:
  1. Configuration File for our Workflow (State Machine): Keeps the state information and the transitions between states.
  2. Spring Expression will be used to define the transition rules between different states.
I decided to implement a simple interview process as an illustration for my small framework:

The spring configuration file for the mentioned process will be as follows:

<xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
 
  <object id="ExamToFirstInterview" type="SimpleStateMachine.Link, SimpleStateMachine">
    <property name="Condition" value="ExamScore >= 25"/>
    <property name="State" ref="FirstInterview" />
  <object>

  <object id="ExamToRejected" type="SimpleStateMachine.Link, SimpleStateMachine">
    <property name="Condition" value="ExamScore < 25"/>
    <property name="State" ref="Rejected" />
  <object>

  <object id="FirstInterviewToSecondInterview" type="SimpleStateMachine.Link, SimpleStateMachine">
    <property name="Condition" value="FirstInterviewAccepted"/>
    <property name="State" ref="SecondInterview" />
  <object>

  <object id="FirstInterviewToRejected" type="SimpleStateMachine.Link, SimpleStateMachine">
    <property name="Condition" value="!FirstInterviewAccepted"/>
    <property name="State" ref="Rejected" />
  <object>

  <object id="SecondInterviewToAccepted" type="SimpleStateMachine.Link, SimpleStateMachine">
    <property name="Condition" value="SecondInterviewAccepted"/>
    <property name="State" ref="Accepted" />
  <object>

  <object id="SecondInterviewToRejected" type="SimpleStateMachine.Link, SimpleStateMachine">
    <property name="Condition" value="!SecondInterviewAccepted"/>
    <property name="State" ref="Rejected" />
  <object>

  <object id="Exam" type="SimpleStateMachine.State, SimpleStateMachine">
    <property name="Id" value="exam"/>
    <property name="Name" value="Exam"/>
    <property name="Links">
      <list element-type="SimpleStateMachine.Link, SimpleStateMachine">
        <ref object="ExamToFirstInterview"/>
        <ref object="ExamToRejected"/>
      <list>
    <property>
  <object>

  <object id="FirstInterview" type="SimpleStateMachine.State, SimpleStateMachine">
    <property name="Id" value="first_interview"/>
    <property name="Name" value="First Interview"/>
    <property name="Links">
      <list element-type="SimpleStateMachine.Link, SimpleStateMachine">
        <ref object="FirstInterviewToSecondInterview"/>
        <ref object="FirstInterviewToRejected"/>
      <list>
    <property>
  <object>

  <object id="SecondInterview" type="SimpleStateMachine.State, SimpleStateMachine">
    <property name="Id" value="second_interview"/>
    <property name="Name" value="Second Interview"/>
    <property name="Links">
      <list element-type="SimpleStateMachine.Link, SimpleStateMachine">
        <ref object="SecondInterviewToAccepted"/>
        <ref object="SecondInterviewToRejected"/>
      <list>
    <property>
  <object>

  <object id="Accepted" type="SimpleStateMachine.State, SimpleStateMachine">
    <property name="Id" value="accepted"/>
    <property name="Name" value="Accepted"/>
    <property name="Links">
      <list element-type="SimpleStateMachine.Link, SimpleStateMachine">
      <list>
    <property>
  <object>

  <object id="Rejected" type="SimpleStateMachine.State, SimpleStateMachine">
    <property name="Id" value="rejected"/>
    <property name="Name" value="Rejected"/>
    <property name="Links">
      <list element-type="SimpleStateMachine.Link, SimpleStateMachine">
      <list>
    <property>
  <object>


  <object id="InterviewProcess.InterviewStateMachine" type="InterviewProcess.InterviewStateMachine, InterviewProcess">
    <property name="AllStates">
      <list element-type="SimpleStateMachine.State, SimpleStateMachine">
        <ref object="Exam"/>
        <ref object="FirstInterview"/>
        <ref object="SecondInterview"/>
        <ref object="Accepted"/>
        <ref object="Rejected"/>
      <list>
    <property>
    <property name="InitialState" ref="Exam"/>
  <object>

  <object id="SimpleStateMachine.IStateMachineManager" type="SimpleStateMachine.StateMachineManager, SimpleStateMachine">
  <object>

<objects>


Quite Simple? I think so :) Let us try it.



At the end I am not saying that this a replacement for windows workflow foundation is a very powerful framework but I am only pushing people to think outside of the box.

You can refer to the full source code here and don't forgot that I used Visual Studio 2010 Beta 2. Good Luck!

The initials of my blog are WWWW, isn't this cool? :D ... maybe this will be the next WWW ;).

Friday, May 8, 2009

Using WCF and Spring.NET to Scale Your Architecture

Windows Communication Foundation (WCF) is more than just web services. Most of the .NET developers think of it as a replacement for old ASMX web services. Today I am going to show you how I am going use it in a different way. I am going to use it to distribute different components of a system to be run separately on different machines without re-compilation of your code. You can do this based on your needs and the hardware available.

First, Let us define the problem we are trying to solve. Assume that you are working on an application that part of its logic is to compute a complex operation. This operation can not be simplified and it consumes most of the application time. Your default deployment will be as following:


This deployment model will introduce a bottleneck to your application. What I have in mind is to deploy the application on a server and the complex component on another one. This way you can distribute the processing across your servers. Then the deployment will be as following:


Let us see a simple example for the implementation of the following components. This our service contract:

[ServiceContract]

public interface ICalcEngine

{

        [OperationContract]


        Operation CalculateOperation(Operation operation);

}

We will use spring to load your component hosted in memory as mentioned in the first deployment model.

      <object name="CalcEngineWCF.ICalcEngine"

              type="CalcEngineWCF.CalcEngine, CalcEngineWCF"/>

My proposed solution will allow you change the deployment model without changing a line of code in your application as mentioned in the second deployment model. You will deploy your complex component as a service on a remote host. In this case spring configuration in the client side (the main application server) will be as following:

      <object name="CalcEngineWCF.CalcEngineFactory"

              type="CalcEngineWCF.CalcEngineFactory, CalcEngineWCF">

        <property name="Address" value="http://remote-server:8000/CalcEngine/Service"/>

      </object>


      <object name="CalcEngineWCF.ICalcEngine"

                        type="CalcEngineWCF.CalcEngine, CalcEngineWCF"

                        factory-method="CreateWCFInstance"

                        factory-object="CalcEngineWCF.CalcEngineFactory"/>

I hope this will be clear enough for you to be able to apply this model in your applications.

You can also refer to a working example here. The mentioned solution contains 3 projects:

  1. CalcEngineWCF: Our complex component that needs to be distributed.
  2. WCFHost: Remote application that will host CalcEngineWCF.
  3. WCFClient: The main application server. Its name might be confusing because it acts as a client to the remote host.

Friday, April 4, 2008

Steganography in C#

It has been a while since I wrote my last blog, but anyway I am back again. I am going to talk today about Steganography using C#.

Well let us define Steganography first. According to Wikipedia:
Steganography is the art and science of writing hidden messages in such a way that no one apart from the sender and intended recipient even realizes there is a hidden message. By contrast, cryptography obscures the meaning of a message, but it does not conceal the fact that there is a message.”

So it is all about information hiding, today I will not get into the different techniques of steganography . I will just talk about one of my project that I used steganography in it.
It was required to implement an application that will be able to sign files digital using the RSA Algorithm.


The application provides some features and they are listed as follows:

1. Signing Files: Files can be signed and store the signature inside it or in another separate file
2. Signing Wave Files: The application signs wave files by signing the header of the file and then store and hide this information inside the wave file

The operation of the file signing is divided into three states:
1. Hashing:
In this operation we read the data that is going to be hashed. Depending on the hash function provided by the framework, you can hash the data in order to be ready for the next step.

2. Signing:
In this operation we are going to sign the data received from the previous step. All we have to do is to encrypt the provided data using the private key of the user.

Here is the method I used to sign the data:

public byte[] SignData(string data, HashType hashType)
{
HashWrapper hasher = new HashWrapper(hashType);
byte[] bytesArraySignedValue = rsa.SignData(HelperFunctions.ConvertStringToByteArray(data), hasher.GetHasher());

SignedValue = HelperFunctions.ConvertByteArrayToHexString(bytesArraySignedValue);

return bytesArraySignedValue;
}

3. Verification:
In this step we are going to verify the signature. In order to do that we have to hash data and keep the digest, then decrypt the signature using the public key. Both should match in order to be a valid signature.

Here is the method I used to verify the signed data:

public bool VerifySignedData(string data, HashType hashType, byte[] signature)
{
HashWrapper hasher = new HashWrapper(hashType);
return rsa.VerifyData(HelperFunctions.ConvertStringToByteArray(data), hasher.GetHasher(), signature);
}

It seems that Microsoft has done all the work for you ;)

I will give you a breif about reading wave files in C#, For more information regarding reading wave files format please refer to the following link. The WAVE file format is a subset of Microsoft's RIFF specification for the storage of multimedia files. A RIFF file starts out with a file header followed by a sequence of data chunks. A WAVE file is often just a RIFF file with a single "WAVE" chunk which consists of two sub-chunks -- a "fmt " chunk specifying the data format and a "data" chunk containing the actual sample data.
Signing Wave Files:
I used a new idea by signing wave files. Signing wav files operations is similar to signing a normal file. First, we have to calculate the signature of the input file but instead of calculating the digest for the whole file. We will calculate the digest for the header only. Then encrypt this hash using the private key. The only difference is the storing operation. We will store the signature bits, by changing the least significant bit in all the wave samples according to the signature bits. This bit won’t make a difference in the sound of the file and we will have the digitally signed.

Application Logical Design:



*Logging Module: This module is responsible for the logging functionality across all the application
*Helper Classes: This module includes some common functionalities like handling the bit manipulation using the c# language
*Wave File Module: This module is responsible for handling the wave operations: Reading wave binary data, writing a new wave file and signing the wave files
*RSA Wrapper: This module wraps the functionality of the RSA algorithm. It facilitates the using of encryption depending on the application logic
*Hash Wrapper: This module is responsible for wrapping the hash algorithm. It uses the hash service providers available through the dot net framework
*Presentation Layer: This module is responsible for handling the UI functionalities across all the application


I used Visual C# 2005 to implement this application. I am sharing the source code, I hope it could be helpful. You can find it here. Pleas let me know your feedback.



Download: DigitalSigner.zip

Sunday, October 7, 2007

Microformats: One step towards semantic web (Part 2)

Microformats isn’t a new technology at all. It has been around for a while now. It is being adopted by many organizations. The future versions of the Firefox browser version 3 and version 8 of Internet Explorer are expected to include native support for microformats.

Why are microformats a good idea to represents semantics in web page?
  • It is a standard method to markup website content.
  • It is not a new language. It is just a combination of XHTML and CSS.
  • It is embedded in the web page. i.e. hidden from humans, readable by machines.
  • It facilitate the interoperability between different applications like integrating the Outlook calendar with the events of a certain website.
It is designed on the 3R principle (reduce, recycle and reuse). First, Reduce: It uses the KISS principle (Keep it simple and straight forward). Second, Recycle: It encourages the modularity. Third, Reuse: It favors best practices.

How can I create a microformats?
The Microformats community has developed a number of creators for hCard, hCalendar, and hReview. It helps people to create their first microformats. You enter the required data and it creates the equivalent XHTML code. These provide a simple way to start publishing microformatted content. Here is a list of some microformats creator:
People will think that it will take some time to be widely used on the web. Here is a tip for you the page you are reading right now uses microformats to markup contacts. Let us see some microformats in action, now you are asking me isn’t the semantics embedded in a webpage for machines to understand. So I think we need a special tools to extract this information. I will review 2 tools you can use to discover microformats in embedded web pages.

Operator
Operator lets you combine pieces of information on Web sites with applications in ways that are useful. For instance, Flickr + Google Maps, Upcoming + Google Calendar, Yahoo! Local + your address book, and many more possibilities and permutations. All of these scenarios are possible due to Microformats, an emerging standard for injecting semantics into HTML. The following screen shot shows Operator in action while opening http://11870.com/ which is a social network which implements hReview for user's reviews about places, services and business:
Tails Export
An extension for Showing and Exporting Microformats. Currently it supports the following formats ( hCard, hCalendar, hReview, xFolk and Rel-license). The following screen shot shows Tails export in action while opening http://11870.com/ :


Finally, Microformats is a good step towards semantic web. Now, your browser can understand some of the semantics in a web page like the contacts, addresses, reviews ... etc. For example, Microformats can help you integrate addresses with google maps and so on...
It is now clear that the dreams of Tim Burners Lee are going to be true "a single web of meaning about everything and for everyone".

Saturday, September 29, 2007

Microformats: One Step towards Semantic Web (Part 1)

So what is really Semantic Web? Well, Semantic Web is an extension of the World Wide Web in which content can be expressed in a format that can be read and used by software not only by humans, to facilitate the searching, sharing and integration of information. In simple words it is a way to standardize the data and information representation over the world wide web, so that software would be able to understand its semantics without the need of complicated intelligent software. There have been several technologies that drive the road to semantic web like Resource Description Framework (RDF) and Web Ontology Language (OWL).

Semantic Web is one of the theories that Tim Burners Lee the father of the World Wide Web has been dreaming about since he launched the first website in the history in 1991. He had a vision towards this giant network that it shouldn't be a human-human communication only, machines should have a role too. His exact words were "a single Web of meaning, about everything and for everyone.". He made his idea available freely with no patent, so they can be easily adopted by anyone.

During the keynote at Microsoft MIX06, Bill Gates said, “We need microformats and to get people to agree on them. It is going to bootstrap exchanging data on the Web . . . we need them for things like contact cards, events, directions.”



What are Microformats?

Microformats are, according to the definition of microformats.org,

“Designed for humans first and machines second, microformats are a set of simple, open data formats built upon existing and widely adopted standards. Instead of throwing away what works today, microformats intend to solve simpler problems first by adapting to current behaviors and usage patterns (e.g., XHTML, blogging).”

In another words, it is a web-based data formatting methodology that re-use existing content as metadata, using only XHTML and HTML classes and attributes. So that human still be able to understand it as they used to and software agents can process it easily to extract the information they want.

Microformats might catch up with the web development community faster than the RDF or OWL. As it is build on the existing skills of web developers. It doesn’t require developers to learn whole new technologies, throw away their existing code bases, or wait years for browser developers to catch up and actually implement support for the technology. Microformats simply use features of HTML that have been around for years and are familiar to most web developers,

Current microformats allow the encoding and extraction of events, contact information, social relationships, and so on. More are being developed.

Microformats uses XHTML and HTML standards allow for semantics to be embedded within the attributes of markup tags. Microformats indicates the presence of metadata using the following attributes class, rel and rev.

Let us see an example of microformats to understand how it works, the following example is how to use microformats to describe contact information:

<div class="vcard">
<div class="fn">Fady Younan</div>
<div class="org">ITWorx</div>
<div class="tel">000-555-1234</div>
<a class="url" href="http://www.fady-younan.com">http://www.fady-younan.com</a>

</div>


Another example of how describe geographic information:

<span class="geo">
<span class="latitude">28.47</span>,
<span class="longitude">19.89</span>

</span>


If you haven't notice it uses the same XHTML and CSS to embed semantics in the web page, it uses the class "geo" to describe that is a geographic information, latitude and longitude to describe the co-ordination and so on ...

Here is list of some of the available microformats and there are others under development:
  • hCard: describes the contact information for people or organizations
  • hCalendar: describes information about events such as conferences,meetings and parties
  • XFN: describes the relationships between people.
  • hReview: describes the reviews for movies, books .. etc.
To be continued ...

Saturday, September 1, 2007

Erlang: Web Development (Part 4)

Erlang was mainly developed targeting the telecom application. It had to satisfy some of requirements of the telecom applications which are being distributed, massively concurrent, real-time and high availability. These requirements are similar to those put upon Internet based applications, making Erlang a good candidate for development of web based services. So is Erlang ready to be an enterprise web development framework?

The first tries of Erlang to conquer the web development community was back at 1997, when Ericsson developed its first web server. This server was called INETS. It is still a part of the Open Telecom Platform (OTP). Back there it was written with 10,000 lines of code (Don’t forget it is a functional language) and achieved 80% of the Apache server functionality which at that time consisted of about 100,000 lines of code.


Yaws (Yet Another Web Server) is another HTTP high performance web server particularly well suited for dynamic-content web applications. It is entirely written in Erlang. It was released as an open source in 2002. It is a multithreaded web server where a light weight process is used to handle each client request. The performance of Yaws comes from the underlying Erlang system and its ability to handle concurrent processes in an efficient way. It was a revolutionary web server considering the performance.

Let us not forget the famous comparison between Yaws and Apache. In a denial of service attack, the number of parallel connections needed to crash the Erlang web server was about 20 times as many as an Apache web server running on the same hardware. Apache (blue and green) dies when subject to a load of concurrent 4,000 parallel requests. Yaws (red) works till 80,000 concurrent requests.


I will try to give you a simple example about how to create a simple dynamic web page with Yaws.

This a simple Hello world to Yaws.

<html>
<h1> Title</h1>
<erl>
out(Arg) -> {html, “Hello World"}.
</erl>
<h1> Something</h1>
</html>

Thanks to Yariv Sadan’s effort to involve Erlang in the web development community using ErlyWeb which is a web framework that helps you easily build database-driven applications using the MVC architecture. It's similar to Ruby on Rails, except that it's written in Erlang. There is also ErlyDB a database abstraction layer generator. ErlyDB taps into Erlang’s runtime metaprogramming powers to generate an abstraction layer for your database on the fly.


I am going to propose an architecture for web development using Erlang. It will consists of five layers:

  • Hardware/ Operating System: This layer represents the physical layer of the system. It can be a Linux system running on a network file system(NFS) to make use of the concurrency and distribution features in Erlang.
  • DBMS: This layer represents the persistence layer of the system. It can be either Mnesia or MySQL.
  • Data Base Abstraction Layer.
  • Erlang/Open Telecom Platform: It is the standard libraries of the Erlang language.
  • Presentation Layer: This layer consists of two components. Yaws as a web server and ErlyWeb as rapid development framework.

Now, I can see that Erlang is starting to involve itself in the web development community. The problem is that the Erlang community isn't big enough to support this involvement. Is this going to change?



Erlang: Open Telecom Platform (Part 3)

Is Erlang really a new language? Do we have to write our old libraries and applications again from scratch? Actually Erlang is an old language may be older than Java and dot NET. It was developed in the late 80's. I will talk today about one the strengths of Erlang which is the Open Telecom Platform (OTP). The OTP is a development platform for building telecommunications applications based on Erlang. It was released as open source in 1998.

The OTP is a development system platform for building, and a control system platform for running, telecommunications applications. It is not a monolithic platform, but is made up of sets of tools and building blocks. Most of it is written in Erlang, there are some components that are written in C++. The OTP architecture consists of three layers as shown in the figure.


The Bottom layer: The system hardware in this layer. This is merely an architectural view; in real systems, the bottom layer contains many computers which may be of different types.

The Middle layer: Support for telecommunications requirements is provided by a robust real-time components. Its main modules are:
  • Erlang run-time system: The basic system that supports the execution of Erlang programs.
  • Web server: A Web server that serves HTTP requests via executing server side applications written in Erlang.
  • Mnesia: A real-time fault-tolerant distributed DBMS that supports fast transactions for the telecommunications application, and a query language, called Mnemosyne, for handling complex queries.
  • SASL: The systems architecture support libraries (SASL) contain basic software that supports system start/restart, live system software updates, and process management.
  • SNMP support: SNMP provides run-time support through an extensible agent.
The Top layer: All applications have access to Mnesia and SASL. The SNMP agent and the Web server may also invoke functions that are provided by the applications in this layer.

Using this platform, libraries and utilities you will be able to write robust, real-time systems. You won't be able to realize how powerful is Erlang. I will talk next time about existing systems that uses Erlang.

To be continued ...