Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * File name: FaceStatusDecoder.java |
| 3 | * |
| 4 | * Purpose: Decode lists of FaceStatus objects from /localhost/nfd/faces/list |
| 5 | * |
| 6 | * © Copyright Intel Corporation. All rights reserved. |
| 7 | * Intel Corporation, 2200 Mission College Boulevard, |
| 8 | * Santa Clara, CA 95052-8119, USA |
| 9 | */ |
| 10 | package com.intel.jndn.management; |
| 11 | |
| 12 | import java.nio.ByteBuffer; |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.List; |
| 15 | import net.named_data.jndn.encoding.EncodingException; |
| 16 | import net.named_data.jndn.encoding.tlv.TlvDecoder; |
| 17 | import net.named_data.jndn.util.Blob; |
| 18 | |
| 19 | /** |
| 20 | * Decode lists of FaceStatus objects from /localhost/nfd/faces/list |
| 21 | * @author Andrew Brown <andrew.brown@intel.com> |
| 22 | */ |
| 23 | public class FaceStatusDecoder { |
| 24 | |
| 25 | /** |
| 26 | * Spec from http://redmine.named-data.net/projects/nfd/wiki/ControlCommand |
| 27 | */ |
| 28 | public static final int FACE_ID = 105; |
| 29 | public static final int URI = 114; |
| 30 | public static final int EXPIRATION_PERIOD = 109; |
| 31 | |
| 32 | /** |
| 33 | * Spec from http://redmine.named-data.net/projects/nfd/widi/FaceMgmt |
| 34 | */ |
| 35 | public static final int FACE_STATUS = 128; |
| 36 | public static final int LOCAL_URI = 129; |
| 37 | public static final int CHANNEL_STATUS = 130; |
| 38 | public static final int FACE_SCOPE = 132; |
| 39 | public static final int FACE_PERSISTENCY = 133; |
| 40 | public static final int LINK_TYPE = 134; |
| 41 | public static final int N_IN_INTERESTS = 144; |
| 42 | public static final int N_IN_DATAS = 145; |
| 43 | public static final int N_OUT_INTERESTS = 146; |
| 44 | public static final int N_OUT_DATAS = 147; |
| 45 | public static final int N_IN_BYTES = 148; |
| 46 | public static final int N_OUT_BYTES = 149; |
| 47 | |
| 48 | /** |
| 49 | * Decode a list of faces according to |
| 50 | * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt |
| 51 | * @param buffer |
| 52 | * @return |
| 53 | * @throws EncodingException |
| 54 | */ |
| 55 | public List<FaceStatus> decodeFaces(ByteBuffer buffer) throws EncodingException{ |
| 56 | ArrayList<FaceStatus> faces = new ArrayList<>(); |
| 57 | TlvDecoder decoder = new TlvDecoder(buffer); |
| 58 | int parentEndOffset; |
| 59 | do{ |
| 60 | parentEndOffset = decoder.readNestedTlvsStart(FACE_STATUS); |
| 61 | faces.add(decodeFaceStatus(decoder, parentEndOffset)); |
| 62 | decoder.finishNestedTlvs(parentEndOffset); |
| 63 | } |
| 64 | while(parentEndOffset < buffer.limit()); |
| 65 | return faces; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Decode one face status using the current decoder |
| 70 | * @param buffer |
| 71 | * @return |
| 72 | * @throws EncodingException |
| 73 | */ |
| 74 | private static FaceStatus decodeFaceStatus(TlvDecoder decoder, int parentEndOffset) throws EncodingException{ |
| 75 | FaceStatus status = new FaceStatus(); |
| 76 | |
| 77 | // parse |
| 78 | status.faceId = (int) decoder.readNonNegativeIntegerTlv(FACE_ID); |
| 79 | Blob uri = new Blob(decoder.readBlobTlv(URI), true); // copy because buffer is immutable |
| 80 | status.uri = uri.toString(); |
| 81 | Blob localUri = new Blob(decoder.readBlobTlv(LOCAL_URI), true); // copy because buffer is immutable |
| 82 | status.localUri = localUri.toString(); |
| 83 | status.expirationPeriod = (int) decoder.readOptionalNonNegativeIntegerTlv(EXPIRATION_PERIOD, parentEndOffset); |
| 84 | status.faceScope = FaceScope.values()[(int) decoder.readNonNegativeIntegerTlv(FACE_SCOPE)]; |
| 85 | status.facePersistency = FacePersistency.values()[(int) decoder.readNonNegativeIntegerTlv(FACE_PERSISTENCY)]; |
| 86 | status.linkType = LinkType.values()[(int) decoder.readNonNegativeIntegerTlv(LINK_TYPE)]; |
| 87 | status.inInterests = (int) decoder.readNonNegativeIntegerTlv(N_IN_INTERESTS); |
| 88 | status.inDatas = (int) decoder.readNonNegativeIntegerTlv(N_IN_DATAS); |
| 89 | status.outInterests = (int) decoder.readNonNegativeIntegerTlv(N_OUT_INTERESTS); |
| 90 | status.outDatas = (int) decoder.readNonNegativeIntegerTlv(N_OUT_DATAS); |
| 91 | status.inBytes = (int) decoder.readNonNegativeIntegerTlv(N_IN_BYTES); |
| 92 | status.outBytes = (int) decoder.readNonNegativeIntegerTlv(N_OUT_BYTES); |
| 93 | |
| 94 | return status; |
| 95 | } |
| 96 | |
| 97 | } |