I implemented the Bridge Spanning Tree Protocol. I represented the bridges and associated ports on the bridges in two classes. The bridge was in control of all data processing, reading, and writing. This allowed my main logic to be very simple, and just called the bridge’s various methods in a while loop. The Port class simplified use of the sockets by closely linking together sockets with other necessary information about each port. Only the Bridge object calls the Port’s methods, as Ports are associated with a particular Bridge. The Ports are created within the Bridge, and can only be accessed through the Bridge.
At the beginning of bridge operation, each bridge spends time making the minimum spanning tree. During this time, all packets are received and separated into BPDU (Bridge Protocol Data Unit) and regular data packets. BPDUs are processed immediately and are used to make decisions about best root, root port, designated and disabled ports, and other configuration details. Data packets are placed in the bridge’s frame buffer, to be sent later. Once the specified amount of time has passed, the bridge stops processing BPDUs to make the minimum spanning tree, and then forwards all data in the frame buffer to the correct destination (whether that is forwarding to a specific port or broadcasting on all ports).
During regular operation, the bridge spends a short amount of time receiving all data packets. After this period is over, all data is sent to the correct destination. For late-start and early-stop bridges, the other bridges will process its BPDU (or will see its timeout), trigger a recreation of the minimum spanning tree, and will continue with normal operation.
For more information about this, see its Wikipedia page.
If you'd like to see the code, please feel free to email me.