Skip to content

Commit

Permalink
Implement Register
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed Apr 29, 2024
1 parent 31b0890 commit 86f5693
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,28 @@ class Session {

Map<int, Completer<Result>> callRequests = {};

final Map<int, RegisterRequest> registerRequests = {};
final Map<int, Yield Function(Invocation)> registrations = {};

void processIncomingMessage(Message msg) {
if (msg is Result) {
var request = callRequests.remove(msg.requestID);
if (request != null) {
request.complete(msg);
}
} else if (msg is Registered) {
var request = registerRequests.remove(msg.requestID);
if (request != null) {
registrations[msg.registrationID] = request.endpoint;
request.future.complete(Registration(msg.registrationID));
}
} else if (msg is Invocation) {
var endpoint = registrations[msg.registrationID];
if (endpoint != null) {
Yield yield = endpoint(msg);
Uint8List data = wampSession.sendMessage(yield);
baseSession.send(data);
}
}
}

Expand All @@ -51,4 +67,15 @@ class Session {

return completer.future;
}

Future<Registration> register(String procedure, Yield Function(Invocation) endpoint) {
var register = Register(nextID, procedure);

var completer = Completer<Registration>();
registerRequests[register.requestID] = RegisterRequest(completer, endpoint);

baseSession.send(wampSession.sendMessage(register));

return completer.future;
}
}
14 changes: 14 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "dart:async";
import "dart:io";

import "package:wampproto/messages.dart";
import "package:wampproto/serializers.dart";
import "package:wampproto/session.dart";

Expand All @@ -24,3 +25,16 @@ class BaseSession {
await _ws.close();
}
}

class Registration {
Registration(this.registrationID);

final int registrationID;
}

class RegisterRequest {
RegisterRequest(this.future, this.endpoint);

final Completer<Registration> future;
final Yield Function(Invocation) endpoint;
}

0 comments on commit 86f5693

Please sign in to comment.