/** * 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"); }
/** * 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); }
/** * Decodes the byte equivalent of a CNSMessage * * @param bytes * The byte equivalent of a CNSMessage * @return The recreated CNSMessage * @//throws IOException * Thrown if something goes wrong during the recreation */ public Object filterRX(byte[] bytes) ////throws IOException { this.byteIn = new MemoryStream(bytes); this.dis = new BinaryReader(byteIn); // Recreate the message CNSMessage msg = new CNSMessage(); msg.type = this.dis.ReadByte(); msg.success = this.dis.ReadBoolean(); msg.location1 = NetChannelLocation.parse(this.dis.ReadString()); msg.location2 = NetChannelLocation.parse(this.dis.ReadString()); msg.name = this.dis.ReadString(); return(msg); }
/** * 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); } }
/** * Converts an object (a CNSMessage) into bytes * * @param obj * The CNSMessage to convert * @return The byte equivalent of the CNSMessage * @//throws IOException * Thrown if something goes wrong during the conversion */ public byte[] filterTX(Object obj) ////throws IOException { // First ensure we have a CNSMessage if (!(obj is CNSMessage)) { throw new IOException("Attempted to send a non CNSMessage on a CNSMessage channel"); } CNSMessage msg = (CNSMessage)obj; // Now reset the byte stream //this.baos.reset(); this.baos.Dispose(); // Write the parts of the CNSMessage to the stream this.dos.Write(msg.type); this.dos.Write(msg.success); if (msg.location1 != null) { this.dos.Write(msg.location1.ToString()); } else { this.dos.Write("null"); } if (msg.location2 != null) { this.dos.Write(msg.location2.ToString()); } else { this.dos.Write("null"); } this.dos.Write(msg.name); // Flush the stream this.dos.Flush(); // Get the bytes return(this.baos.ToArray()); }