- Latest Versions
- Account
- Address
- BTC
- ETH
- Meta
- MKM (Default)
- BTC
- ETH
- Document
- Visa (User)
- Profile
- Bulletin (Group)
- Address
- Message Contents
- Text Content
- File Content
- Image Content
- Audio Content
- Video Content
- Page Content
- Name Card
- Quote Content
- Money Content
- Transfer Money
- Combine Forward
- System Commands
- Meta Command
- Document Command
- Receipt Command
- History Command
- Group Command
- Invite
- Expel
- Query
- Quit
- Join
import chat.dim.mem.MemoryCache;
import chat.dim.mem.SharedAccountCache;
import chat.dim.mkm.BaseAddressFactory;
import chat.dim.protocol.Address;
public class CompatibleAddressFactory extends BaseAddressFactory {
/**
* Call it when received 'UIApplicationDidReceiveMemoryWarningNotification',
* this will remove 50% of cached objects
*
* @return number of survivors
*/
public int reduceMemory() {
MemoryCache<String, Address> cache = SharedAccountCache.addressCache;
return cache.reduceMemory();
}
@Override
protected Address parse(String address) {
try {
Address res = super.parse(address);
if (res != null) {
return res;
}
} catch (Exception e) {
// FIXME:
e.printStackTrace();
assert false : "invalid address: " + address;
}
//
// TODO: parse for other types of address
//
int len = address == null ? 0 : address.length();
if (4 <= len && len <= 64) {
return new UnknownAddress(address);
}
assert false : "invalid address: " + address;
return null;
}
}import chat.dim.protocol.Address;
import chat.dim.type.ConstantString;
public final class UnknownAddress extends ConstantString implements Address {
public UnknownAddress(String string) {
super(string);
}
@Override
public int getNetwork() {
return 0; // EntityType.USER;
}
}import java.util.Map;
import chat.dim.mkm.*;
import chat.dim.ext.SharedAccountExtensions;
import chat.dim.protocol.Meta;
public final class CompatibleMetaFactory extends BaseMetaFactory {
public CompatibleMetaFactory(String algorithm) {
super(algorithm);
}
@Override
public Meta parseMeta(Map<String, Object> meta) {
Meta out;
GeneralAccountHelper helper = SharedAccountExtensions.helper;
String type = helper.getMetaType(info, "");
switch (type) {
case "MKM":
case "mkm":
case "1":
out = new DefaultMeta(meta);
break;
case "BTC":
case "btc":
case "2":
out = new BTCMeta(meta);
break;
case "ETH":
case "eth":
case "4":
out = new ETHMeta(meta);
break;
default:
throw new IllegalArgumentException("unknown meta type: " + type);
}
return out.isValid() ? out : null;
}
}import chat.dim.protocol.*;
import chat.dim.dkd.*;
import chat.dim.*;
public class CommonExtensionLoader extends ExtensionLoader {
@Override
protected void registerAddressFactory() {
Address.setFactory(new CompatibleAddressFactory());
}
@Override
protected void registerMetaFactories() {
Meta.Factory mkm = new CompatibleMetaFactory(MetaType.MKM);
Meta.Factory btc = new CompatibleMetaFactory(MetaType.BTC);
Meta.Factory eth = new CompatibleMetaFactory(MetaType.ETH);
Meta.setFactory("1", mkm);
Meta.setFactory("2", btc);
Meta.setFactory("4", eth);
Meta.setFactory("mkm", mkm);
Meta.setFactory("btc", btc);
Meta.setFactory("eth", eth);
Meta.setFactory("MKM", mkm);
Meta.setFactory("BTC", btc);
Meta.setFactory("ETH", eth);
}
@Override
public void registerContentFactories() {
super.registerContentFactories();
registerCustomizedFactories();
}
protected void registerCustomizedFactories() {
// Application Customized
Content.setFactory(ContentType.APPLICATION, AppCustomizedContent::new);
Content.setFactory(ContentType.CUSTOMIZED, AppCustomizedContent::new);
}
@Override
protected void registerCommandFactories() {
super.registerCommandFactories();
// Handshake
setCommandFactory(HandshakeCommand.HANDSHAKE, HandshakeCommand::new);
}
}You must ensure that every Address you extend has a Meta type that can correspond to it one by one.
