[ 
https://issues.apache.org/jira/browse/GOSSIP-79?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15973943#comment-15973943
 ] 

ASF GitHub Bot commented on GOSSIP-79:
--------------------------------------

Github user edwardcapriolo commented on a diff in the pull request:

    https://github.com/apache/incubator-gossip/pull/47#discussion_r112104663
  
    --- Diff: 
gossip-base/src/main/java/org/apache/gossip/protocol/JacksonProtocolManager.java
 ---
    @@ -0,0 +1,131 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.gossip.protocol;
    +
    +import com.codahale.metrics.Meter;
    +import com.codahale.metrics.MetricRegistry;
    +import com.fasterxml.jackson.core.JsonGenerator;
    +import com.fasterxml.jackson.databind.ObjectMapper;
    +import org.apache.gossip.GossipSettings;
    +import org.apache.gossip.crdt.CrdtModule;
    +import org.apache.gossip.manager.PassiveGossipConstants;
    +import org.apache.gossip.model.Base;
    +import org.apache.gossip.model.SignedPayload;
    +
    +import java.io.File;
    +import java.io.FileInputStream;
    +import java.io.IOException;
    +import java.security.InvalidKeyException;
    +import java.security.KeyFactory;
    +import java.security.NoSuchAlgorithmException;
    +import java.security.NoSuchProviderException;
    +import java.security.PrivateKey;
    +import java.security.Signature;
    +import java.security.SignatureException;
    +import java.security.spec.InvalidKeySpecException;
    +import java.security.spec.PKCS8EncodedKeySpec;
    +
    +// this class is constructed by reflection in GossipManager.
    +public class JacksonProtocolManager implements ProtocolManager {
    +  
    +  private final ObjectMapper objectMapper;
    +  private final PrivateKey privKey;
    +  private final Meter signed;
    +  private final Meter unsigned;
    +  
    +  /** required for reflection to work! */
    +  public JacksonProtocolManager(GossipSettings settings, String id, 
MetricRegistry registry) {
    +    // set up object mapper.
    +    objectMapper = buildObjectMapper(settings);
    +    
    +    // set up message signing.
    +    if (settings.isSignMessages()){
    +      File privateKey = new File(settings.getPathToKeyStore(), id);
    +      File publicKey = new File(settings.getPathToKeyStore(), id + ".pub");
    +      if (!privateKey.exists()){
    +        throw new IllegalArgumentException("private key not found " + 
privateKey);
    +      }
    +      if (!publicKey.exists()){
    +        throw new IllegalArgumentException("public key not found " + 
publicKey);
    +      }
    +      try (FileInputStream keyfis = new FileInputStream(privateKey)) {
    +        byte[] encKey = new byte[keyfis.available()];
    +        keyfis.read(encKey);
    +        keyfis.close();
    +        PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(encKey);
    +        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    +        privKey = keyFactory.generatePrivate(privKeySpec);
    +      } catch (NoSuchAlgorithmException | InvalidKeySpecException | 
IOException e) {
    +        throw new RuntimeException("failed hard", e);
    +      }
    +    } else {
    +      privKey = null;
    +    }
    +    
    +    signed = registry.meter(PassiveGossipConstants.SIGNED_MESSAGE);
    +    unsigned = registry.meter(PassiveGossipConstants.UNSIGNED_MESSAGE);
    +  }
    +
    +  @Override
    +  public byte[] write(Base message) throws IOException {
    +    byte[] json_bytes;
    +    if (privKey == null){
    +      json_bytes = objectMapper.writeValueAsBytes(message);
    +    } else {
    +      SignedPayload p = new SignedPayload();
    +      p.setData(objectMapper.writeValueAsString(message).getBytes());
    +      p.setSignature(sign(p.getData(), privKey));
    +      json_bytes = objectMapper.writeValueAsBytes(p);
    +    }
    +    return json_bytes;
    +  }
    +
    +  @Override
    +  public Base read(byte[] buf) throws IOException {
    +    Base activeGossipMessage = objectMapper.readValue(buf, Base.class);
    +    if (activeGossipMessage instanceof SignedPayload){
    +      SignedPayload s = (SignedPayload) activeGossipMessage;
    +      signed.mark();
    +      return objectMapper.readValue(s.getData(), Base.class);
    +    } else {
    +      unsigned.mark();
    +      return activeGossipMessage;
    +    }
    +  }
    +
    +  public static ObjectMapper buildObjectMapper(GossipSettings settings) {
    +    ObjectMapper om = new ObjectMapper();
    +    om.enableDefaultTyping();
    +    // todo: should be specified in the configuration.
    --- End diff --
    
    Agreed. It would be hard to add a handler at runtime without being able to 
get at this. For now it is probably ok.


> create gossip-transport-udp module
> ----------------------------------
>
>                 Key: GOSSIP-79
>                 URL: https://issues.apache.org/jira/browse/GOSSIP-79
>             Project: Gossip
>          Issue Type: Improvement
>            Reporter: Gary Dusbabek
>            Assignee: Gary Dusbabek
>
> Create a transport module that houses the UDP transport.
> This will probably require some refactoring. It may be prudent to create a 
> few interfaces that are kept in gossip-core which are then implemented in 
> gossip-transport-udp.
> This probably needs to be a modules
> 1 udp-server
> 2 udp-client
> OtherThings:
> GossipManager.build() needs to look at the URI and dynamically load the right 
> server
> GossipCore.send() needs to locate the right client for a URI.
> Both of these things probably need a registry. Please dont make it a static 
> singleton thing ! :)



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to