/** * Creates a new CNSService * * @param cnsNode * The NodeID of the Node with the CNS on it * @//throws JCSPNetworkException * Thrown if something goes wrong in the underlying architecture */ public CNSService(NodeID cnsNode) // //throws JCSPNetworkException { // Create the input and output channel this.toCNS = NetChannel.one2net(new NetChannelLocation(cnsNode, 1), new CNSNetworkMessageFilter.FilterTX()); this.fromCNS = NetChannel.net2one(new CNSNetworkMessageFilter.FilterRX()); // We now need to logon to the CNS CNSMessage message = new CNSMessage(); message.type = CNSMessageProtocol.LOGON_MESSAGE; message.location1 = (NetChannelLocation)this.fromCNS.getLocation(); this.toCNS.write(message); // Wait for logon reply message CNSMessage logonReply = (CNSMessage)this.fromCNS.read(); // Check if we logged on OK if (logonReply.success == false) { Node.err.log(this.GetType(), "Failed to logon to CNS"); throw new JCSPNetworkException("Failed to Logon to CNS"); } Node.log.log(this.GetType(), "Logged into CNS"); }
/** * Creates a new BNSService * * @param bnsNode * The Node that the BNS is on * @//throws JCSPNetworkException * Thrown if something goes wrong in the underlying architecture */ public BNSService(NodeID bnsNode) // //throws JCSPNetworkException { // Create input and output end this.toBNS = NetChannel.one2net(bnsNode, 2, new BNSNetworkMessageFilter.FilterTX()); this.fromBNS = NetChannel.net2one(new BNSNetworkMessageFilter.FilterRX()); // Logon to the BNS BNSMessage message = new BNSMessage(); message.type = BNSMessageProtocol.LOGON_MESSAGE; message.serviceLocation = (NetChannelLocation)this.fromBNS.getLocation(); this.toBNS.write(message); // Wait for logon reply message BNSMessage logonReply = (BNSMessage)this.fromBNS.read(); // Check if we logged on OK if (logonReply.success == false) { Node.err.log(this.GetType(), "Failed to logon to BNS"); throw new JCSPNetworkException("Failed to logon to BNS"); } Node.log.log(this.GetType(), "Logged into BNS"); }
/** * Resolves a name on the CNS, retrieving the NetChannelLocation for the channel * * @param name * The name to resolve * @return The NetChannelLocation of the channel declared name * @//throws JCSPNetworkException * Thrown if something goes wrong in the underlying architecture */ public NetChannelLocation resolve(String name) ////throws JCSPNetworkException { // Create a temporary channel to receive the incoming NetChannelLocation NetChannelInput In = NetChannel.net2one(new CNSNetworkMessageFilter.FilterRX()); // Create a resolution message CNSMessage message = new CNSMessage(); message.type = CNSMessageProtocol.RESOLVE_REQUEST; message.location1 = (NetChannelLocation)In.getLocation(); message.name = name; // Write the resolution message to the CNS this.toCNS.write(message); // Read in reply CNSMessage reply = (CNSMessage)In.read(); // Destroy the temporary channel In.destroy(); // Now return the resolved location, or throw an exception if (reply.success == true) { return(reply.location1); } throw new JCSPNetworkException("Failed to resolve channel named: " + name); }
/** * Resolves a name on the BNS, retrieving the NetBarrierLocation for the NetBarrier * * @param name * The name to resolve from the BNS * @return The NetBarrierLocation of the NetBarrier declared with name * @//throws JCSPNetworkException * Thrown if something goes wrong in the underlying architecture */ public NetBarrierLocation resolve(String name) // //throws JCSPNetworkException { // Create a temporary channel to receive the incoming NetBarrierLocation NetChannelInput responseChan = NetChannel.net2one(new BNSNetworkMessageFilter.FilterRX()); // Create the resolution message BNSMessage message = new BNSMessage(); message.type = BNSMessageProtocol.RESOLVE_REQUEST; message.serviceLocation = (NetChannelLocation)responseChan.getLocation(); message.name = name; // Write resolution message to the BNS this.toBNS.write(message); // Read in reply BNSMessage reply = (BNSMessage)responseChan.read(); // Destroy temporary channel responseChan.destroy(); // Return result if (reply.success) { return(reply.location); } throw new JCSPNetworkException("Failed to resolve barrier named: " + name); }
/** * Registers an input end with the CNS * * @param name * The name to register the channel with * @param in * The NetChannelInput to register with the CNS * @return True if the channel was successfully registered, false otherwise */ public Boolean register(String name, NetChannelInput In) { // Ensure that only one registration can happen at a time lock (this) { // Create a new registration message CNSMessage message = new CNSMessage(); message.type = CNSMessageProtocol.REGISTER_REQUEST; message.name = name; message.location1 = (NetChannelLocation)this.fromCNS.getLocation(); message.location2 = (NetChannelLocation)In.getLocation(); // Write registration message to the CNS this.toCNS.write(message); // Read in reply CNSMessage reply = (CNSMessage)this.fromCNS.read(); return(reply.success); } }