Skip to content

dimchat/core-dart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Decentralized Instant Messaging Protocol (Dart)

License PRs Welcome Platform Issues Repo Size Tags Version

Watchers Forks Stars Followers

Dependencies

Name Version Description
Ming Ke Ming (名可名) Version Decentralized User Identity Authentication
Dao Ke Dao (道可道) Version Universal Message Module

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
import 'package:dimp/dimp.dart';


class HandshakeState {

  static const int START   = 0;  // C -> S, without session key(or session expired)
  static const int AGAIN   = 1;  // S -> C, with new session key
  static const int RESTART = 2;  // C -> S, with new session key
  static const int SUCCESS = 3;  // S -> C, handshake accepted

  static int checkState(String title, String? session) {
    if (title == 'DIM!'/* || title == 'OK!'*/) {
      return SUCCESS;
    } else if (title == 'DIM?') {
      return AGAIN;
    } else if (session == null) {
      return START;
    } else {
      return RESTART;
    }
  }
}


///  Handshake command: {
///      type : 0x88,
///      sn   : 123,
///
///      command : "handshake",    // command name
///      title   : "Hello world!", // "DIM?", "DIM!"
///      session : "{SESSION_KEY}" // session key
///  }
abstract interface class HandshakeCommand implements Command {

  static const String HANDSHAKE = 'handshake';

  String get title;
  String? get sessionKey;

  int get state;

  static HandshakeCommand start() =>
      BaseHandshakeCommand.from('Hello world!');

  static HandshakeCommand restart(String session) =>
      BaseHandshakeCommand.from('Hello world!', sessionKey: session);

  static HandshakeCommand again(String session) =>
      BaseHandshakeCommand.from('DIM?', sessionKey: session);

  static HandshakeCommand success(String? session) =>
      BaseHandshakeCommand.from('DIM!', sessionKey: session);

}


class BaseHandshakeCommand extends BaseCommand implements HandshakeCommand {
  BaseHandshakeCommand(super.dict);

  BaseHandshakeCommand.from(String title, {String? sessionKey})
      : super.fromName(HandshakeCommand.HANDSHAKE) {
    // text message
    this['title'] = title;
    // session key
    if (sessionKey != null) {
      this['session'] = sessionKey;
    }
  }

  @override
  String get title => getString('title') ?? '';

  @override
  String? get sessionKey => getString('session');

  @override
  int get state => HandshakeState.checkState(title, sessionKey);

}

Extends Content

import 'package:dimp/dimp.dart';


///  Application Customized message: {
///      type : 0xA0,
///      sn   : 123,
///
///      app   : "{APP_ID}",  // application (e.g.: "chat.dim.sechat")
///      mod   : "{MODULE}",  // module name (e.g.: "drift_bottle")
///      act   : "{ACTION}",  // action name (3.g.: "throw")
///      extra : info         // action parameters
///  }
abstract interface class AppContent implements Content {

  /// get App ID
  String get application;

  /// get Module name
  String get module;

  /// get Action name
  String get action;

  static AppContent create({
    required String app, required String mod, required String act
  }) => ApplicationContent(app: app, mod: mod, act: act);

}


class ApplicationContent extends BaseContent implements AppContent {
  ApplicationContent(super.dict);

  ApplicationContent({
    required String app, required String mod, required String act
  }) : super.fromType(ContentType.APPLICATION) {
    this['app'] = app;
    this['mod'] = mod;
    this['act'] = act;
  }

  @override
  String get application => getString('app') ?? '';

  @override
  String get module => getString('mod') ?? '';

  @override
  String get action => getString('act') ?? '';

}

Extends ID Address


Copyright © 2023 Albert Moky Followers

About

Decentralized Instant Messaging Protocol (Dart)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages