Jul 5, 2008


eXtensible Messaging and Presence Protocol (XMPP) is a protocol to stream XML elements for messaging, presence and request-response services. It is mainly used in building IM and presence based applications.
The protocol is not coupled to any network architecture, but it’s generally used in client-server architecture. An XMPP server can communicate with its clients (recommended port: 5222) or with other XMPP servers (recommended port: 5269) over a TCP connection.

An example network architecture could be like

The gateway is necessary to translate the messages between a non-XMPP server and an XMPP server.

All XMPP messaging happens in the form of Streams and Stanzas. Stream is the container for all messages sent from one entity to the other. A stream is initiated with a <stream> tag and is destroyed with a </stream> tag. Multiple XML elements can be sent over the connection before the stream is closed.
These streams are unidirectional i.e. if a client initiates a stream with the server to send a request; the server initiates a new response stream on the same TCP connection. Unlike the client-server communication, the server-server communication requires two separate TCP connections for exchange of XML elements over streams.

Stanza is one atomic unit of information that can be sent from one entity to another over a stream. It is a structured XML element. Three types of stanzas are defined in the XMPP standards:

  message: used for instant messaging
  presence: used for presence related messaging
  iq: used in request-response services for querying information from entities.

The client-server (or server-server) communication can be thought of as two open-ended XML documents. The stream just wraps all the stanzas sent in a session from one entity to the other.
A stream with different stanzas:

Watch out for more on XMPP..

Jul 3, 2008

LINQ - Introduction

LINQ (Language Integrated Query) introduces a generic and standard pattern for querying and updating data store of any kind. Basically a query is an expression that retrieves data from a data source. Queries are usually expressed in a specialized query language. I assume LINQ is intoduced to decouple the query language from the datasource. This avoids a lot of "unnecessary" learnings, because literally there is query language for each type of data source be it SQL databases, ADO.NET datasets, Collections types and XML documents.
A LINQ query operations has there distinct phases:

  Recognizing the Data Source
With LINQ, there is another concept that has been introduced called Queryable, implementing interface IQueryable. Any Enumerable type is a queryable type and it requires no modification as a LINQ data source. If the source data is not already in memory as a queryable type, the LINQ provider must represent it as such. For example, LINQ to XML loads an XML document into a queryable XElement type.Types such as ArrayList that support the non-generic IEnumerable interface can also be used as a LINQ data source.
Here we will take an example of array int.

int[] array = new int[] {0,1,2,3,4};

  Defining the Query
A query specifies what data to be retrieved from a data source and how that information should be sorted and grouped. A query is stored in a query variable and initialized with a query expression. To make it easier to write queries, C# has introduced new query syntax.

// myQuery is an IEnumerable of type int
var myQuery =
from n in array
where (n % 2) == 0
select n;

Here the query expression contains three clauses: from, where and select just like any SQL query. The "from" clause specifies the data source, the "where" clause applies the filter, and the "select" clause specifies the type of the returned elements.

The important point is that in LINQ, the query variable itself takes no action and returns no data. It just stores the information that is required to produce the results when the query is executed at some later point.

Queries can also be expressed by using method syntax.( Something I am not aware right now )

  Query Execution
The query variable itself only stores the query commands. The actual execution of the query is deferred until we iterate over the query variable in a foreach statement. This concept is regarded as "deferred execution" and is demonstrated in the following example:

foreach (int num in myQuery)
  Console.WriteLine( "{0}", num );

Because the query variable itself never holds the query results, we can execute it as often as we like. For example, we may have a database that is being updated continually by a separate application, we could create one query that retrieves the latest data, and we could execute it repeatedly at some interval to retrieve different results every time.
Whenever the query variable is accessed or any of it's method is called, the query gets executed and the result sets are cached in the respective objects. This term is regarded as "Forced execution".
You can refer msdn for further details.

Jul 1, 2008

Marshalling Array of Structures

It is fairly easy to Marshal simple structures in .NET but when it comes to sending a whole bunch of structures as parameter it’s little head scratching task if you have not done it already. The worst part is you won’t find any articles on how one should go about it. When I had to marshal an array of structures, every time I ran into same state of confusion. I decided to blog it this time (for future reference of course :) )
So let’s assume there is a structure called StructType and structArray be an array of StructType.

//Lets not bother about how the array was populated
StructType[] structArray = new GetStructArray();
// Get the size of each element
int structSize = Marshal.SizeOf(structArray [0]);

Here int can be replaced with long (depends on 32 bit or 64 bit addressing)

// Total size of the memory block
IntPtr ptr = Marshal.AllocHGlobal(structSize * structArray.Length);
int addr = (int)ptr;
for (int index = 0; index < structArray.Length; index++)
   Marshal.StructureToPtr(structArray [index],
         (IntPtr)(addr + structSize * index),false);

The IntPtr type ptr will hold the address of the memory block containing StructType array.

Top Ad 728x90