Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 609

db4o-doc-8.0-1.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Client-Server" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Messaging	</title>
        <script type="text/javascript">/* <![CDATA[ */
window.onload = function(){
	var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf';
	ZeroClipboard.setMoviePath(pathToFlash);
			
	function bindToClipBord(element,content){
		var clip = new ZeroClipboard.Client();
		clip.setText(content);
		clip.glue(element);
	};
		
	if(location.protocol==='file:'){
		$('.copylink-marker').remove();
	} else{
			$('.copylink-marker').each(function(){
				var text = $(this).parent().parent().children('.prettyprint').html();
				$(this).hover(function(){
					bindToClipBord(this,text);
				},
				function(){});
			});	
	}		
	prettyPrint();	
};
                /* ]]> */</script>
        <link href="../SkinSupport/MadCap.css" rel="stylesheet" />
        <link href="../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" />
        <script src="../Resources/Code/prettify.js">
        </script>
        <script src="../Resources/Code/lang-vb.js">
        </script>
        <script src="../Resources/Code/jquery.min.js">
        </script>
        <script src="../Resources/Code/ZeroClipboard.js">
        </script>
        <script src="../SkinSupport/MadCapAll.js" type="text/javascript">
        </script>
    </head>
    <body>
        <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../index_CSH.html#client-server/messaging.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../client-server.htm">Client-Server</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Messaging</span>
        </div>
        <p>
            <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">');
				document.write("Direct Link");
			document.write('</a>');/*]]>*/</script>
        </p>
        <p>
        </p>
        <h1><a name="kanchor64"></a><a name="kanchor65"></a>Messaging</h1>
        <p>In client/server it possible to send messages between a client and the server. Possible use cases could be:</p>
        <ul>
            <li value="1">Shutting down and restarting the server.</li>
            <li value="2">Triggering server backup.</li>
            <li value="3">Using a customized login strategy to restrict the number of allowed client connections.</li>
            <li value="4">Running special code on the server. For example batch updates.</li>
        </ul>
        <h2>Sending and Receiving Messages</h2>
        <p>First you need to decide on a class that you want to use as the message. Any object that is storable in db4o can be used as a message. Of course you use multiple classes for representing different messages. Let's create a dedicated class.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">public class HelloMessage
{
    private readonly string message;

    public HelloMessage(string message)
    {
        this.message = message;
    }

    public override string ToString()
    {
        return message;
    }
}
</pre>
            <div class="codesnippet-meta">MessagingExample.cs: The message class
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Public Class HelloMessage
    Private ReadOnly message As String

    Public Sub New(ByVal message As String)
        Me.message = message
    End Sub

    Public Overrides Function ToString() As String
        Return message
    End Function
End Class
</pre>
            <div class="codesnippet-meta">MessagingExample.vb: The message class
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>Now you need to register a handler to handle arriving messages. This can be done by configuring a message recipient on the server. Let's simply print out the arriving message. Additionally we answer to the client with another message.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IServerConfiguration configuration = Db4oClientServer.NewServerConfiguration();
configuration.Networking.MessageRecipient = new ServerMessageReceiver();
IObjectServer server = Db4oClientServer.OpenServer(configuration, DatabaseFile, PortNumber);</pre>
            <div class="codesnippet-meta">MessagingExample.cs: configure a message receiver for the server
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim configuration As IServerConfiguration = Db4oClientServer.NewServerConfiguration()
configuration.Networking.MessageRecipient = New ServerMessageReceiver()
Dim server As IObjectServer = Db4oClientServer.OpenServer(configuration, DatabaseFile, PortNumber)</pre>
            <div class="codesnippet-meta">MessagingExample.vb: configure a message receiver for the server
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">class ServerMessageReceiver : IMessageRecipient
{
    public void ProcessMessage(IMessageContext context, object message)
    {
        Console.WriteLine("The server received a '{0}' message", message);
        // you can respond to the client
        context.Sender.Send(new HelloMessage("Hi Client!"));
    }
}</pre>
            <div class="codesnippet-meta">MessagingExample.cs: The message receiver for the server
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Class ServerMessageReceiver
    Implements IMessageRecipient
    Public Sub ProcessMessage(ByVal context As IMessageContext, ByVal message As Object) _
        Implements IMessageRecipient.ProcessMessage
        Console.WriteLine("The server received a '{0}' message", message)
        ' you can respond to the client
        context.Sender.Send(New HelloMessage("Hi Client!"))
    End Sub
End Class</pre>
            <div class="codesnippet-meta">MessagingExample.vb: The message receiver for the server
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>The same can be done on the client. Register a handler for the received messages. </p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IClientConfiguration configuration = Db4oClientServer.NewClientConfiguration();
configuration.Networking.MessageRecipient = new ClientMessageReceiver();</pre>
            <div class="codesnippet-meta">MessagingExample.cs: configure a message receiver for a client
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim configuration As IClientConfiguration = Db4oClientServer.NewClientConfiguration()
configuration.Networking.MessageRecipient = New ClientMessageReceiver()</pre>
            <div class="codesnippet-meta">MessagingExample.vb: configure a message receiver for a client
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">class ClientMessageReceiver : IMessageRecipient
{
    public void ProcessMessage(IMessageContext context, object message)
    {
        Console.WriteLine("The client received a '{0}' message",message);
    }
}</pre>
            <div class="codesnippet-meta">MessagingExample.cs: The message receiver for the client
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Class ClientMessageReceiver
    Implements IMessageRecipient
    Public Sub ProcessMessage(ByVal context As IMessageContext, ByVal message As Object) _
        Implements IMessageRecipient.ProcessMessage
        Console.WriteLine("The client received a '{0}' message", message)
    End Sub
End Class</pre>
            <div class="codesnippet-meta">MessagingExample.vb: The message receiver for the client
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>Now on the client we can get a message sender. The message sender allows you to send a message to the server. In this example we send a hello message.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IMessageSender sender = configuration.MessageSender;
using (IObjectContainer container = Db4oClientServer.OpenClient(configuration, "localhost",
                                                                PortNumber, UserAndPassword,
                                                                UserAndPassword))
{
    sender.Send(new HelloMessage("Hi Server!"));
    WaitForAWhile();
}</pre>
            <div class="codesnippet-meta">MessagingExample.cs: Get the message sender and use it
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim sender As IMessageSender = configuration.MessageSender
Using container As IObjectContainer = Db4oClientServer.OpenClient(configuration, _
            "localhost", PortNumber, UserAndPassword, UserAndPassword)
    sender.Send(New HelloMessage("Hi Server!"))
    WaitForAWhile()
End Using</pre>
            <div class="codesnippet-meta">MessagingExample.vb: Get the message sender and use it
			<div class="codedownload"><a href="../CodeExamples/clientserver/messaging/Example-clientserver-messaging-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>