 |
返回原文..
Listing 4. The PeerReference class
// The findResources() method returns a list of the
// resources managed by the peer. It does this by sending
// a message to the peer, and deciphering its reply.
List
findResources()
throws ReferenceException,
SPPException,
TTDException
{
LinkedList linkedlist = new LinkedList();
// A SPPChannel instance is a lot like a Socket instance.
// One is constructed from a host and a port. Instances of
// the TTDItem class (which correspond to the frames that I
// will discuss below) are written to and read from the
// channel.
SPPChannel sppchannel = new SPPChannel(m_stringHost, m_nPort);
// The command and its parameters are specified as key/value pairs.
HashMap hashmap = new HashMap();
hashmap.put("name", "$peer");
hashmap.put("action", "select");
hashmap.put("selector", "*");
// Write the command and an end-of-message frame to the channel.
TTDItem ttditem = m_ttdfactory.createTTDItem(Main.CMD.getType(), hashmap, new byte [] {});
sppchannel.writeTTDItem(ttditem);
sppchannel.writeTTDItem(sppchannel.EOM);
// Process the status frame from the peer.
ttditem = sppchannel.readTTDItem(m_ttdfactory);
if (ttditem == sppchannel.EOS)
{
System.out.println("- premature end-of-stream");
throw new ReferenceException();
}
else if (ttditem == sppchannel.EOM)
{
System.out.println("- premature end-of-message from server");
throw new ReferenceException();
}
else if (ttditem.getType().equals(Main.ERR.getType()))
{
ttditem = sppchannel.readTTDItem(m_ttdfactory);
System.out.println("- \"findResources\" failed: " + new String(ttditem.getData()));
throw new ReferenceException();
}
else if (!ttditem.getType().equals(Main.OK.getType()))
{
ttditem = sppchannel.readTTDItem(m_ttdfactory);
System.out.println("- invalid response format");
throw new ReferenceException();
}
// Process the data -- the results of the select.
while ((ttditem = sppchannel.readTTDItem(m_ttdfactory)) != null)
{
if (ttditem == sppchannel.EOM) break;
if (ttditem == sppchannel.EOS) break;
byte [] arb = ttditem.getData();
String string = new String(arb);
linkedlist.add(new ResourceReference(m_ttdfactory, m_stringHost, m_nPort, string));
}
sppchannel.close();
return linkedlist;
}
|
返回原文.
|  |
|