Skip to content

dimchat/core-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Decentralized Instant Messaging Protocol (Java)

License PRs Welcome Platform Issues Repo Size Tags Version

Watchers Forks Stars Followers

Dependencies

  • Latest Versions
Name Version Description
Cryptography Version Crypto Keys
Ming Ke Ming (名可名) Version Decentralized User Identity Authentication
Dao Ke Dao (道可道) Version Universal Message Module
  • build.gradle
allprojects {
    repositories {
        mavenLocal()
        mavenCentral()
        google()
    }
}

dependencies {

    // https://central.sonatype.com/artifact/chat.dim/DIMP
    implementation group: 'chat.dim', name: 'DIMP', version: '2.0.0'

}
  • pom.xml
<dependencies>

    <!-- https://mvnrepository.com/artifact/chat.dim/DIMP -->
    <dependency>
        <groupId>chat.dim</groupId>
        <artifactId>DIMP</artifactId>
        <version>2.0.0</version>
        <type>pom</type>
    </dependency>

</dependencies>

Examples

Extends Command

  • Handshake Command Protocol 0. (C-S) handshake start
    1. (S-C) handshake again with new session
    2. (C-S) handshake restart with new session
    3. (S-C) handshake success
public enum HandshakeState {
    START,    // C -> S, without session key(or session expired)
    AGAIN,    // S -> C, with new session key
    RESTART,  // C -> S, with new session key
    SUCCESS,  // S -> C, handshake accepted
}

/**
 *  Handshake command message
 *
 *  <blockquote><pre>
 *  data format: {
 *      type : 0x88,
 *      sn   : 123,
 *
 *      command : "handshake",    // command name
 *      title   : "Hello world!", // "DIM?", "DIM!"
 *      session : "{SESSION_KEY}" // session key
 *  }
 *  </pre></blockquote>
 */
public class HandshakeCommand extends BaseCommand {

    public final static String HANDSHAKE = "handshake";

    public HandshakeCommand(Map<String, Object> content) {
        super(content);
    }

    public HandshakeCommand(String text, String session) {
        super(HANDSHAKE);
        // text message
        assert text != null : "new handshake command error";
        put("title", text);
        // session key
        if (session != null) {
            put("session", session);
        }
    }

    public String getTitle() {
        return getString("title", null);
    }

    public String getSessionKey() {
        return getString("session", null);
    }

    public HandshakeState getState() {
        return checkState(getTitle(), getSessionKey());
    }

    private static HandshakeState checkState(String text, String session) {
        assert text != null : "handshake title should not be empty";
        if (text.equals("DIM!")/* || text.equals("OK!")*/) {
            return HandshakeState.SUCCESS;
        } else if (text.equals("DIM?")) {
            return HandshakeState.AGAIN;
        } else if (session == null) {
            return HandshakeState.START;
        } else {
            return HandshakeState.RESTART;
        }
    }

    //
    //  Factories
    //

    public static HandshakeCommand start() {
        return new HandshakeCommand("Hello world!", null);
    }

    public static HandshakeCommand restart(String sessionKey) {
        return new HandshakeCommand("Hello world!", sessionKey);
    }

    public static HandshakeCommand again(String sessionKey) {
        return new HandshakeCommand("DIM?", sessionKey);
    }

    public static HandshakeCommand success(String sessionKey) {
        return new HandshakeCommand("DIM!", sessionKey);
    }
}

Extends Content

/**
 *  Application Customized message: {
 *      'type' : i2s(0xA0),
 *      'sn'   : 123,
 *
 *      'app'   : "{APP_ID}",  // application (e.g.: "chat.dim.sechat")
 *      'extra' : info         // others
 *  }
 */
public class ApplicationContent extends BaseContent implements AppContent {

    public ApplicationContent(Map<String, Object> content) {
        super(content);
    }

    public ApplicationContent(String app) {
        super(ContentType.APPLICATION);
        put("app", app);
    }

    @Override
    public String getApplication() {
        return getString("app", "");
    }

}

Extends ID Address


Copyright © 2018-2025 Albert Moky Followers

About

Decentralized Instant Messaging Protocol (Java)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages