Skip to content

Commit

Permalink
Merge pull request Matthias247#58 from cognitree/handshake-details
Browse files Browse the repository at this point in the history
welcome/hello message detials never change, so reuse it
  • Loading branch information
Matthias247 committed Jul 2, 2015
2 parents 408e6e9 + 1c807a3 commit 5a0394a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 52 deletions.
36 changes: 19 additions & 17 deletions jawampa-core/src/main/java/ws/wamp/jawampa/WampRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class WampRouter {
/** Represents a realm that is exposed through the router */
static class Realm {
final RealmConfig config;
final ObjectNode welcomeDetails;
final Map<Long, ClientHandler> channelsBySessionId = new HashMap<Long, ClientHandler>();
final Map<String, Procedure> procedures = new HashMap<String, Procedure>();

Expand All @@ -86,6 +87,22 @@ public Realm(RealmConfig config) {
subscriptionsByFlags.put(SubscriptionFlags.Exact, new HashMap<String, Subscription>());
subscriptionsByFlags.put(SubscriptionFlags.Prefix, new HashMap<String, Subscription>());
subscriptionsByFlags.put(SubscriptionFlags.Wildcard, new HashMap<String, Subscription>());

// Expose the roles that are configured for the realm
ObjectMapper objectMapper = new ObjectMapper();
welcomeDetails = objectMapper.createObjectNode();
welcomeDetails.put("agent", Version.getVersion());
ObjectNode routerRoles = welcomeDetails.putObject("roles");
for (WampRoles role : config.roles) {
ObjectNode roleNode = routerRoles.putObject(role.toString());
if (role == WampRoles.Publisher) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("publisher_exclusion", true);
} else if (role == WampRoles.Subscriber) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("pattern_based_subscription", true);
}
}
}

void includeChannel(ClientHandler channel, long sessionId, Set<WampRoles> roles) {
Expand Down Expand Up @@ -921,24 +938,9 @@ private void onMessageFromUnregisteredChannel(ClientHandler channelHandler, Wamp
realm.includeChannel(channelHandler, sessionId, roles);
// Remove the channel from the idle channel list - It is no longer idle
idleChannels.remove(channelHandler.controller);

// Expose the roles that are configured for the realm
ObjectNode welcomeDetails = objectMapper.createObjectNode();
welcomeDetails.put("agent", Version.getVersion());
ObjectNode routerRoles = welcomeDetails.putObject("roles");
for (WampRoles role : realm.config.roles) {
ObjectNode roleNode = routerRoles.putObject(role.toString());
if (role == WampRoles.Publisher) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("publisher_exclusion", true);
} else if (role == WampRoles.Subscriber) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("pattern_based_subscription", true);
}
}


// Respond with the WELCOME message
WelcomeMessage welcome = new WelcomeMessage(channelHandler.sessionId, welcomeDetails);
WelcomeMessage welcome = new WelcomeMessage(channelHandler.sessionId, realm.welcomeDetails);
channelHandler.controller.sendMessage(welcome, IWampConnectionPromise.Empty);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import ws.wamp.jawampa.WampRoles;
import ws.wamp.jawampa.auth.client.ClientSideAuthentication;
import ws.wamp.jawampa.connection.IWampConnector;
import ws.wamp.jawampa.connection.IWampConnectorProvider;
import ws.wamp.jawampa.internal.Version;

/**
* Stores various configuration data for WAMP clients
Expand All @@ -51,6 +54,8 @@ public class ClientConfiguration {
final IWampConnectorProvider connectorProvider;
/** The connector which is used to create new connections to the remote peer */
final IWampConnector connector;

final ObjectNode helloDetails;

public ClientConfiguration(
boolean closeClientOnErrors,
Expand Down Expand Up @@ -82,6 +87,36 @@ public ClientConfiguration(

this.connectorProvider = connectorProvider;
this.connector = connector;

// Put the requested roles in the Hello message
helloDetails = objectMapper.createObjectNode();
helloDetails.put("agent", Version.getVersion());

ObjectNode rolesNode = helloDetails.putObject("roles");
for (WampRoles role : clientRoles) {
ObjectNode roleNode = rolesNode.putObject(role.toString());
if (role == WampRoles.Publisher ) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("publisher_exclusion", true);
} else if (role == WampRoles.Subscriber) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("pattern_based_subscription", true);
} else if (role == WampRoles.Caller) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("caller_identification", true);
}
}

// Insert authentication data
if(authId != null) {
helloDetails.put("authid", authId);
}
if (authMethods != null && authMethods.size() != 0) {
ArrayNode authMethodsNode = helloDetails.putArray("authmethods");
for(ClientSideAuthentication authMethod : authMethods) {
authMethodsNode.add(authMethod.getAuthMethod());
}
}
}

public boolean closeClientOnErrors() {
Expand Down Expand Up @@ -128,4 +163,8 @@ public String authId() {
public List<ClientSideAuthentication> authMethods() {
return new ArrayList<ClientSideAuthentication>(authMethods);
}

ObjectNode helloDetails(){
return helloDetails;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
import ws.wamp.jawampa.auth.client.ClientSideAuthentication;
import ws.wamp.jawampa.connection.IConnectionController;
import ws.wamp.jawampa.connection.IWampConnectionPromise;
import ws.wamp.jawampa.internal.Version;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
Expand Down Expand Up @@ -123,40 +121,8 @@ void sendHelloMessage() {
// Connection to the remote host was established
// However the WAMP session is not established until the handshake was finished

// Put the requested roles in the Hello message
ObjectNode o = stateController.clientConfig().objectMapper().createObjectNode();
o.put("agent", Version.getVersion());

ObjectNode rolesNode = o.putObject("roles");
for (WampRoles role : stateController.clientConfig().clientRoles()) {
ObjectNode roleNode = rolesNode.putObject(role.toString());
if (role == WampRoles.Publisher ) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("publisher_exclusion", true);
} else if (role == WampRoles.Subscriber) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("pattern_based_subscription", true);
} else if (role == WampRoles.Caller) {
ObjectNode featuresNode = roleNode.putObject("features");
featuresNode.put("caller_identification", true);
}
}

// Insert authentication data
String authId = stateController.clientConfig().authId();
List<ClientSideAuthentication> authMethods = stateController.clientConfig().authMethods();
if(authId != null) {
o.put("authid", authId);
}
if (authMethods != null && authMethods.size() != 0) {
ArrayNode authMethodsNode = o.putArray("authmethods");
for(ClientSideAuthentication authMethod : authMethods) {
authMethodsNode.add(authMethod.getAuthMethod());
}
}

connectionController
.sendMessage(new WampMessages.HelloMessage(stateController.clientConfig().realm(), o), IWampConnectionPromise.Empty);
.sendMessage(new WampMessages.HelloMessage(stateController.clientConfig().realm(), stateController.clientConfig().helloDetails()), IWampConnectionPromise.Empty);
}

void onMessage(WampMessage msg) {
Expand Down

0 comments on commit 5a0394a

Please sign in to comment.