use of me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket 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());
}
use of me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket 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);
};
}
use of me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket in project solarthing by wildmountainfarms.
the class PacketGroups method parseToInstancePacketGroup.
public static InstancePacketGroup parseToInstancePacketGroup(PacketGroup group, DefaultInstanceOptions defaultInstanceOptions) {
if (group instanceof InstancePacketGroup) {
return (InstancePacketGroup) group;
}
List<Packet> packets = new ArrayList<>();
String sourceId = defaultInstanceOptions.getDefaultSourceId();
int fragmentId = defaultInstanceOptions.getDefaultFragmentId();
for (Packet packet : group.getPackets()) {
if (packet instanceof InstancePacket) {
InstancePacket instancePacket = (InstancePacket) packet;
switch(instancePacket.getPacketType()) {
case SOURCE:
sourceId = ((InstanceSourcePacket) instancePacket).getSourceId();
break;
case FRAGMENT_INDICATOR:
fragmentId = ((InstanceFragmentIndicatorPacket) instancePacket).getFragmentId();
break;
default:
break;
}
} else {
packets.add(packet);
}
}
return createInstancePacketGroup(packets, group.getDateMillis(), sourceId, fragmentId);
}
Aggregations