// Constructor. /////////////// internal MembershipInfo(SpreadConnection connection,SpreadMessage message, bool daemonEndianMismatch) { // Set local variables. /////////////////////// this.serviceType = message.ServiceType; this.group = message.Sender; this.members = message.Groups; // Is this a regular membership message. //////////////////////////////////////// if(IsRegularMembership) { // Extract the groupID. /////////////////////// int dataIndex = 0; int[] id = new int[3]; id[0] = SpreadConnection.toInt(message.Data, dataIndex); dataIndex += 4; id[1] = SpreadConnection.toInt(message.Data, dataIndex); dataIndex += 4; id[2] = SpreadConnection.toInt(message.Data, dataIndex); dataIndex += 4; // Endian-flip? /////////////// if(daemonEndianMismatch) { id[0] = SpreadConnection.flip(id[0]); id[1] = SpreadConnection.flip(id[1]); id[2] = SpreadConnection.flip(id[2]); } // Create the group ID. /////////////////////// this.groupID = new GroupID(id[0], id[1], id[2]); // Get the number of groups. //////////////////////////// int numGroups = SpreadConnection.toInt(message.Data, dataIndex); if(daemonEndianMismatch) { numGroups = SpreadConnection.flip(numGroups); } dataIndex += 4; // Get the groups. ////////////////// this.groups = new SpreadGroup[numGroups]; for(int i = 0 ; i < numGroups ; i++) { this.groups[i] = connection.toGroup(message.Data, dataIndex); dataIndex += SpreadConnection.MAX_GROUP_NAME; } } }
// Puts a group name into an array of bytes. //////////////////////////////////////////// private static void toBytes(SpreadGroup group, byte[] buffer, int bufferIndex) { // Get the group's name. //////////////////////// byte[] name; try { name = System.Text.Encoding.ASCII.GetBytes(group.ToString()); } catch(Exception e) { // Already checked for this exception in connect. ///////////////////////////////////////////////// name = new byte[0]; Console.WriteLine(e); } // Put a cap on the length. /////////////////////////// int len = name.Length; if(len > MAX_GROUP_NAME) len = MAX_GROUP_NAME; // Copy the name into the buffer. ///////////////////////////////// System.Array.Copy(name,0,buffer,bufferIndex,len); for( ; len < MAX_GROUP_NAME ; len++ ) buffer[bufferIndex + len] = 0; }
public void AddGroup(string group) { SpreadGroup spreadGroup = new SpreadGroup(null, group); AddGroup(spreadGroup); }
public void AddGroup(SpreadGroup group) { groups.Add(group); }
// Get the private group name. ////////////////////////////// private void ReadGroup() { // Read the length. /////////////////// int len; try { len = stream.ReadByte(); } catch(Exception e) { throw new SpreadException("read(): " + e); } // Check for no more data. ////////////////////////// if(len == -1) { throw new SpreadException("Connection closed during connect attempt"); } // Read the name. ///////////////// byte[] buffer = new byte[len]; int numRead; try { numRead = stream.Read(buffer,0,buffer.Length); } catch(Exception e) { throw new SpreadException("read(): " + e); } // Check for not enough data. ///////////////////////////// if(numRead != len) { throw new SpreadException("Connection closed during connect attempt"); } // Store the group. /////////////////// group = new SpreadGroup(this, System.Text.Encoding.ASCII.GetString(buffer,0,buffer.Length)); }
private void UserCommand() { // Show the prompt. /////////////////// Console.Write("\n" + "User> "); // Get the input. ///////////////// string[] tokens = Console.ReadLine().Split(new Char[] {' '}); // Check what it is. //////////////////// SpreadMessage msg; char command = tokens[0].Length>0?tokens[0][0]:'\n'; try { switch(command) { //JOIN case 'j': // Join the group. ////////////////// if(tokens.Length > 1) { group = new SpreadGroup(); group.Join(connection, tokens[1]); Console.WriteLine("Joined " + group + "."); } else { System.Console.Error.WriteLine("No group name."); } break; //LEAVE case 'l': // Leave the group. /////////////////// if(group != null) { group.Leave(); Console.WriteLine("Left " + group + "."); } else { Console.WriteLine("No group to leave."); } break; //SEND case 's': // Get a new outgoing message. ////////////////////////////// msg = new SpreadMessage(); msg.IsSafe = true; // Add the groups. ////////////////// for(int i=1;i < tokens.Length;i++) msg.AddGroup(tokens[i]); // Get the message. /////////////////// Console.Write("Enter message: "); msg.Data = System.Text.Encoding.ASCII.GetBytes(Console.ReadLine()); // Send it. /////////// connection.Multicast(msg); // Increment the sent message count. //////////////////////////////////// numSent++; // Show how many were sent. /////////////////////////// Console.WriteLine("Sent message " + numSent + "."); break; //BURST case 'b': // Get a new outgoing message. ////////////////////////////// msg = new SpreadMessage(); msg.IsSafe = true;; // Get the group. ///////////////// if(tokens.Length > 1) { msg.AddGroup(tokens[1]); } else { Console.Error.WriteLine("No group name."); break; } // Get the message size. //////////////////////// Console.Write("Enter the size of each message: "); int size = int.Parse(Console.ReadLine()); if(size < 0) size = 20; // Send the messages. ///////////////////// Console.WriteLine("Sending 10 messages of " + size + " bytes."); byte[] data = new byte[size]; for(int i = 0 ; i < size ; i++) { data[i] = 0; } for(int i = 0 ; i < 10 ; i++) { // Increment the sent message count. //////////////////////////////////// numSent++; // Set the message data. //////////////////////// byte[] mess = System.Text.Encoding.ASCII.GetBytes("mess num " + i); Array.Copy(mess, 0, data, 0, mess.Length); msg.Data = data; // Send the message. //////////////////// connection.Multicast(msg); Console.WriteLine("Sent message " + (i + 1) + " (total " + numSent + ")."); } break; //RECEIVE case 'r': if(listening == false) { // Receive a message. ///////////////////// DisplayMessage(connection.Receive()); } break; //POLL case 'p': if(listening == false) { // Poll. //////// if(connection.Poll() == true) { Console.WriteLine("There is a message waiting."); } else { Console.WriteLine("There is no message waiting."); } } break; //THREAD case 't': if(listening) { connection.OnRegularMessage -= handler; connection.OnMembershipMessage -= handler; if (rt.threadSuspended) lock(rt) { Monitor.Pulse(rt); rt.threadSuspended = false; } } else { connection.OnRegularMessage += handler; connection.OnMembershipMessage += handler; lock(rt) { rt.threadSuspended = true; } } listening = !listening; break; //QUIT case 'q': // Disconnect. ////////////// connection.Disconnect(); // Quit. //////// Environment.Exit(0); break; default: // Unknown command. /////////////////// Console.WriteLine("Unknown command"); // Show the menu again. /////////////////////// PrintMenu(); break; } } catch(Exception e) { Console.WriteLine(e); Environment.Exit(1); } }
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); } }