Messaging

In client/server mode the TCP connection between the client and the server can also be used to send messages from the client to the server.

Possible usecases could be:

Here is some example code how this can be done.

First we need to decide on a class that we want to use as the message. Any object that is storable in db4o can be used as a message, but strings and other simple types will not be carried (as they are not storable in db4o on their own). Let's create a dedicated class:

MyClientServerMessage.cs
01using System; 02 03namespace Db4objects.Db4odoc.Messaging 04{ 05 class MyClientServerMessage 06 { 07 private string _info; 08 09 public MyClientServerMessage(String info) 10 { 11 this._info = info; 12 } 13 14 public override String ToString() 15 { 16 return "MyClientServerMessage: " + _info; 17 } 18 } 19}

MyClientServerMessage.vb
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02Imports System 03 04Namespace Db4objects.Db4odoc.Messaging 05 Class MyClientServerMessage 06 Private _info As String 07 08 Public Sub New(ByVal info As String) 09 Me._info = info 10 End Sub 11 12 Public Overrides Function ToString() As String 13 Return "MyClientServerMessage: " + _info 14 End Function 15 End Class 16End Namespace

Now we have to add some code to the server to react to arriving messages. This can be done by configuring a MessageRecipient object on the server. Let's simply print out all objects that arrive as messages. For the following we assume that we already have an ObjectServer object, opened with Db4o.openServer().

MessagingExample.cs
01using System; 02using Db4objects.Db4o; 03using Db4objects.Db4o.Messaging; 04 05namespace Db4objects.Db4odoc.Messaging 06{ 07 08 public class MessagingExample 09 { 10 public readonly static string YapFileName = "formula1.yap"; 11 12 public static void ConfigureServer() 13 { 14 IObjectServer objectServer = Db4oFactory.OpenServer(YapFileName, 0); 15 objectServer.Ext().Configure().SetMessageRecipient(new SimpleMessageRecipient()); 16 try 17 { 18 IObjectContainer clientObjectContainer = objectServer.OpenClient(); 19 // Here is what we would do on the client to send the message 20 IMessageSender sender = clientObjectContainer.Ext().Configure() 21 .GetMessageSender(); 22 23 sender.Send(new MyClientServerMessage("Hello from client.")); 24 clientObjectContainer.Close(); 25 } 26 finally 27 { 28 objectServer.Close(); 29 } 30 } 31 // end ConfigureServer 32 } 33 34 public class SimpleMessageRecipient: IMessageRecipient 35 { 36 public void ProcessMessage(IObjectContainer objectContainer,Object message) 37 { 38 // message objects will arrive in this code block 39 System.Console.WriteLine(message); 40 } 41 } 42}

MessagingExample.vb
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02Imports System 03Imports Db4objects.Db4o 04Imports Db4objects.Db4o.Messaging 05 06 07Namespace Db4objects.Db4odoc.Messaging 08 09 Public Class MessagingExample 10 Public Shared ReadOnly YapFileName As String = "formula1.yap" 11 12 Public Shared Sub ConfigureServer() 13 Dim objectServer As IObjectServer = Db4oFactory.OpenServer(YapFileName, 0) 14 objectServer.Ext().Configure().SetMessageRecipient(New SimpleMessageRecipient()) 15 Try 16 Dim clientObjectContainer As IObjectContainer = objectServer.OpenClient() 17 ' Here is what we would do on the client to send the message 18 Dim sender As IMessageSender = clientObjectContainer.Ext().Configure() 19 sender.Send(New MyClientServerMessage("Hello from client.")) 20 clientObjectContainer.Close() 21 Finally 22 objectServer.Close() 23 End Try 24 End Sub 25 ' end ConfigureServer 26 End Class 27 28 Public Class SimpleMessageRecipient 29 Implements IMessageRecipient 30 Public Sub ProcessMessage(ByVal objectContainer As IObjectContainer, ByVal message As Object) Implements IMessageRecipient.ProcessMessage 31 ' message objects will arrive in this code block 32 System.Console.WriteLine(message) 33 End Sub 34 End Class 35 36End Namespace

The MessageSender object on the client can be reused to send multiple messages.