private void _listener_OnReceiveMessage(object sender, ReceiveMessageEventArgs e) { LogBroker.Log(string.Format("RX {0} from {1}", e.Message.Type, e.Message.SourceID)); switch (e.Message.Type) { case MessageType.KeyDistribute: AddKeys((e.Message as KeyDistributeMessage).Keys); _actualResponses++; break; } if (_actualResponses == _joinAck.ExpectedResponses) { var joinAckAckMsg = new JoinAckAckMessage() { SourceID = _config.ID, DestinationID = _joinAck.SourceID }; SendMessage(joinAckAckMsg, _group); } }
public void Leave() { if (!_isJoined) { return; } _processor.Stop(); var keys = _processor.Get(_processor.KeyCount); // Do this before instantiating leaver since it uses the same socket _group.OnReceiveMessage -= _group_OnReceiveMessage; // If we're the only node, there is no need to redistribute keys if (_nodeCount > 1) { using (var _leaver = new Leaver(_config, _group, keys)) _leaver.Leave(); } // Kill socket so we don't respond to group messages _group.Dispose(); _isJoined = false; LogBroker.Log(string.Format("{0} keys processed", _processor.ProcessedKeyCount)); LogBroker.Log("Leave Complete"); }
private void _t_Elapsed(object sender, Timers.ElapsedEventArgs e) { LogBroker.Log("Join timed out"); _joinSucceeded = false; _joinFinished = true; }
public void Append(byte[] data) { _buffer.AddRange(data); while (true) { if (_buffer.Count < 5) { break; } int length = BitConverter.ToInt32(_buffer.Skip(1).Take(4).ToArray(), 0); if (_buffer.Count >= length + 5) { var message = _buffer.Take(length + 5).ToArray(); _buffer.RemoveRange(0, length + 5); Queue(message); } else { LogBroker.Log("RX message fragment"); break; } } }
private void SendMessage(Message msg, ISocket socket) { LogBroker.Log(string.Format("TX {0} to {1}", msg.Type, msg.DestinationID)); socket.Send(Message.Encode(msg)); }
private void _group_OnReceiveMessage(object sender, ReceiveMessageEventArgs e) { if (e.Message.Type != MessageType.JoinAck && e.Message.Type != MessageType.JoinEnd) { return; // Only care about these messages } LogBroker.Log(string.Format("RX {0} from {1}", e.Message.Type, e.Message.SourceID)); if (e.Message.DestinationID != _config.ID && e.Message.DestinationID != Constants.NULL_DESTINATION) { return; } switch (e.Message.Type) { case MessageType.JoinAck: _joinAck = e.Message as JoinAckMessage; break; case MessageType.JoinEnd: _t.Stop(); _nodeCount = (e.Message as JoinEndMessage).NodeCount; _joinSucceeded = true; _joinFinished = true; break; } }
private void Receive() { while (_running) { try { int c; var buffer = new byte[8192]; if ((c = _receiver.Client.Receive(buffer)) > 0) { buffer = buffer.Take(c).ToArray(); _builder.Append(buffer); } } catch (SocketException se) { if (se.ErrorCode != 10004) { LogBroker.Log("SocketException in multicast socket receive thread"); } //else //LogBroker.Log("SocketException in multicast socket receive thread: Probably socket.Dispose()"); } catch (Exception) { LogBroker.Log("Exception in multicast socket receive thread"); } } }
private void NotifyElection() { LogBroker.Log("I am leader"); if (OnElection != null) { OnElection(this, new EventArgs()); } }
public Coordinator() { _config = AppConfig.GetConfig(); LogBroker.Log(string.Format("My ID: {0}", _config.ID)); _responses = new List <KeyResponseMessage>(); _responseTimeout = new Timer(Int32.Parse(_config.ResponseTimeout)); _responseTimeout.AutoReset = false; _responseTimeout.Elapsed += _responseTimeout_Elapsed; }
public static List <string> GenerateKeys() { LogBroker.Log("Generating input keys"); var keys = new List <string>(); using (var rdr = new StreamReader(GetDictionaryPath())) { while (rdr.Peek() > -1) { keys.Add(rdr.ReadLine()); } } return(keys); }
public void Join() { if (_isJoined) { return; } _group = new MulticastSocket( IPAddress.Parse(_config.GroupMcg), Int32.Parse(_config.GroupPort)); List <string> keys; using (_joiner = new Joiner(_config, _group)) { bool joined = _joiner.Join(); if (joined) { _nodeCount = _joiner.GetNodeCount(); keys = _joiner.GetKeys(); } else { _nodeCount = 1; // Won't rx joinend so need to update here keys = MD5HashTableProcessor.GenerateKeys(); } } _elector = new Elector(_config, _group); _elector.OnElection += _elector_OnElection; UpdateNodeCount(_nodeCount); // Do this after joiner is disposed since it uses the same socket _group.OnReceiveMessage += _group_OnReceiveMessage; _processor = new MD5HashTableProcessor(); _processor.Add(keys); _processor.OnProcessComplete += _processor_OnProcessComplete; _processor.Start(); _isJoined = true; LogBroker.Log("Join Complete"); }
private void _group_OnReceiveMessage(object sender, ReceiveMessageEventArgs e) { if (e.Message.Type != MessageType.KeyInstruct) { return; // Only care about these messages } LogBroker.Log(string.Format("RX {0} from {1}", e.Message.Type, e.Message.SourceID)); if (e.Message.DestinationID != _config.ID) { return; } switch (e.Message.Type) { case MessageType.KeyInstruct: HandleKeyInstruct(e.Message as KeyInstructMessage); break; } }
private void _tcpServer_OnReceiveMessage(object sender, ReceiveMessageEventArgs e) { LogBroker.Log(string.Format("RX {0} from {1}", e.Message.Type, e.Message.SourceID)); switch (e.Message.Type) { case MessageType.KeyDistribute: var msg = e.Message as KeyDistributeMessage; _processor.Add(msg.Keys); var leaveAck = new LeaveAckMessage() { SourceID = _config.ID, DestinationID = Constants.NULL_DESTINATION }; SendMessage(leaveAck, _group); break; } }
private void Process() { while (_process) { lock (_keys) { if (_keys.Count > 0) { string key = _keys.First(); string hash = Hash(key); _results.Add(key, hash); _keys.Remove(key); LogBroker.Log(string.Format("{0} - {1}", key, hash)); } else { NotifyProcessComplete(); break; } } } }
private void _group_OnReceiveMessage(object sender, ReceiveMessageEventArgs e) { if (e.Message.Type != MessageType.ElectionProposal && e.Message.Type != MessageType.ElectionAck && e.Message.Type != MessageType.ElectionEnd) { return; // Only care about these messages } LogBroker.Log(string.Format("RX {0} from {1}", e.Message.Type, e.Message.SourceID)); if (e.Message.DestinationID != _config.ID && e.Message.DestinationID != Constants.NULL_DESTINATION) { return; } Debug.WriteLine(string.Format("RX {0} from {1}", e.Message.Type, e.Message.SourceID)); switch (e.Message.Type) { case MessageType.ElectionProposal: if (e.Message.SourceID != _config.ID) // Don't handle loopback proposals { HandleElectionProposal(e.Message as ElectionProposalMessage); } break; case MessageType.ElectionAck: HandleElectionAck(e.Message as ElectionAckMessage); break; case MessageType.ElectionEnd: HandleElectionEnd(e.Message as ElectionEndMessage); break; } }
private void UpdateNodeCount(int nodeCount) { LogBroker.Log(string.Format("NodeCount = {0}", nodeCount)); _nodeCount = nodeCount; _elector.UpdateNodeCount(nodeCount); }
private void _group_OnReceiveMessage(object sender, ReceiveMessageEventArgs e) { if (e.Message.Type != MessageType.Join && e.Message.Type != MessageType.KeyRequest && e.Message.Type != MessageType.KeyResponse && e.Message.Type != MessageType.KeyInstruct && e.Message.Type != MessageType.JoinAckAck && e.Message.Type != MessageType.JoinEnd && e.Message.Type != MessageType.Leave && e.Message.Type != MessageType.LeaveAck && e.Message.Type != MessageType.LeaveEnd) { return; // Only care about these messages } if (e.Message.DestinationID != _config.ID && e.Message.DestinationID != Constants.NULL_DESTINATION) { return; } LogBroker.Log(string.Format("RX {0} from {1}", e.Message.Type, e.Message.SourceID)); switch (e.Message.Type) { case MessageType.Join: HandleJoin(e.Message as JoinMessage); break; case MessageType.KeyRequest: HandleKeyRequest(e.Message as KeyRequestMessage); break; case MessageType.KeyResponse: HandleKeyResponse(e.Message as KeyResponseMessage); break; case MessageType.KeyInstruct: HandleKeyInstruct(e.Message as KeyInstructMessage); break; case MessageType.JoinAckAck: HandleJoinAckAck(e.Message as JoinAckAckMessage); break; case MessageType.JoinEnd: HandleJoinEnd(e.Message as JoinEndMessage); break; case MessageType.Leave: HandleLeave(e.Message as LeaveMessage); break; case MessageType.LeaveAck: HandleLeaveAck(e.Message as LeaveAckMessage); break; case MessageType.LeaveEnd: HandleLeaveEnd(e.Message as LeaveEndMessage); break; } }
private void _processor_OnProcessComplete(object sender, ProcessCompleteEventArgs <string, string> e) { LogBroker.Log(string.Format("Processing complete: {0} keys processed", e.Results.Count)); }
private void _t_Elapsed(object sender, Timers.ElapsedEventArgs e) { LogBroker.Log("Leave timed out"); _leaveFinished = true; }