Пример #1
0
        // Disconnects from the spread daemon.
        //////////////////////////////////////
        /**
         * Disconnects the connection to the daemon.  Nothing else should be done with this connection
         * after disconnecting it.
         *
         * @throws  SpreadException  if there is no connection or there is an error disconnecting
         * @see  SpreadConnection#connect(InetAddress, int, String, boolean, boolean)
         */
        public void Disconnect()
        {
            // Check if we're connected.
            ////////////////////////////
            if (connected == false)
            {
                throw new SpreadException("Not connected.");
            }

            // Get a new message.
            /////////////////////
            SpreadMessage killMessage = new SpreadMessage();

            // Send it to our private group.
            ////////////////////////////////
            killMessage.AddGroup(group);

            // Set the service type.
            ////////////////////////
            killMessage.ServiceType = SpreadMessage.KILL_MESS;

            // Send the message.
            ////////////////////
            Multicast(killMessage);

            // Close the socket.
            ////////////////////
            try
            {
                socket.Close();
            }
            catch (Exception e)
            {
                throw new SpreadException("close(): " + e);
            }
        }
Пример #2
0
        public void Join(SpreadConnection c, string groupName)
        {
            // Check if this group has already been joined.
            ///////////////////////////////////////////////
            if (this.connection != null)
            {
                throw new SpreadException("Already joined.");
            }

            // Set member variables.
            ////////////////////////
            this.connection = c;
            this.name = groupName;

            // Error check the name.
            ////////////////////////
            byte[] bytes;
            try
            {
                bytes = System.Text.Encoding.ASCII.GetBytes(name);
            }
            catch (Exception e)
            {
                throw new SpreadException("Encoding not supported: " + e);
            }
            for (int i = 0; i < bytes.Length; i++)
            {
                // Make sure the byte (character) is within the valid range.
                ////////////////////////////////////////////////////////////
                if ((bytes[i] < 36) || (bytes[i] > 126))
                {
                    throw new SpreadException("Illegal character in group name.");
                }
            }

            // Get a new message.
            /////////////////////
            SpreadMessage joinMessage = new SpreadMessage();

            // Set the group we're sending to.
            //////////////////////////////////
            joinMessage.AddGroup(name);

            // Set the service type.
            ////////////////////////
            joinMessage.ServiceType = SpreadMessage.JOIN_MESS;

            // Send the message.
            ////////////////////
            connection.Multicast(joinMessage);
        }
Пример #3
0
        public void Leave()
        {
            // Check if we can leave.
            /////////////////////////
            if (connection == null)
            {
                throw new SpreadException("No group to leave.");
            }

            // Get a new message.
            /////////////////////
            SpreadMessage leaveMessage = new SpreadMessage();

            // Set the group we're sending to.
            //////////////////////////////////
            leaveMessage.AddGroup(name);

            // Set the service type.
            ////////////////////////
            leaveMessage.ServiceType = SpreadMessage.LEAVE_MESS;

            // Send the message.
            ////////////////////
            connection.Multicast(leaveMessage);

            // No longer connected.
            ///////////////////////
            connection = null;
        }
Пример #4
0
 private void button3_Click(object sender, EventArgs e)
 {
     SpreadMessage msg;
     try
     {
         msg = new SpreadMessage();
         msg.IsSafe = true;
         msg.AddGroup(comboBox1.Text);
         msg.Data = System.Text.Encoding.ASCII.GetBytes(textBox3.Text);
         // Send it
         connection.Multicast(msg);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }