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/protocol.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 : i2s(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/protocol.dart';
/// Application Customized message: {
/// type : i2s(0xA0),
/// sn : 123,
///
/// app : "{APP_ID}", // application (e.g.: "chat.dim.sechat")
/// extra : info // others
/// }
class ApplicationContent extends BaseContent implements AppContent {
ApplicationContent(super.dict);
ApplicationContent({required String app}) : super.fromType(ContentType.APPLICATION) {
this['app'] = app;
}
@override
String get application => getString('app') ?? '';
}
Extends ID Address
Copyright © 2023 Albert Moky
