Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
	public Flooder(String user, int numMessages, int numBytes, String address, int port, bool readOnly, bool writeOnly) {
		try {
			// Start timer.
			///////////////
			DateTime startTime = DateTime.Now;
			
			// Connect.
			///////////
			SpreadConnection connection = new SpreadConnection();
			connection.Connect(address, port, user, false, false);
			string privateName = connection.PrivateGroup.ToString();
			
			// Join.
			////////
			SpreadGroup group = new SpreadGroup();
			if(readOnly) {
				Console.WriteLine("Only receiving messages");
				group.Join(connection, "flooder");
			}
			else if(writeOnly) {
				Console.WriteLine("Starting multicast of " + numMessages + " messages, " + numBytes + " bytes each (self discarding).");
			}
			else {
				group.Join(connection, "flooder");
				Console.WriteLine("Starting multicast of " + numMessages + " messages, " + numBytes + " bytes each.");
			}
			
			// The outgoing message.
			////////////////////////
			SpreadMessage mout = null;
			if(readOnly == false) {
				mout = new SpreadMessage();
				mout.IsSafe = true;
				mout.Data = new byte[numBytes];
				mout.AddGroup("flooder");
			}
			
			// Send/Receive.
			////////////////
			for(int i = 1 ; i <= numMessages ; i++) {
				// Send.
				////////
				if(readOnly == false) {
					connection.Multicast(mout);
					}
				
				// Receive.
				///////////
				if((readOnly) || ((i > 50) && (writeOnly == false))) {
					SpreadMessage min;
					do {
						min = connection.Receive();
					}
					while((readOnly == false) && (privateName.Equals(min.Sender.ToString()) == false));
				}
				
				// Report.
				//////////
				if((i % 1000) == 0) {
					Console.WriteLine("Completed " + i + " messages");
				}
			}
			
			// Stop timer.
			//////////////
			DateTime stopTime = DateTime.Now;
			TimeSpan time = stopTime.Subtract(startTime);
			double Mbps = numBytes;
			Mbps *= numMessages;
			Mbps *= 8;
			if((readOnly == false) && (writeOnly == false))
				Mbps *= 2;
			Mbps *= 1000;
			Mbps /= time.TotalMilliseconds;
			Mbps /= (1024 * 1024);
			Console.WriteLine("Time: " + time + "ms (" + (int)Mbps + "." + (((int)(Mbps * 100)) % 100) + " Mbps)");
		}
		catch(Exception e) {
			Console.WriteLine(e);
		}
	}