In the last post we saw how to create a package and use that for downloading set of files.
Writing Device Agent
What do I get if I write an my own agent?
Let’s see how we can write a device agent.
When an agent is started in device, it should call ‘AcknowledgeLaunch’.
Calling AcknowledgeLaunch signifies 2 things.
Here is the code snippet to do AcknowledgeLaunch
HRESULT (*pfnGetDeviceService)(IDeviceAgentTransport**);
hMod = ::LoadLibrary(L "DeviceAgentTransport.dll");
if(hMod == NULL)
{
//Error Handle…
}
pfnGetDeviceService = (GetDeviceAgentTransportFunction) ::GetProcAddress(hMod,L"GetDeviceAgentTransport");
if( NULL == pfnGetDeviceService)
hr = pfnGetDeviceService(out_pTransport);
if(FAILED(hr))
//We got the DeviceAgentTransport. Call acknowledgelaunch to indicate all initializations are done.
hr = out_pService->AcknowledgeLaunch(count,lpServiceIds);
Now, the Device agent is ready to accept connections…
IDevicePacketStream* out_pStream;
out_pService ->AcceptConnectionEx(lpServiceId ,&out_pStream);
AcceptConnectionEx is analogous to TCP accept call. This is a blocking call and will wait till a Connect (CreatePackaetStream) call is made from desktop with the same serviced. Note that this serviced has to be one among the registered service Ids (in AcknowledgeLaunch).
Send receive Packets
Let’s send some packets to desktop.
We need to create a packet, write the info into the packet and send the packet in stream.
GetNewPacketFunc packFunc;
HINSTANCE hMod = ::LoadLibrary(L "DeviceAgentTransport.dll");
packFunc = (GetNewPacketFunc) ::GetProcAddress(hMod, L"GetNewPacket");
if( NULL == packFunc)
IPacket *writePacket;
packFunc(&writePacket);
writePacket->WriteString(szSendString); //Some string to be sent to desktop
out_pStream->write(writePacket);
Just like in TCP, Read is a blocking call. Below call will wait till some data is sent from the desktop.
IPacket *packet;
packFunc (&packet);
hr =out_pStream->Read(&packet);
Desktop Side
So far we are discussing about how the device agent will look like. We will look at the desktop counterpart for this.
//PackageId is the Guid corresponding to the package we want to deploy
RemoteAgent mRemoteAgent = Device.GetRemoteAgent(new ObjectId(packageID));
//This will start the agent. This call will be blocked till AcknowledgeLaunch is called in device side.
mRemoteAgent.Start(“”);
//This is the connect call.
//This will unblock the AcceptConnectionExe in device
DevicePAcketStream mStream = mRemoteAgent.CreatePacketStream(streamId);
Packet pkt = new Packet();
Pkt.WriteString(“hai”);
mStream.write(pkt);
pkt = mStream.Read();
string msg = pkt.ReadString();
Below is the sequence diagram for the desktop and device interaction
In next post we will look at actual File Viewer written using CoreCon API's.
--
Anand R