Example #1
0
        /**
         * Creates a new client end of a NetBarrier
         *
         * @param loc
         *            The location of the server end of the NetBarrier
         * @param enrolled
         *            The number of locally enrolled processes
         * @return A new NetBarrier client end with the number of enrolled processes
         * @//throws JCSPNetworkException
         *             Thrown if something goes wrong in the underlying architecture
         * @//throws ArgumentException
         *             Thrown if the number of of local enrolled is outside the defined range
         */
        public static NetBarrier netBarrier(NetBarrierLocation loc, int enrolled)
        {
            NetBarrier barrierToReturn = null;

            try
            {
                barrierToReturn = NetBarrier.create(loc, enrolled);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e);
            }
            catch (JCSPNetworkException e)
            {
                Console.WriteLine(e);
            }

            return(barrierToReturn);
        }
Example #2
0
        /**
         * Static factory method for creating a client end of a NetBarrier
         *
         * @param loc
         *            The location of the server end of the connection
         * @param localEnroll
         *            The number of locally enrolled processes
         * @return A new NetBarrier client end
         * @//throws JCSPNetworkException
         *             Thrown if something goes wrong in the underlying architecture
         * @//throws ArgumentException
         *             Thrown if local enrolled is less than 1
         */
        internal static NetBarrier create(NetBarrierLocation loc, int localEnroll)
        ////throws JCSPNetworkException, ArgumentException
        {
            // First, the sanity check.
            if (localEnroll < 1)
            {
                throw new ArgumentException(
                          "Tried to create a NetBarrier with fewer than one locally enrolled process");
            }

            // Next, create the BarrierData structure
            BarrierData data = new BarrierData();

            // Set the state to OK_CLIENT
            data.state = BarrierDataState.OK_CLIENT;
            // Create the connecting channel between this object and the Links
            Any2OneChannel chan = Channel.any2one(new InfiniteBuffer());

            // Attach the output end to the structure
            data.toBarrier = chan.Out();
            // Initialise the barrier with the BarrierManager
            BarrierManager.getInstance().create(data);

            // Now check if this is a locally connected barrier
            if (loc.getNodeID().equals(Node.getInstance().getNodeID()))
            {
                // We are locally connected, so create a new NetBarrier. The constructor will connect to the Barrier server
                // end for us.
                return(new NetBarrier(data, localEnroll, 0, loc, chan.In(), null));
            }

            // We are not locally connected. Continue.

            // This is the channel we will pass to the NetBarrier
            ChannelOutput toLink;

            // First, check if the LinkManager has a connection for us.
            Link link = LinkManager.getInstance().requestLink(loc.getNodeID());

            // The previous operation returns null if no connection exists.
            if (link == null)
            {
                // No connection to the Link exists. Use the factory to get one.
                link = LinkFactory.getLink(loc.getNodeID());

                // The LinkFactory will have created and started the Link for us, if it could connect. We can continue
            }

            // Retrieve the channel connecting to the TX process
            toLink = link.getTxChannel();

            // We now need to enroll with the server end. Send the enroll message
            NetworkMessage msg = new NetworkMessage();

            msg.type = NetworkProtocol.ENROLL;
            // Destination is the VBN of the location
            msg.attr1 = loc.getVBN();
            // Attribute 2 is not used
            msg.attr2 = -1;
            // Write the message to the Link
            toLink.write(msg);
            // Register with the Link
            link.registerBarrier(data);
            // Return a new NetBarrier
            return(new NetBarrier(data, localEnroll, 0, loc, chan.In(), toLink));
        }
Example #3
0
        /**
         * The constructor for a NetBarrier
         *
         * @param barData
         *            The data structure defining the Barrier
         * @param numToEnroll
         *            The number of local processes to enroll
         * @param netNumToEnroll
         *            The number of network processes that will enroll
         * @param serverLocation
         *            The location of the server end of the NetBarrier
         * @param inToBar
         *            The channel into the NetBarrier from the Link
         * @param toLink
         *            The channel connecting the client end of a NetBarrierer to its Link
         * @//throws ArgumentException
         *             Thrown if the number of local enrolled processes is less than 1, or remote enrolled is less than 0
         */
        private NetBarrier(BarrierData barData, int numToEnroll, int netNumToEnroll, NetBarrierLocation serverLocation, AltingChannelInput inToBar, ChannelOutput toLink)
        ////throws ArgumentException
        {
            // First do some sanity checks
            if (numToEnroll < 1)
            {
                throw new ArgumentException("*** Attempt to set an enrollment of less than 1 on a NetBarrier *** \n");
            }
            if (netNumToEnroll < 0)
            {
                throw new ArgumentException("*** Attempt to create a NetBarrier with negative remote enrolls *** \n");
            }

            // Now set the standard parameters
            this.localEnrolled  = numToEnroll;
            this.localCountDown = numToEnroll;
            this.data           = barData;
            this.localLocation  = new NetBarrierLocation(Node.getInstance().getNodeID(), this.data.vbn);
            this.In             = inToBar;

            // Now check if we are a server or client end.
            if (this.data.state == BarrierDataState.OK_SERVER)
            {
                // We are a server end. There is no remote location, and we must set the networked enrolls
                this.remoteLocation            = null;
                this.initialNetEnrollCountdown = netNumToEnroll;
                this.netEnrolled  = netNumToEnroll;
                this.netCountDown = netNumToEnroll;
            }
            else
            {
                // We are a client end. Set the remote location
                this.remoteLocation = serverLocation;

                // Now, are we a locally connected barrier, or remote connected barrier
                if (serverLocation.getNodeID().equals(Node.getInstance().getNodeID()))
                {
                    this.localBar = BarrierManager.getInstance().getBarrier(serverLocation.getVBN());
                    // We are remotely connected. Get the channel connected to the server end
                    this.toLinkTX = this.localBar.toBarrier;
                    // Now we need to check if we can still enroll with it
                    lock (this.localBar)
                    {
                        if (this.localBar.state != BarrierDataState.OK_SERVER)
                        {
                            throw new JCSPNetworkException(
                                      "Attempted to enroll with a NetBarrier that is not a server end.");
                        }
                        // Set the locally connected flag to true.
                        this.locallyConnected = true;
                        // Send an enroll message
                        NetworkMessage msg = new NetworkMessage();
                        msg.type = NetworkProtocol.ENROLL;
                        // Destination is the VBN of the location, although this isn't used as we are locally connected
                        msg.attr1 = serverLocation.getVBN();
                        // Attrubute 2 is not used
                        msg.attr2 = -1;
                        // Write the enroll to the server end
                        this.toLinkTX.write(msg);
                        // We do not need to register with a Link, as we do not go down to that layer.
                    }
                }
                else
                {
                    // Otherwise we are remotely connected. Set the link connection channel to the given one.
                    this.toLinkTX = toLink;
                }
            }
        }