All Downloads are FREE. Search and download functionalities are using the official Maven repository.

chat.dim.AddressNameServer Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
/* license: https://mit-license.org
 *
 *  DIM-SDK : Decentralized Instant Messaging Software Development Kit
 *
 *                                Written in 2022 by Moky 
 *
 * ==============================================================================
 * The MIT License (MIT)
 *
 * Copyright (c) 2022 Albert Moky
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * ==============================================================================
 */
package chat.dim;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import chat.dim.protocol.ID;

public class AddressNameServer implements AddressNameService {

    private final Map     reserved = new HashMap<>();
    private final Map            caches = new HashMap<>();
    private final Map> namesTables = new HashMap<>();

    protected AddressNameServer() {
        super();
        // constant ANS records
        caches.put("all",      ID.EVERYONE);
        caches.put("everyone", ID.EVERYONE);
        caches.put("anyone",   ID.ANYONE);
        caches.put("owner",    ID.ANYONE);
        caches.put("founder",  ID.FOUNDER);
        // reserved names
        for (String item : KEYWORDS) {
            reserved.put(item, true);
        }
    }

    @Override
    public boolean isReserved(String name) {
        Boolean value = reserved.get(name);
        if (value == null) {
            return false;
        }
        return value;
    }

    @Override
    public ID identifier(String name) {
        return caches.get(name);
    }

    @Override
    public List names(ID identifier) {
        List array = namesTables.get(identifier);
        if (array == null) {
            array = new ArrayList<>();
            // TODO: update all tables?
            for (Map.Entry entry : caches.entrySet()) {
                if (identifier.equals(entry.getValue())) {
                    array.add(entry.getKey());
                }
            }
            namesTables.put(identifier, array);
        }
        return array;
    }

    protected boolean cache(String name, ID identifier) {
        if (isReserved(name)) {
            // this name is reserved, cannot register
            return false;
        }
        if (identifier == null) {
            caches.remove(name);
            // TODO: only remove one table?
            namesTables.clear();
        } else {
            caches.put(name, identifier);
            // names changed, remove the table of names for this ID
            namesTables.remove(identifier);
        }
        return true;
    }

    /**
     *  Save ANS record
     *
     * @param name       - username
     * @param identifier - user ID; if empty, means delete this name
     * @return true on success
     */
    public boolean save(String name, ID identifier) {
        // TODO: save new record into database
        return cache(name, identifier);
    }

    // remove the keywords temporary before save new records
    public int fix(Map records) {
        reserved.put("assistant", false);
        //reserved.put("station", false);
        int count = 0;
        String alias;
        ID identifier;
        for (Map.Entry entry : records.entrySet()) {
            alias = entry.getKey();
            identifier = ID.parse(entry.getValue());
            if (alias == null || identifier == null) {
                // should not happen
                continue;
            }
            if (save(alias, identifier)) {
                count += 1;
            }
        }
        //reserved.put("station", true);
        reserved.put("assistant", true);
        return count;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy