跳转到主要内容

单击提交则表示您同意developerWorks 的条款和条件。 查看条款和条件.

当您初次登录到 developerWorks 时,将会为您创建一份概要信息。您在 developerWorks 概要信息中选择公开的信息将公开显示给其他人,但您可以随时修改这些信息的显示状态。您的姓名(除非选择隐藏)和昵称将和您在 developerWorks 发布的内容一同显示。

所有提交的信息确保安全。

  • 关闭 [x]

当您初次登录到 developerWorks 时,将会为您创建一份概要信息,您需要指定一个昵称。您的昵称将和您在 developerWorks 发布的内容显示在一起。

昵称长度在 3 至 31 个字符之间。 您的昵称在 developerWorks 社区中必须是唯一的,并且出于隐私保护的原因,不能是您的电子邮件地址。

单击提交则表示您同意developerWorks 的条款和条件。 查看条款和条件.

所有提交的信息确保安全。

  • 关闭 [x]

Listing 4. The PeerReference class

返回原文..


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;
  }
	   
	   

返回原文.