Search in sources :

Example 1 with InstanceTargetPacket

use of me.retrodaredevil.solarthing.packets.instance.InstanceTargetPacket in project solarthing by wildmountainfarms.

the class PacketGroups method parseToTargetPacketGroup.

public static TargetPacketGroup parseToTargetPacketGroup(PacketGroup packetGroup) {
    if (packetGroup instanceof TargetPacketGroup) {
        return (TargetPacketGroup) packetGroup;
    }
    List<Packet> packets = new ArrayList<>();
    InstanceSourcePacket sourcePacket = null;
    InstanceTargetPacket targetPacket = null;
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof InstancePacket) {
            InstancePacket instancePacket = (InstancePacket) packet;
            switch(instancePacket.getPacketType()) {
                case SOURCE:
                    sourcePacket = (InstanceSourcePacket) packet;
                    break;
                case TARGET:
                    targetPacket = (InstanceTargetPacket) packet;
                    break;
            }
        } else {
            packets.add(packet);
        }
    }
    return createTargetPacketGroup(packets, packetGroup.getDateMillis(), sourcePacket == null ? InstanceSourcePacket.UNUSED_SOURCE_ID : sourcePacket.getSourceId(), targetPacket == null ? Collections.emptyList() : targetPacket.getTargetFragmentIds());
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) InstanceFragmentIndicatorPacket(me.retrodaredevil.solarthing.packets.instance.InstanceFragmentIndicatorPacket) InstancePacket(me.retrodaredevil.solarthing.packets.instance.InstancePacket) InstanceSourcePacket(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket) InstanceTargetPacket(me.retrodaredevil.solarthing.packets.instance.InstanceTargetPacket) InstancePacket(me.retrodaredevil.solarthing.packets.instance.InstancePacket) InstanceSourcePacket(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket) InstanceTargetPacket(me.retrodaredevil.solarthing.packets.instance.InstanceTargetPacket)

Example 2 with InstanceTargetPacket

use of me.retrodaredevil.solarthing.packets.instance.InstanceTargetPacket in project solarthing by wildmountainfarms.

the class CommandManager method makeCreator.

/**
 * @param instanceTargetPacket The {@link InstanceTargetPacket} to indicate which fragments to target or null. If null, it is not added to the packet collection
 * @param commandOpenPacket The command packet
 * @return A creator to make a packet collection. When supplied with an {@link Instant} representing now, a packet collection is created.
 */
public PacketCollectionCreator makeCreator(String sourceId, ZoneId zoneId, @Nullable InstanceTargetPacket instanceTargetPacket, CommandOpenPacket commandOpenPacket, PacketCollectionIdGenerator packetCollectionIdGenerator) {
    // instanceTargetPacket may be null
    KeyPair keyPair = getKeyPair();
    InstanceSourcePacket instanceSourcePacket = InstanceSourcePackets.create(sourceId);
    // ----
    return now -> {
        PacketCollection packetCollectionToNestAndEncrypt = PacketCollections.create(now, instanceTargetPacket == null ? Arrays.asList(commandOpenPacket, instanceSourcePacket) : Arrays.asList(commandOpenPacket, instanceSourcePacket, instanceTargetPacket), "unused document ID that does not get serialized");
        // Note, on packetCollectionToNestAndEncrypt, _id is not serialized, so the generator and zoneId used above do NOT affect anything
        final String payload;
        try {
            payload = MAPPER.writeValueAsString(packetCollectionToNestAndEncrypt);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        String hashString = Long.toHexString(now.toEpochMilli()) + "," + HashUtil.encodedHash(payload);
        final String encrypted;
        try {
            synchronized (CIPHER) {
                // It's possible we could be in a multi-threaded environment, and you cannot have multiple threads using a single cipher at once
                encrypted = Encrypt.encrypt(CIPHER, keyPair.getPrivate(), hashString);
            }
        } catch (InvalidKeyException | EncryptException e) {
            throw new RuntimeException(e);
        }
        List<Packet> packets = new ArrayList<>(Arrays.asList(new ImmutableLargeIntegrityPacket(sender, encrypted, payload), instanceSourcePacket));
        if (instanceTargetPacket != null) {
            packets.add(instanceTargetPacket);
        }
        return PacketCollections.createFromPackets(now, packets, packetCollectionIdGenerator, zoneId);
    };
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) KeyPair(java.security.KeyPair) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) me.retrodaredevil.solarthing.packets.security.crypto(me.retrodaredevil.solarthing.packets.security.crypto) LoggerFactory(org.slf4j.LoggerFactory) ImmutableLargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.ImmutableLargeIntegrityPacket) Cipher(javax.crypto.Cipher) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Nullable(me.retrodaredevil.solarthing.annotations.Nullable) InstanceSourcePacket(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket) Objects.requireNonNull(java.util.Objects.requireNonNull) JacksonUtil(me.retrodaredevil.solarthing.util.JacksonUtil) Logger(org.slf4j.Logger) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StandardOpenOption(java.nio.file.StandardOpenOption) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) PublicKey(java.security.PublicKey) Instant(java.time.Instant) PacketCollectionIdGenerator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionIdGenerator) File(java.io.File) ZoneId(java.time.ZoneId) PacketCollections(me.retrodaredevil.solarthing.packets.collection.PacketCollections) InstanceSourcePackets(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePackets) List(java.util.List) PrivateKey(java.security.PrivateKey) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) InstanceTargetPacket(me.retrodaredevil.solarthing.packets.instance.InstanceTargetPacket) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) KeyPair(java.security.KeyPair) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) InstanceSourcePacket(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableLargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.ImmutableLargeIntegrityPacket) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Packet (me.retrodaredevil.solarthing.packets.Packet)2 InstanceSourcePacket (me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket)2 InstanceTargetPacket (me.retrodaredevil.solarthing.packets.instance.InstanceTargetPacket)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Supplier (java.util.function.Supplier)1