Skip to content

Commit

Permalink
feat: get raw rtp with /x2x3/all/rtp/{xid}
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Garczorz <[email protected]>
  • Loading branch information
towi and insomnick committed Nov 13, 2024
1 parent 8c87709 commit f65d6b8
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
import com.sipgate.li.lib.x2x3.protocol.PduObject;
import com.sipgate.li.lib.x2x3.protocol.PduType;
import com.sipgate.li.simulator.x2x3.X2X3Memory;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -96,6 +102,31 @@ public ResponseEntity<List<String>> getAllX3() throws IOException {

// ================================

@GetMapping(value = "/all/rtp/{xid}", produces = "application/octet-stream")
public ResponseEntity<byte[]> getAllRtp(@PathVariable final UUID xid) throws IOException {
try (final var buf = new ByteArrayOutputStream()) {
x2X3Memory
.getStorage()
.stream()
.filter(pdu -> PduType.X3_PDU.equals(pdu.pduType()))
.filter(pdu -> pdu.xid().equals(xid))
.sorted(Comparator.comparingInt(pdu -> pdu.findSequenceNumber().orElse(-1)))
.map(PduObject::payload)
.forEach(payload -> writeToBuf(payload, buf));
return ResponseEntity.ok(buf.toByteArray());
}
}

private static void writeToBuf(final byte[] payload, final ByteArrayOutputStream buf) {
try {
buf.write(payload);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

// ================================

private List<String> getStorageAsList(final Predicate<PduObject> filter) {
return x2X3Memory
.getStorage()
Expand Down

0 comments on commit f65d6b8

Please sign in to comment.