Decentralized Instant Messaging Protocol (Dart)


Dependencies
Examples
extends Command
- Handshake Command Protocol
- (C-S) handshake start
- (S-C) handshake again with new session
- (C-S) handshake restart with new session
- (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', null);
@override
int get state => HandshakeState.checkState(title, sessionKey);
}
extends Content
import 'package:dimp/dimp.dart';
/// Application Customized message: {
/// type : 0xCC,
/// 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 CustomizedContent implements Content {
/// get App ID
String get application;
/// get Module name
String get module;
/// get Action name
String get action;
static CustomizedContent create({
required String app, required String mod, required String act
}) => AppCustomizedContent.from(app: app, mod: mod, act: act);
}
class AppCustomizedContent extends BaseContent implements CustomizedContent {
AppCustomizedContent(super.dict);
AppCustomizedContent.from({
required String app, required String mod, required String act
}) : super.fromType(ContentType.CUSTOMIZED) {
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
