aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/gns/GnsRecord.java
blob: 25488863033103d036f3649f048ab08be84d2658 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 This file is part of GNUnet.
  Copyright (C) 2014 Christian Grothoff (and other contributing authors)

  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3, or (at your
  option) any later version.

  GNUnet is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
 */


package org.gnunet.gns;


import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;
import org.gnunet.construct.*;
import org.gnunet.gns.records.RecordData;
import org.gnunet.gns.records.UnknownRecordData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * A GNS record.
 */
public class GnsRecord implements Message {
    private static final Logger logger = LoggerFactory
            .getLogger(GnsRecord.class);

    private static boolean registryInitialized;

    private static BiMap<String,Long> recordTypeMap = HashBiMap.create();
    private static Map<Long,Class> recordClassMap = Maps.newHashMap();

    /**
     * Expiration, either absolute or relative time depending on 'flags'.
     */
    @UInt64
    public long expiration;

    /**
     * Size of 'recordDataBytes'.
     */
    @UInt32
    public long dataSize;

    /**
     * Type ID of this record.
     */
    @UInt32
    public long recordType;

    @UInt32
    public long flags;

    @VariableSizeIntegerArray(lengthField = "dataSize", signed = true, bitSize = 8)
    public byte[] recordDataBytes;

    public static void initializeRegistry() {
        if (registryInitialized)
            return;
        Class<?>[] classes = MessageLoader.getUnionCases(RecordData.class);
        for (Class<?> aClass : classes) {
            String recordTypeString;
            try {
                Field field = aClass.getField("recordTypeString");
                recordTypeString = (String) field.get(null);
            } catch (NoSuchFieldException e) {
                logger.warn("RecordData class {} has no recordTypeString field");
                continue;
            } catch (IllegalAccessException e) {
                logger.warn("RecordData class {} can't access recordTypeString field");
                continue;
            }

            long recordTypeId = MessageLoader.getUnionTag(RecordData.class, (Class<? extends MessageUnion>) aClass);
            recordTypeMap.put(recordTypeString, recordTypeId);
            recordClassMap.put(recordTypeId, aClass);
        }
        registryInitialized = true;
    }

    public RecordData getRecordData() {
        initializeRegistry();
        Class cls = recordClassMap.get(recordType);
        if (null == cls) {
            UnknownRecordData unknownRecordData = new UnknownRecordData();
            unknownRecordData.data = recordDataBytes;
        }
        return (RecordData) Construct.parseAs(recordDataBytes, cls);
    }

    public static GnsRecord createFromString(String typeString, String content) {
        long typeId = getIdFromString(typeString);
        if (typeId < 0)
            return null;
        return createFromString(typeId, content);
    }

    public static GnsRecord createFromString(long typeId, String content) {
        initializeRegistry();
        Class cls = recordClassMap.get(typeId);
        if (null == cls) {
            return null;
        }
        Method mth;
        try {
            mth = cls.getMethod("createFromString", String.class);
        } catch (NoSuchMethodException e) {
            return null;
        }
        try {
            return (GnsRecord) mth.invoke(null, content);
        } catch (IllegalAccessException e) {
            return null;
        } catch (InvocationTargetException e) {
            return null;
        }
    }

    public static String getStringFromId(long typeId) {
        initializeRegistry();
        if (!recordTypeMap.inverse().containsKey(typeId)) {
            return null;
        }
        return recordTypeMap.inverse().get(typeId);
    }

    public static long getIdFromString(String typeString) {
        initializeRegistry();
        if (!recordTypeMap.containsKey(typeString)) {
            return -1;
        }
        return recordTypeMap.get(typeString);
    }
}