Let's implement a simple standalone db4o server with a special client that can tell the server to shut itself down gracefully on demand.
First, both the client and the server need some shared configuration information. We will provide this using an interface:
01namespace Db4objects.Db4odoc.ClientServer 02
{ 03
/// <summary> 04
/// Configuration used for StartServer and StopServer. 05
/// </summary> 06
public class ServerConfiguration 07
{ 08
/// <summary> 09
/// the host to be used. 10
/// If you want to run the client server examples on two computers, 11
/// enter the computer name of the one that you want to use as server. 12
/// </summary> 13
public const string HOST = "localhost"; 14
15
/// <summary> 16
/// the database file to be used by the server. 17
/// </summary> 18
public const string FILE = "formula1.yap"; 19
20
/// <summary> 21
/// the port to be used by the server. 22
/// </summary> 23
public const int PORT = 0xdb40; 24
25
/// <summary> 26
/// the user name for access control. 27
/// </summary> 28
public const string USER = "db4o"; 29
30
/// <summary> 31
/// the pasword for access control. 32
/// </summary> 33
public const string PASS = "db4o"; 34
} 35
}
01Namespace Db4objects.Db4odoc.ClientServer 02
''' <summary> 03
''' Configuration used for StartServer and StopServer. 04
''' </summary> 05
Public Class ServerConfiguration 06
''' <summary> 07
''' the host to be used. 08
''' If you want to run the client server examples on two computers, 09
''' enter the computer name of the one that you want to use as server. 10
''' </summary> 11
Public Const HOST As String = "localhost" 12
13
''' <summary> 14
''' the database file to be used by the server. 15
''' </summary> 16
Public Const FILE As String = "formula1.yap" 17
18
''' <summary> 19
''' the port to be used by the server. 20
''' </summary> 21
Public Const PORT As Integer = &HDB40 22
23
''' <summary> 24
''' the user name for access control. 25
''' </summary> 26
Public Const USER As String = "db4o" 27
28
''' <summary> 29
''' the pasword for access control. 30
''' </summary> 31
Public Const PASS As String = "db4o" 32
33
End Class 34
End Namespace
Now we'll create the server:
01using System; 02
using System.Threading; 03
using Db4objects.Db4o; 04
using Db4objects.Db4o.Messaging; 05
06
namespace Db4objects.Db4odoc.ClientServer 07
{ 08
/// <summary> 09
/// starts a db4o server with the settings from ServerConfiguration. 10
/// This is a typical setup for a long running server. 11
/// The Server may be stopped from a remote location by running 12
/// StopServer. The StartServer instance is used as a MessageRecipient 13
/// and reacts to receiving an instance of a StopServer object. 14
/// Note that all user classes need to be present on the server side 15
/// and that all possible Db4oFactory.Configure() calls to alter the db4o 16
/// configuration need to be executed on the client and on the server. 17
/// </summary> 18
public class StartServer : ServerConfiguration, IMessageRecipient 19
{ 20
/// <summary> 21
/// setting the value to true denotes that the server should be closed 22
/// </summary> 23
private bool stop = false; 24
25
/// <summary> 26
/// starts a db4o server using the configuration from 27
/// ServerConfiguration. 28
/// </summary> 29
public static void Main(string[] arguments) 30
{ 31
new StartServer().RunServer(); 32
} 33
// end Main 34
35
/// <summary> 36
/// opens the IObjectServer, and waits forever until Close() is called 37
/// or a StopServer message is being received. 38
/// </summary> 39
public void RunServer() 40
{ 41
lock(this) 42
{ 43
IObjectServer db4oServer = Db4oFactory.OpenServer(FILE, PORT); 44
db4oServer.GrantAccess(USER, PASS); 45
46
// Using the messaging functionality to redirect all 47
// messages to this.processMessage 48
db4oServer.Ext().Configure().SetMessageRecipient(this); 49
try 50
{ 51
if (! stop) 52
{ 53
// wait forever until Close will change stop variable 54
Monitor.Wait(this); 55
} 56
} 57
catch (Exception e) 58
{ 59
Console.WriteLine(e.ToString()); 60
} 61
db4oServer.Close(); 62
} 63
} 64
// end RunServer 65
66
/// <summary> 67
/// messaging callback 68
/// see com.db4o.messaging.MessageRecipient#ProcessMessage() 69
/// </summary> 70
public void ProcessMessage(IObjectContainer con, object message) 71
{ 72
if (message is StopServer) 73
{ 74
Close(); 75
} 76
} 77
// end ProcessMessage 78
79
/// <summary> 80
/// closes this server. 81
/// </summary> 82
public void Close() 83
{ 84
lock(this) 85
{ 86
stop = true; 87
Monitor.PulseAll(this); 88
} 89
} 90
// end Close 91
} 92
}
01Imports System 02
Imports System.Threading 03
Imports Db4objects.Db4o 04
Imports Db4objects.Db4o.Messaging 05
06
Namespace Db4objects.Db4odoc.ClientServer 07
''' <summary> 08
''' starts a db4o server with the settings from ServerConfiguration. 09
''' This is a typical setup for a long running server. 10
''' The Server may be stopped from a remote location by running 11
''' StopServer. The StartServer instance is used as a MessageRecipient 12
''' and reacts to receiving an instance of a StopServer object. 13
''' Note that all user classes need to be present on the server side 14
''' and that all possible Db4oFactory.Configure() calls to alter the db4o 15
''' configuration need to be executed on the client and on the server. 16
''' </summary> 17
Public Class StartServer 18
Inherits ServerConfiguration 19
Implements IMessageRecipient 20
''' <summary> 21
''' setting the value to true denotes that the server should be closed 22
''' </summary> 23
Private [stop] As Boolean = False 24
25
''' <summary> 26
''' starts a db4o server using the configuration from 27
''' ServerConfiguration. 28
''' </summary> 29
Public Shared Sub Main(ByVal arguments As String()) 30
Dim server As New StartServer 31
server.RunServer() 32
End Sub 33
' end Main 34
35
''' <summary> 36
''' opens the IObjectServer, and waits forever until Close() is called 37
''' or a StopServer message is being received. 38
''' </summary> 39
Public Sub RunServer() 40
SyncLock Me 41
Dim db4oServer As IObjectServer = Db4oFactory.OpenServer(FILE, PORT) 42
db4oServer.GrantAccess(User, PASS) 43
' Using the messaging functionality to redirect all 44
' messages to this.processMessage 45
db4oServer.Ext().Configure().SetMessageRecipient(Me) 46
Try 47
If Not [stop] Then 48
' wait forever until Close will change stop variable 49
Monitor.Wait(Me) 50
End If 51
Catch e As Exception 52
Console.WriteLine(e.ToString()) 53
End Try 54
db4oServer.Close() 55
End SyncLock 56
End Sub 57
' end RunServer 58
59
''' <summary> 60
''' messaging callback 61
''' see com.db4o.messaging.MessageRecipient#ProcessMessage() 62
''' </summary> 63
Public Sub ProcessMessage(ByVal con As IObjectContainer, ByVal message As Object) Implements IMessageRecipient.ProcessMessage 64
If TypeOf message Is StopServer Then 65
Close() 66
End If 67
End Sub 68
' end ProcessMessage 69
70
''' <summary> 71
''' closes this server. 72
''' </summary> 73
Public Sub Close() 74
SyncLock Me 75
[stop] = True 76
Monitor.PulseAll(Me) 77
End SyncLock 78
End Sub 79
' end Close 80
End Class 81
End Namespace
And last but not least, the client that stops the server.
01using System; 02
using Db4objects.Db4o; 03
using Db4objects.Db4o.Messaging; 04
05
namespace Db4objects.Db4odoc.ClientServer 06
{ 07
/// <summary> 08
/// stops the db4o Server started with StartServer. 09
/// This is done by opening a client connection 10
/// to the server and by sending a StopServer object as 11
/// a message. StartServer will react in it's 12
/// processMessage method. 13
/// </summary> 14
public class StopServer : ServerConfiguration 15
{ 16
/// <summary> 17
/// stops a db4o Server started with StartServer. 18
/// </summary> 19
/// <exception cref="Exception" /> 20
public static void Main(string[] args) 21
{ 22
IObjectContainer objectContainer = null; 23
try 24
{ 25
// connect to the server 26
objectContainer = Db4oFactory.OpenClient(HOST, PORT, USER, PASS); 27
} 28
catch (Exception e) 29
{ 30
Console.WriteLine(e.ToString()); 31
} 32
33
if (objectContainer != null) 34
{ 35
// get the messageSender for the IObjectContainer 36
IMessageSender messageSender = objectContainer.Ext() 37
.Configure().GetMessageSender(); 38
39
// send an instance of a StopServer object 40
messageSender.Send(new StopServer()); 41
42
// close the IObjectContainer 43
objectContainer.Close(); 44
} 45
} 46
// end Main 47
} 48
}
01Imports System 02
Imports Db4objects.Db4o 03
Imports Db4objects.Db4o.Messaging 04
05
Namespace Db4objects.Db4odoc.ClientServer 06
''' <summary> 07
''' stops the db4o Server started with StartServer. 08
''' This is done by opening a client connection 09
''' to the server and by sending a StopServer object as 10
''' a message. StartServer will react in it's 11
''' processMessage method. 12
''' </summary> 13
Public Class StopServer 14
Inherits ServerConfiguration 15
''' <summary> 16
''' stops a db4o Server started with StartServer. 17
''' </summary> 18
''' <exception cref="Exception" /> 19
Public Shared Sub Main(ByVal args As String()) 20
Dim objectContainer As IObjectContainer = Nothing 21
Try 22
' connect to the server 23
objectContainer = Db4oFactory.OpenClient(HOST, PORT, User, PASS) 24
Catch e As Exception 25
Console.WriteLine(e.ToString()) 26
End Try 27
If Not objectContainer Is Nothing Then 28
' get the messageSender for the IObjectContainer 29
Dim messageSender As IMessageSender = objectContainer.Ext().Configure().GetMessageSender() 30
' send an instance of a StopServer object 31
messageSender.Send(New StopServer()) 32
' close the IObjectContainer 33
objectContainer.Close() 34
End If 35
End Sub 36
'end Main 37
End Class 38
End Namespace