|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
BackgroundThis article introduces Microsoft's new peer-to-peer Collaboration technology in Windows Vista. Collaboration enables a new generation of serverless applications, and provides:
Naming and DiscoveryPeer Name Resolution Protocol (PNRP) is a serverless DNS that exists in today's versions of Windows. It's used by peer-to-peer applications to register their presence and discover other peers on the local network or Internet. PNRP capabilities are vast, however, it relies heavily on functioning IPv6 infrastructure and IPv4 to IPv6 tunneling. Windows Vista includes new improvements in this area, but is the topic for another article. To simplify discover on a subnet, Microsoft has added the People Near Me functionality to Windows Vista. Applications must Sign-In to alert others of their presence and capabilities. Once signed in, an application can publish data to indicate what applications are installed or running on the local computer, or what data is needed to launch an application. Applications can also use the underlying collaboration API to enumerate other peers on the subnet and discover their application capabilities. People Near Me provides a simple way for a user to discover people in the same office or meeting room to create ad-hoc, real-time work groups to share resources and data. It remains to be seen if this functionality will be available in a future Windows XP service pack. ContactsOnce you have discovered someone with whom you want to have collaboration over time, you can save them as a contact. The Collaboration API includes methods to manage contacts discovered on the subnet and store them in the Windows Address Book (WAB). It also provides APIs to create invitations to collaborate using a common, registered application. Finally, APIs are provided to monitor a contact's presence similar to the way a contact's status can be monitored in an Instant Messenger application. Application CapabilitiesAn application can use the collaboration APIs during installation to register its capabilities and/or parameters needed to launch an application. Other users with the same application installed or running can discover and use this information to collaborate. This new functionality combined with existing peer-to-peer graphing and grouping technology will allow developers to create new and innovative applications. IntroductionMicrosoft's entire Peer-to-Peer technology is exposed through the latest Platform SDK as C/C++ API calls. However, the code in this article shows these APIs being used from .NET managed code using C#. The sample application includes a StartupBefore any other peer-to-peer collaboration APIs can be called, sealed class PeerCollabService
{
private PeerCollabService()
{
uint hr = PeerCollabNative.PeerCollabStartup(1);
if (hr != 0) throw new PeerCollabException(hr);
}
~PeerCollabService()
{
PeerCollabNative.PeerCollabShutdown();
}
public static readonly PeerCollabService
Instance = new PeerCollabService();
}
The PeerCollab ClassThe purpose of the The SignInThe
Note that the sample application only demonstrates the public void SignIn(PeerSignInOption Options)
{
uint hr =
PeerCollabNative.PeerCollabSignin(IntPtr.Zero,
Options);
if (hr != 0) throw new PeerCollabException(hr);
RegisterForEvents();
}
Note that according to Microsoft's documentation, the first parameter is supposed to take a Once Microsoft supports the SignOutThe public void SignOut(PeerSignInOption Options)
{
uint hr = PeerCollabNative.PeerCollabSignout(Options);
if (hr != 0) throw new PeerCollabException(hr);
UnregisterForEvents();
}
Note that signing in or out affects all other peer-to-peer collaboration applications running under the current Windows account. If you are planning to use collaboration from a service, you should consider creating a separate, distinct account for your service. StatusUse the public PeerSignInOption Status
{
get
{
PeerSignInOption Options;
uint hr = PeerCollabNative.PeerCollabGetSigninOptions(out Options);
if (hr != 0) throw new PeerCollabException(hr);
return Options;
}
}
People Near Me CollectionOnce your application is signed in, you can use the public PeopleNearMeCollection PeopleNearMe
{
get { return new PeopleNearMeCollection(); }
}
This property returns a public void IEnumerable.Reset()
{
uint hr;
if (hPeerEnum != IntPtr.Zero)
{
hr = PeerCollabNative.PeerEndEnumeration(hPeerEnum);
if (hr != 0) throw new PeerCollabException(hr);
hPeerEnum = IntPtr.Zero;
}
hr = PeerCollabNative.PeerCollabEnumPeopleNearMe(out hPeerEnum);
if (hr != 0) throw new PeerCollabException(hr);
uint count;
hr = PeerCollabNative.PeerGetItemCount(hPeerEnum, out count);
if (hr != 0) throw new PeerCollabException(hr);
this.count = (int)count;
index = -1;
}
EventsThe Collaboration API allows an application to register and receive events when other applications' presence or capabilities change. The following code shows how the internal private void RegisterForEvents()
{
if (hPeerEvent != IntPtr.Zero) return; // already registered
AutoResetEvent sendEvent = new AutoResetEvent(false);
Handle = ThreadPool.RegisterWaitForSingleObject(sendEvent,
new WaitOrTimerCallback(PeerEventWorker), null, -1, false);
PEER_COLLAB_EVENT_REGISTRATION[] info =
{
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_WATCHLIST_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_ENDPOINT_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_ENDPOINT_PRESENCE_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_ENDPOINT_APPLICATION_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_ENDPOINT_OBJECT_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_MY_ENDPOINT_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_MY_PRESENCE_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_MY_APPLICATION_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_MY_OBJECT_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_PEOPLE_NEAR_ME_CHANGED),
new PEER_COLLAB_EVENT_REGISTRATION(
PEER_COLLAB_EVENT_TYPE.PEER_EVENT_REQUEST_STATUS_CHANGED)
};
int size = Marshal.SizeOf(info[0]);
IntPtr infoptr = Marshal.AllocCoTaskMem(info.Length*size);
int offset = 0;
foreach (PEER_COLLAB_EVENT_REGISTRATION item in info)
{
Marshal.StructureToPtr(item, (IntPtr)(infoptr.ToInt32()+offset), false);
offset += size;
}
uint result = PeerCollabNative.PeerCollabRegisterEvent(sendEvent.Handle,
info.Length, infoptr, out hPeerEvent);
if (result != 0) Marshal.ThrowExceptionForHR((int)result);
}
The The The following code shows how the worker thread handles incoming events. private void PeerEventWorker(object xdata, bool timedOut)
{
while (true)
{
IntPtr evptr;
uint result =
PeerCollabNative.PeerCollabGetEventData(hPeerEvent, out evptr);
if (result == PeerCollabNative.PEER_S_NO_EVENT_DATA ||
evptr == IntPtr.Zero)
break;
if (result != 0) Marshal.ThrowExceptionForHR((int)result);
PEER_COLLAB_EVENT_DATA data =
(PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(evptr,
typeof(PEER_COLLAB_EVENT_DATA));
IntPtr dataptr = (IntPtr)(evptr.ToInt32() +
Marshal.SizeOf(typeof(PEER_COLLAB_EVENT_DATA)));
switch (data.eventType)
{
case PEER_COLLAB_EVENT_TYPE.PEER_EVENT_REQUEST_STATUS_CHANGED:
HandleEventRequestStatusChanged(dataptr);
break;
//...
}
PeerCollabNative.PeerFreeData(evptr);
}
}
The Event TypesWhile there are 11 types of events, this sample application only implements the The
The Using the Sample ApplicationThe sample application only runs on Windows Vista. It automatically signs you into People Near Me, and shows you whether others on the same subnet are signed in too. The top list shows you the nick names of those who are signed in. The following code shows how this is done: private void Form_Load(object sender, System.EventArgs e)
{
collab = new PeerCollab();
collab.RequestStatusChanged += new
Peer.Collaboration.PeerCollab.RequestStatusChangedHandler(
collab_RequestStatusChanged);
collab.PeopleNearMeChanged += new
PeerCollab.PeopleNearMeChangedHandler(
collab_PeopleNearMeChanged);
try
{
collab.SignIn(PeerSignInOption.NearMe);
foreach (PeopleNearMe pnm in collab.PeopleNearMe)
{
listBox1.Items.Add(pnm);
}
LogMessage(@"SignIn", "Completed");
}
catch (PeerCollabException ex)
{
LogMessage(@"SignIn", ex.Message);
}
}
Click on a name to see more information about each application signed into People Near Me. The bottom list contains a log of events or error messages that occur while the application is running. If you only have one Windows Vista computer, use the Switch User feature to login as multiple users. Each user account has its own People Near Me presence on the subnet. You only need to start the sample application under one account. Under the other accounts, open Control Panel and use People Near Me to sign-in. You will notice a new icon appears in the system tray:
Right click on these icons to change the presence or your account or one of the other accounts. Also, right click and select Properties to change the User Information nickname. Changing the name will result in an void collab_PeopleNearMeChanged(object sender,
PeerCollabPNMChangedEventArgs e)
{
if (e.PeopleNearMe == null) // its me
{
switch (e.Action)
{
case PeerChangeType.Deleted:
LogMessage(@"SignOut", "Me");
RefreshPeopleNearMe();
break;
case PeerChangeType.Added:
LogMessage(@"SignIn", "Me");
break;
case PeerChangeType.Updated:
LogMessage(@"Update", "Me");
break;
}
}
else
{
switch (e.Action) // its someone else
{
case PeerChangeType.Added:
LogMessage(@"SignIn", e.PeopleNearMe.NickName);
AddPeopleNearMe(e.PeopleNearMe);
break;
case PeerChangeType.Deleted:
// the event does not indicate who was deleted,
// so we are forced to refresh
LogMessage(@"SignOut", "Someone Near Me");
RefreshPeopleNearMe();
break;
case PeerChangeType.Updated:
LogMessage(@"Update", e.PeopleNearMe.NickName);
RefreshPeopleNearMe();
break;
}
}
}
If the Point of InterestYou can monitor my ongoing progress while writing about peer-to-peer collaboration, by visiting my blog. Links to ResourcesI have found the following resources to be very useful in understanding peer-to-peer collaboration: ConclusionI hope you have found this article interesting. I'll be writing more articles to describe the other features of Peer-to-Peer Collaboration in the coming weeks and months. If you have suggestions for other topics, please leave a comment. Finally, a big thanks to all those who have been voting. History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||