C#

[back]

The appearance of mobile devices such as PDAs and Smart Phones made it very attractive to inculde these devices and create mobile scenarios using SQLSpaces. In the beginning of SQLSpaces the leading OS on mobiles was Microsoft's Windows Mobile (5 and later 6) with support for the .net-Framework. Comming from the Java world it was very convenient for us to implement a client in C#.

This client is a native client, i.e., it communicates to the server using the XML protocol over a socket. It has dependencies to System.XML for processing XML.

You can download it here: SQLSpaces.dll. The current version is 3.6, thus not all features of the 3.7 server release are supported.

The API of the C# client and the feature set is very similar to the Java client, however, not all features are available:

  • Semi-formal fields
  • Inverse fields
  • Callback notifications for ALL events (combined WRITE, DELETE and UPDATE)

Code Examples

As already mentioned, the API of the SQLSpaces C# client is very similar to the Java client. However, here you will find some code examples.

  • In order to use the library you need to inculde the SQLSpaces.dll and add the using statement:
    using Collide.SQLSpaces;
    
  • Connecting to a TupleSpace on localhost, port 2525, with the User called "CSharp" and no password, to the space called "cmdSpace"
    TupleSpace ts = new TupleSpace("localhost", 2525, "CSharp", "", new String[] {"cmdSpace"});
    
  • Writing a tuple with one field containing the String "test"
    Field f = new Field("test");
    Tuple tup = new Tuple(new Field[] {f});
    TupleID id = ts.write(tup);
    
  • Creating a template tuple to read or take the previously created tuple (note that informal fields are created using typeof(string))
    Tuple template = new Tuple(new Field[] {new Field(typeof(string))});
    Tuple tup2 = ts.read(template);
    string value = t.GetField(0).Value.ToString();
    System.Console.WriteLine(value);
    
  • Defining and registering a callback: The first thing you need to write is a class that implements the Callback interface and the Call method. The MessageCallback class is a custom class to be implemented by yourself.
    class MessageCallback : Callback
    {
        public override bool Call(Callback.Command cmd, int seqnum, Tuple afterCmd, Tuple beforeCmd, bool isException)
        {
            String s = afterCmd.GetField(0).Value.ToString();
            System.Console.WriteLine(s);
                    return false;
        }
    }
    

    Inside the code you have to register this MessageCallback class to the TupleSpace with the type of operation (here: write), the defined template tuple and an boolean flag, if the notification should take place in a new thread or in the notification thread. If you don't understand this, just pass always true.
    Tuple template = new Tuple(new Field[] {new Field(typeof(string))});
    ts.EventRegister(Callback.Command.write, template, new MessageCallback(), true);
    

    In that example, if a tuple machting the defined template (only one string field) is written into the space cmdSpace, the Call method is called and the client is notified.