Click here to Skip to main content
Click here to Skip to main content

Scalable state synchronization using P2P - part 3 - How to use the infrastructure (Tutorial)

, 3 Mar 2011
Rate this:
Please Sign up or sign in to vote.
In the previous blog postings I explained the features that the infrastructure provides , and how to experiment with the test application.In this post I will show how to use this infrastructure in your own project.In case you don't want to use NuGet you should jump to the next section: "Add assembli

In the previous blog postings I explained the features that the infrastructure provides , and how to experiment with the test application.

In this post I will show how to use this infrastructure in your own project.

In case you don't want to use NuGet you should jump to the next section: "Add assemblies references manually"

Install using NuGet

I assume that you are already familiar with NuGet , if not you can read more about it here.
  • Add Library Package reference:

  • Search and install the P2PSync package (by typing P2P in the search box):

  • After the package is installed, you should see the infrastructure assemblies in your project The dependencies (NLog) , and your app.config file contains the new sections.

The added assemblies: (dependencies for NLog)

  • NLog.dll
  • NLog.Extended.dll

    (own wrapper for the NLog)

  • Roniz.Diagnostics.Logging.dll

Those assemblies that are for supporting logging, I use the Log2Console. You can download it here from codeplex.
And the main infrastructure assemblies:

  • Roniz.WCF.P2P.Channels.dll
  • Roniz.WCF.P2P.Messages.dll
  • Roniz.WCF.P2P.Sync.dll

Add assemblies references manually

In case you don't want to use NuGet you should download the latest version from the codeplex project site.
After extracting the file , add the following assemblies to your project:

  • Roniz.Diagnostics.Logging.dll
  • Roniz.WCF.P2P.Channels.dll
  • Roniz.WCF.P2P.Messages.dll
  • Roniz.WCF.P2P.Sync.dll

Download from NLog and add the following assemblies:

  • NLog.dll
  • NLog.Extended.dll

Implement your own business logic classes

  • Implement own business logic synchronization class – inherit from Roniz.WCF.P2P.Sync.Interfaces.ISynchronizationBusinessLogic (In the example application this is the Roniz.WCF.P2P.ApplicationTester.MySynchronizationBusinessLogic class.) Should override the following methods:

ProvideFullPresenceInfo – can return any class that derived from FullPresenceInfo, or null if decided not to send anything to the other peers when your peer goes online.

OnOnlineAnnouncementReceived – this method calls on the other peers when they receive an announcement from the peer that comes online, the method received the derived implementation of the prior FullPresenceInfo.

ProvideCompactPresenceInfo – can return any class that derived from CompactPresenceInfo or null if decided not to send anything to the other peers before your peer goes down (called on Close() method).

OnOfflineAnnouncementReceived - this method calls on the other peers when they receive an announcement from the peer that goes offline, the method receives the derived implementation of the prior CompactPresenceInfo.

OnPresenceInfoChangedReceived - this method calls on the other peers when they receive an announcement from the peer that changed his presence information. For example, if the peer listens on some IP address for messages from other peers, and notifies this information via ProvideFullPresenceInfo() , and then the peer IP address changed, it can send this information to the other peers and they will receive it via this method.

ProvideSynchronizationResponse – calls to provide response to prior request of other peer for peer own synchronization details. Usually the peer will respond some compact response about its own data, not the full detailed data to reduce unneeded data between peers.

For example: if the data is organized in some dictionary, it will respond it’s keys and not the values – let the requested peer filter the keys that he doesn't have yet and receive the full data from them at a later time.

ProvideSynchronizationDetailRequest – calls on the peer that initiates the synchronization after they receive the response via ProvideSynchronizationResponse, and lets them request specific details for the synchronization.

ProvideSynchronizationDetailResponse – calls on the other peers to allow them to respond the full synchronization detail.

ProvideFullSynchronizationDetailResponse - calls on the other peers to allow them to respond the full synchronization detail that is not filtered by some key information, like in ProvideSynchronizationDetailResponse method.

OnSynchronizationDetailsResponseReceived - calls on the peer that initiates the synchronization after receiving the response via Full or partial synchronization.

OnUpdateReceived – calls on peers that receive theUpdate message from another peer.

[Optional] can also override the OnCommunicationStateChanged method to take action when the communication state changes.

There is also a property named IsNeedFullSynchronization that is set automatically by the infrastructure to let it determine if the peer that initiated the synchronization process needs full or partial synchronization. However, the developer can set this property. For example, if he thinks that in some conditions he needs full synchronization.

Implemented Messages

FullPresenceInfo – the class that contains the data that will be sent from the peer that comes online to the rest of the mesh peers. (In the example application this is the Roniz.WCF.P2P.ApplicationTester.Messages.MyFullPresenceInfo class.)

CompactPresenceInfo – [optional] the class that contains the data that will be sent from the peer that goes offline to the rest of the mesh peers.

(In the example application this is the Roniz.WCF.P2P.ApplicationTester.Messages.MyCompactPresenceInfo class.)

BusinessLogicMessageBase – this class is used as the base class for all synchronization messages.

Code (code examples taken from the test application)

  • Initiate the synchronization manager:

    Initiate the SynchronizationStateManager and provide it your own business logic instance:

    SyncManager = new SynchronizationStateManager(MySynchronizationBusinessLogic);

    And when you want to start the synchronization:

    SyncManager.Open();
  • Update other peers with changes:

    Optional call update on the SyncManager when it’s necessary by your application specific logic:

     var updateState = new MyStateContainer
    {
     StateDictionary = new Dictionary<Guid, MyUserUpdateState>(1)
    };
    updateState.StateDictionary.Add(id, UserState);
    SyncManager.Update(updateState);
  • Close the synchronization manager:This line will send notification to the other peers that the current peer is going down – and they can react to this message in their business logic layer.
SyncManager.Close();

Edit the app.config file

In case you use the NuGet to install the package, the app.config file is merged and already contains the following sections. If you didn't install via NuGet , you should add those sections manually by yourself.

  • client and binding in system.serviceModel section:
<system.serviceModel>
 <bindings>
  <netPeerTcpBinding>
   <binding name="NetPeerTcpBindingUnsecure">
    <security mode="None" />
   </binding>
  </netPeerTcpBinding>
 </bindings>
 <client>
  <!--<span class="code-comment">You can change here the mesh address to your specific needs--></span>
  <endpoint address="net.p2p://Roniz.WCF.P2P.Sync.DefaultMeshAddress" 
      binding="netPeerTcpBinding" 
      bindingConfiguration="NetPeerTcpBindingUnsecure" 
      contract="Roniz.WCF.P2P.Sync.Interfaces.ISynchronizedState" 
      name="SyncDefaultEndpointConfigurationName" 
      kind="" 
      endpointConfiguration="" />
    </client>
</system.serviceModel>

Have fun.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Ron zigelman
Mandalogo / E4D Solutions
Israel Israel
My name is Ron Zigelman and I am a .NET ,P2P-WCF expert and consultant. I will share some code examples that can help in some development issues. You can contact me at
roniz.net@gmail.com

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 3 Mar 2011
Article Copyright 2011 by Ron zigelman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid