Out-of-band Signalling

Sometimes a client needs to send a special message to a server in order to tell the server to do something. The server may need to be signalled to perform a defragment or it may need to be signalled to shut itself down gracefully.

This is configured by calling setMessageRecipient(), passing the object that will process client-initiated messages.

StartServer.cs: RunServer
01/// <summary> 02 /// opens the IObjectServer, and waits forever until Close() is called 03 /// or a StopServer message is being received. 04 /// </summary> 05 public void RunServer() 06 { 07 lock(this) 08 { 09 // Using the messaging functionality to redirect all 10 // messages to this.processMessage 11 IConfiguration configuration = Db4oFactory.NewConfiguration(); 12 configuration.ClientServer().SetMessageRecipient(this); 13 14 IObjectServer db4oServer = Db4oFactory.OpenServer(configuration, FileName, Port); 15 db4oServer.GrantAccess(User, Password); 16 17 try 18 { 19 if (! stop) 20 { 21 // wait forever until Close will change stop variable 22 Monitor.Wait(this); 23 } 24 } 25 catch (Exception e) 26 { 27 Console.WriteLine(e.ToString()); 28 } 29 db4oServer.Close(); 30 } 31 }
StartServer.vb: RunServer
01''' <summary> 02 ''' opens the IObjectServer, and waits forever until Close() is called 03 ''' or a StopServer message is being received. 04 ''' </summary> 05 Public Sub RunServer() 06 ' Using the messaging functionality to redirect all 07 ' messages to this.processMessage 08 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 09 configuration.ClientServer().SetMessageRecipient(Me) 10 SyncLock Me 11 Dim db4oServer As IObjectServer = Db4oFactory.OpenServer(configuration, File, Port) 12 db4oServer.GrantAccess(User, Password) 13 Try 14 If Not [stop] Then 15 ' wait forever until Close will change stop variable 16 Monitor.Wait(Me) 17 End If 18 Catch e As Exception 19 Console.WriteLine(e.ToString()) 20 End Try 21 db4oServer.Close() 22 End SyncLock 23 End Sub

[/filter]

The message is received and processed by a processMessage() method:

Db4o allows a client to send an arbitrary signal or message to a server by sending a plain user object to the server. The server will receive a callback message, including the object that came from the client. The server can interpret this message however it wants.