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 - 2007 db4objects Inc. http://www.db4o.com 02 03Namespace Db4objects.Db4odoc.Messaging 04 Class MyClientServerMessage 05 Private _info As String 06 07 Public Sub New(ByVal info As String) 08 Me._info = info 09 End Sub 10 11 Public Overrides Function ToString() As String 12 Return "MyClientServerMessage: " + _info 13 End Function 14 End Class 15End 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.Config; 04using Db4objects.Db4o.Messaging; 05 06namespace Db4objects.Db4odoc.Messaging 07{ 08 09 public class MessagingExample 10 { 11 private const string Db4oFileName = "reference.db4o"; 12 13 public static void Main(string[] Args) 14 { 15 ConfigureServer(); 16 } 17 //end Main 18 19 public static void ConfigureServer() 20 { 21 IConfiguration configuration = Db4oFactory.NewConfiguration(); 22 configuration.ClientServer().SetMessageRecipient(new SimpleMessageRecipient()); 23 IObjectServer objectServer = Db4oFactory.OpenServer(configuration, Db4oFileName, 0); 24 try 25 { 26 IConfiguration clientConfiguration = Db4oFactory.NewConfiguration(); 27 // Here is what we would do on the client to send the message 28 IMessageSender sender = clientConfiguration.ClientServer().GetMessageSender(); 29 IObjectContainer clientObjectContainer = objectServer.OpenClient(clientConfiguration); 30 31 sender.Send(new MyClientServerMessage("Hello from client.")); 32 clientObjectContainer.Close(); 33 } 34 finally 35 { 36 objectServer.Close(); 37 } 38 } 39 // end ConfigureServer 40 } 41 42 public class SimpleMessageRecipient: IMessageRecipient 43 { 44 public void ProcessMessage(IObjectContainer objectContainer,Object message) 45 { 46 // message objects will arrive in this code block 47 System.Console.WriteLine(message); 48 } 49 } 50}

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

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