Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Scalable State Synchronization using P2P - Part 3 - How to Use the Infrastructure (Tutorial)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Mar 2011Ms-PL5 min read 10.7K   1  
How to use the infrastructure in your own project

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 its 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 to 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 the Update 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 under 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:

    C#
    SyncManager = new SynchronizationStateManager(MySynchronizationBusinessLogic);

    And when you want to start the synchronization:

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

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

    C#
     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.
    C#
    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:
    XML
    <system.serviceModel>
     <bindings>
      <netPeerTcpBinding>
       <binding name="NetPeerTcpBindingUnsecure">
        <security mode="None" />
       </binding>
      </netPeerTcpBinding>
     </bindings>
     <client>
      <!--You can change here the mesh address to your specific needs-->
      <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)


Written By
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 --