/// <summary> /// Determines if the outgoing message is local, or needs to be routed through /// the node manager. /// </summary> /// <param name="msg">the outgoing message wrapped in a Send wrapper.</param> public void sendMessage(ref Send msg) { // Pull the destination ID JausAddress destination = new JausAddress((ushort)msg.getBody().getSendRec().getDestinationID().getSubsystemID(), (byte)msg.getBody().getSendRec().getDestinationID().getNodeID(), (byte)msg.getBody().getSendRec().getDestinationID().getComponentID()); // If the destination is local, loopback to the route message function if (destination.get() == jausAddress.get()) { Receive message = new Receive(); message.getBody().getReceiveRec().getSourceID().setSubsystemID(jausAddress.getSubsystemID()); message.getBody().getReceiveRec().getSourceID().setNodeID(jausAddress.getNodeID()); message.getBody().getReceiveRec().getSourceID().setComponentID(jausAddress.getComponentID()); message.getBody().getReceiveRec().getMessagePayload().set(msg.getBody().getSendRec().getMessagePayload().getLength(), msg.getBody().getSendRec().getMessagePayload().getData()); routeMessage(message); } // Otherwise, forward the message to NodeManager else { JrErrorCode ret = JuniorAPI.JrSend((int)jrHandle, destination.get(), (uint)msg.getBody().getSendRec().getMessagePayload().getLength(), msg.getBody().getSendRec().getMessagePayload().getData()); } }
/// <summary> /// Constructor. Connects with nodeManager and sets up variables. /// </summary> /// <param name="jausAddress">The JausAddress of the current component.</param> public JausRouter(JausAddress jausAddress, InternalEventHandler ieHandler) { this.jausAddress = jausAddress; this.ieHandler = ieHandler; jrHandle = 0; if (JuniorAPI.JrConnect((int)jausAddress.get(), "nm.cfg", ref jrHandle) != 0) { Console.WriteLine("UNABLE TO CONNECT TO THE NODE MANAGER. IS IT RUNNING?"); } }
/// <summary> /// Overrides the SimpleThread.stop() method. /// Wakes up any sleeping C++ threads. /// Joins JausRouter thread. /// </summary> public override void stop() { lock (runLock){ isRunning = false; } // Send a message to ourselves to wake up the thread. byte[] msg_id = { 0 }; JuniorAPI.JrSend(jrHandle, jausAddress.get(), (uint)2, msg_id); thread.Join(THREAD_JOIN_TIME * 10); }
public void updateJausID(JausAddress jausAddress, bool allowWildcards = false) { // Request to change our JAUS ID. This means we have to close the current connection // to junior, and recreate it. But first we need to stop the receive thread. stop(); JuniorAPI.JrDisconnect((int)jrHandle); this.jausAddress = jausAddress; jrHandle = 0; int jWildcards = allowWildcards ? 1 : 0; if (JuniorAPI.JrConnect((int)jausAddress.get(), "nm.cfg", ref jrHandle, jWildcards) != 0) { Console.WriteLine("UNABLE TO CONNECT TO THE NODE MANAGER. IS IT RUNNING?"); } start(); }
/// <summary> /// Checks the C++ framework for incoming messages, routing them as needed. /// </summary> public override void run() { int priority = 0; uint source = 0; int flags = 0; uint bufsize = 0; byte[] buffer; ushort msg_id = 0; //runLock._lock(); isRunning = true; //runLock.unlock(); while (isRunning) { buffer = JuniorAPI.JrReceive((int)jrHandle, ref source, ref priority, ref flags, ref msg_id); if (buffer != null & buffer.Length > 0) { if (!isRunning) { break; } bufsize = (uint)buffer.Length; byte[] tmpBuff = new byte[buffer.Length]; for (int i = 0; i < buffer.Length; i++) { tmpBuff[i] = buffer[i]; } buffer = null; Receive message = new Receive(); JausAddress sender = new JausAddress(source); message.getBody().getReceiveRec().setSrcSubsystemID(sender.getSubsystemID()); message.getBody().getReceiveRec().setSrcNodeID(sender.getNodeID()); message.getBody().getReceiveRec().setSrcComponentID(sender.getComponentID()); message.getBody().getReceiveRec().getMessagePayload().set(bufsize, tmpBuff); routeMessage(message); } else { //OS.JrSleep(1); //throttle } } }