Skip to content

Commit

Permalink
raw router
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahad-10 committed May 2, 2024
1 parent 38ae763 commit cc28247
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/exports.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export "src/realm.dart" show Realm;
export "src/router.dart" show Router;
export "src/types.dart" show IBaseSession;
49 changes: 49 additions & 0 deletions lib/src/realm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import "package:wamp/src/types.dart";
import "package:wampproto/broker.dart";
import "package:wampproto/dealer.dart";
import "package:wampproto/messages.dart";
import "package:wampproto/types.dart";

class Realm {
Dealer dealer = Dealer();
Broker broker = Broker();

Map<int, IBaseSession> clients = {};

void attachClient(IBaseSession base) {
clients[base.id()] = base;
dealer.addSession(base.id());
broker.addSession(base.id());
}

void detachClient(IBaseSession base) {
clients.remove(base.id());
broker.removeSession(base.id());
dealer.removeSession(base.id());
}

void receiveMessage(int sessionID, Message msg) {
switch (msg.messageType()) {
case Call.id || Yield.id || Register.id || UnRegister.id:
MessageWithRecipient recipient = dealer.receiveMessage(sessionID, msg);
var client = clients[recipient.recipient];
client?.sendMessage(recipient.message);

case Publish.id || Subscribe.id || UnSubscribe.id:
List<MessageWithRecipient>? recipients = broker.receiveMessage(sessionID, msg);
if (recipients == null) {
return;
}

for (final recipient in recipients) {
var client = clients[recipient.recipient];
client?.sendMessage(msg);
}
case Goodbye.id:
dealer.removeSession(sessionID);
broker.removeSession(sessionID);
var client = clients[sessionID];
client?.ws.close();
}
}
}
46 changes: 46 additions & 0 deletions lib/src/router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import "package:wamp/src/realm.dart";
import "package:wamp/src/types.dart";
import "package:wampproto/messages.dart";

class Router {
Map<String, Realm> realms = {};

void addRealm(String name) {
realms[name] = Realm();
}

void removeRealm(String name) {
realms.remove(name);
}

bool hasRealm(String name) {
return realms.containsKey(name);
}

void attachClient(IBaseSession baseSession) {
String realm = baseSession.realm();
if (!realms.containsKey(realm)) {
throw Exception("cannot attach client to non-existent realm $realm");
}

realms[realm]?.attachClient(baseSession);
}

void detachClient(IBaseSession baseSession) {
String realm = baseSession.realm();
if (!realms.containsKey(realm)) {
throw Exception("cannot detach client from non-existent realm $realm");
}

realms[realm]?.detachClient(baseSession);
}

void receiveMessage(IBaseSession baseSession, Message msg) {
String realm = baseSession.realm();
if (!realms.containsKey(realm)) {
throw Exception("cannot process message for non-existent realm $realm");
}

realms[realm]?.receiveMessage(baseSession.id(), msg);
}
}
40 changes: 40 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "dart:html";
import "dart:typed_data";

import "package:wampproto/messages.dart";

abstract class IBaseSession {
late final WebSocket ws;

int id() {
throw UnimplementedError();
}

String realm() {
throw UnimplementedError();
}

String authid() {
throw UnimplementedError();
}

String authrole() {
throw UnimplementedError();
}

void send(Uint8List data) {
throw UnimplementedError();
}

Uint8List receive() {
throw UnimplementedError();
}

void sendMessage(Message msg) {
throw UnimplementedError();
}

Message receiveMessage() {
throw UnimplementedError();
}
}

0 comments on commit cc28247

Please sign in to comment.