public void Broadcast(MessageType messageType) { PeerToPeerMessage message = new PeerToPeerMessage(messageType); var command = _peerToPeerMessageHandler.HandleMessage(message); var strMessage = Newtonsoft.Json.JsonConvert.SerializeObject(message); _webSocketsHandler.SendMessageToAllAsync(strMessage).Wait(); }
public void BroadcastBlockCreation(MessageType messageType, Block block) { var blockContent = _blockContentService.CalculateBase64(block.BlockContent); PeerToPeerMessage message = new PeerToPeerMessage(messageType, new List <BlockHeader>() { block.BlockHeader }, new List <GraphData>() { new GraphData(block.BlockHeader.DataGraphIri, blockContent) }); var strMessage = Newtonsoft.Json.JsonConvert.SerializeObject(message); _webSocketsHandler.SendMessageToAllAsync(strMessage).Wait(); }
private PeerToPeerCommand QueryAllBlocks() { try { var allBlocks = _blockService.GetAllBlocks(); var allBlockContents = _blockService.GetAllBlockContents(); Message message = new PeerToPeerMessage(MessageType.RESPONSE_BLOCKCHAIN, allBlocks, PrepareGraphData(allBlockContents)); return(new PeerToPeerCommand(CommandType.WRITE, message)); } catch (ReadingBlockException ex) { _logger.LogDebug("Exception was thrown: ", ex); Message errorMessage = new ErrorMessage(ex.Message); return(new PeerToPeerCommand(CommandType.WRITE_ERROR, errorMessage)); } }
public PeerToPeerCommand HandleMessage(PeerToPeerMessage message) { switch (message.MessageType) { case MessageType.QUERY_LATEST: return(QueryLatestBlock()); case MessageType.QUERY_ALL: return(QueryAllBlocks()); case MessageType.RESPONSE_BLOCKCHAIN: return(ResponseBlockchain(message)); default: var msg = String.Format("Unknown type of P2P message: {0}", message.MessageType); throw new ArgumentException(msg); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var message = new PeerToPeerMessage(); while (reader.Read()) { if ("type".Equals(reader.Value)) { message.MessageType = (MessageType)reader.ReadAsInt32(); } else if ("data".Equals(reader.Value)) { message.Data = HandleData(reader); } else if ("graph".Equals(reader.Value)) { message.Graph = HandleGraph(reader); } else { //_logger.LogDebug("Unknown element of parsing JSON: '{0}'.", reader.Value); } } //var jObject = JObject.Load(reader); //if (jObject["type"] != null) //{ // message.MessageType = (MessageType)jObject["type"].Value<int>(); //} //if (jObject["data"] != null && jObject["data"].Type == JTokenType.Array) //{ // message.Data = new List<BlockHeader>(); // foreach (var elem in jObject["data"].ToArray()) // { // message.Data.Add(GetData(elem.Value<JObject>())); // } //} //if (jObject["graph"] != null) //{ // message.Graph = GetGraph(jObject["graph"].Value<JObject>()); //} return(message); }
private PeerToPeerCommand QueryLatestBlock() { try { var block = _blockService.GetLatestBlock(); var message = new PeerToPeerMessage(MessageType.RESPONSE_BLOCKCHAIN, new List <BlockHeader>() { block.BlockHeader }, PrepareGraphData(new List <BlockContent>() { block.BlockContent })); return(new PeerToPeerCommand(CommandType.WRITE, message)); } catch (ReadingBlockException ex) { var errorMessage = new ErrorMessage(ex.Message); return(new PeerToPeerCommand(CommandType.WRITE_ERROR, errorMessage)); } }
public async Task ProcessMessage(WebSocket socket, string messageAsString) { try { _logger.LogDebug("Received new message from peer."); PeerToPeerMessage message = Newtonsoft.Json.JsonConvert.DeserializeObject <PeerToPeerMessage>(messageAsString); _logger.LogDebug("Handling message from peer: {0}", message); PeerToPeerCommand command = _peerToPeerMessageHandler.HandleMessage(message); _logger.LogDebug("Handling command with type '{0}' and message type '{1}'.", command.CommandType, command.GetMessageType()); switch (command.CommandType) { case CommandType.WRITE: await SendMessageAsync(socket, Newtonsoft.Json.JsonConvert.SerializeObject(command.Message)); break; case CommandType.WRITE_ERROR: await SendMessageAsync(socket, Newtonsoft.Json.JsonConvert.SerializeObject(command.Message)); break; case CommandType.BROADCAST: await SendMessageAsync(socket, Newtonsoft.Json.JsonConvert.SerializeObject(command.Message)); break; case CommandType.DO_NOTHING: _logger.LogDebug("Doing nothing."); break; default: _logger.LogWarning("Unknown command type '{0}'.", command.CommandType); break; } } catch (Exception ex) { _logger.LogError(ex, "Exception in message processing."); } }
public PeerToPeerCommand HandleMessage(PeerToPeerMessage message) { return(_peerToPeerMessageHandler.HandleMessage(message)); }
private PeerToPeerCommand ResponseBlockchain(PeerToPeerMessage message) { try { var latestBlock = _blockService.GetLatestBlock(); if (message.Data != null && message.Data.Any()) { var orderedData = message.Data.OrderBy(b => Int32.Parse(b.Index)).ToList(); var latestBlockReceived = orderedData[orderedData.Count - 1]; if (latestBlockReceived.GetIndexAsInt() > latestBlock.BlockHeader.GetIndexAsInt()) { _logger.LogDebug("The index of the received latest block is bigger than the local one."); if (latestBlock.BlockHeader.Hash.Equals(latestBlockReceived.PreviousHash)) { _logger.LogDebug("The hash of the latest block is equal to the previous hash of the received block. " + "Adding new block..."); try { _blockService.AddBlock( latestBlockReceived, ObtainBlockContent(message.Graph[message.Graph.Count - 1])); } catch (RdfSerializationException ex) { _logger.LogWarning("Unable to deserialize received graph as Turtle.", ex); } } else if (orderedData.Count == 1) { _logger.LogDebug("The received chain has only one link. Asking for whole chain."); Message returnMessage = new PeerToPeerMessage(MessageType.QUERY_ALL); return(new PeerToPeerCommand(CommandType.BROADCAST, returnMessage)); } else { _logger.LogDebug("Replacing the whole blockchain."); try { _blockService.ReplaceBlockchain(orderedData, ObtainBlockContents(message.Graph)); } catch (RdfSerializationException ex) { _logger.LogDebug("Exception was thrown while serialization RDF graph.", ex); Message returnMessage = new PeerToPeerMessage(MessageType.ERROR); return(new PeerToPeerCommand(CommandType.WRITE_ERROR, returnMessage)); } catch (CreatingBlockException ex) { _logger.LogDebug("Exception was thrown while creating blocks.", ex); Message returnMessage = new PeerToPeerMessage(MessageType.ERROR); return(new PeerToPeerCommand(CommandType.WRITE_ERROR, returnMessage)); } } } else { _logger.LogDebug("The index of the received latest block is not bigger than the local one. Ignoring."); } } } catch (ReadingBlockException ex) { _logger.LogWarning("Exception was thrown while reading block.", ex); } return(new PeerToPeerCommand(CommandType.DO_NOTHING)); }