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)
Those assemblies that are for supporting logging, I use the Log2Console. You can download it here from codeplex.
And the main infrastructure assemblies:
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:
Download from NLog and add the following assemblies:
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.
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:
- 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)
- 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.
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:
<security mode="None" />