Skip to content

Commit c574dc4

Browse files
committed
#82 - socket support, with hook left for additional params for client emission
1 parent 1e46ec5 commit c574dc4

File tree

13 files changed

+379
-103
lines changed

13 files changed

+379
-103
lines changed

cougar-client/src/test/java/com/betfair/cougar/client/socket/ServerClientFactory.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
import com.betfair.cougar.core.api.exception.ServerFaultCode;
3434
import com.betfair.cougar.core.impl.security.CommonNameCertInfoExtractor;
3535
import com.betfair.cougar.core.impl.tracing.CompoundTracer;
36+
import com.betfair.cougar.netutil.nio.marshalling.DefaultExecutionContextResolverFactory;
37+
import com.betfair.cougar.transport.api.DehydratedExecutionContextResolution;
38+
import com.betfair.cougar.transport.api.RequestTimeResolver;
39+
import com.betfair.cougar.transport.impl.DehydratedExecutionContextResolutionImpl;
3640
import org.slf4j.Logger;
3741
import org.slf4j.LoggerFactory;
3842
import com.betfair.cougar.netutil.nio.CougarProtocol;
@@ -67,6 +71,8 @@
6771
import org.slf4j.Logger;
6872
import org.slf4j.LoggerFactory;
6973

74+
import static org.mockito.Mockito.mock;
75+
7076
/**
7177
* This class is used as a stub to facilitate NIO unit testing
7278
*/
@@ -98,10 +104,12 @@ public void execute(Runnable command) {
98104
};
99105

100106

101-
GeoIPLocator geo = Mockito.mock(GeoIPLocator.class);
102-
SocketRMIMarshaller marshaller = new SocketRMIMarshaller(geo, new CommonNameCertInfoExtractor(), new DefaultSocketTimeResolver(true));
107+
DehydratedExecutionContextResolutionImpl contextResolution = new DehydratedExecutionContextResolutionImpl();
108+
contextResolution.registerFactory(new DefaultExecutionContextResolverFactory(mock(GeoIPLocator.class),mock(RequestTimeResolver.class)));
109+
contextResolution.init(false);
110+
SocketRMIMarshaller marshaller = new SocketRMIMarshaller(new CommonNameCertInfoExtractor(), contextResolution);
103111
IdentityResolverFactory identityResolverFactory = new IdentityResolverFactory();
104-
identityResolverFactory.setIdentityResolver(Mockito.mock(IdentityResolver.class));
112+
identityResolverFactory.setIdentityResolver(mock(IdentityResolver.class));
105113

106114

107115

@@ -221,10 +229,10 @@ public static ExecutionVenueNioServer createServer(String host, int port, byte s
221229

222230

223231
public static ExecutionVenueNioClient createClient (String connectionString, NioConfig cfg) {
224-
GeoIPLocator geo = Mockito.mock(GeoIPLocator.class);
225-
SocketRMIMarshaller marshaller = new SocketRMIMarshaller(geo, new CommonNameCertInfoExtractor(), new DefaultSocketTimeResolver());
232+
DehydratedExecutionContextResolution contextResolution = mock(DehydratedExecutionContextResolution.class);
233+
SocketRMIMarshaller marshaller = new SocketRMIMarshaller(new CommonNameCertInfoExtractor(), contextResolution);
226234
IdentityResolverFactory factory = new IdentityResolverFactory();
227-
factory.setIdentityResolver(Mockito.mock(IdentityResolver.class));
235+
factory.setIdentityResolver(mock(IdentityResolver.class));
228236

229237
NioLogger logger = new NioLogger("ALL");
230238
ExecutionVenueNioClient client = new ExecutionVenueNioClient(logger, cfg, new HessianObjectIOFactory(true), new ClientConnectedObjectManager(), null, connectionString,

cougar-framework/cougar-transport-impl/src/main/java/com/betfair/cougar/transport/impl/DehydratedExecutionContextResolutionImpl.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public int getPriority() {
3535

3636
@Override
3737
public void onCougarStart() {
38+
init(true);
39+
}
40+
41+
public void init(boolean failIfNotComplete) {
3842
for (ProtocolParadigm paradigm : ProtocolParadigm.values()) {
3943
Set<Protocol> protocols = ProtocolRegistry.protocols(paradigm);
4044

@@ -55,11 +59,11 @@ public void onCougarStart() {
5559
}
5660
}
5761
// notify the resolver what they will be resolving
58-
resolver.resolving(handling);
62+
resolver.resolving(Collections.unmodifiableSet(handling));
5963
}
6064
}
6165
}
62-
//todo: #82: this needs to go back once all protocols have been built..
66+
//todo: #82: this needs to go back once all protocols have been built.. && failIfNotComplete
6367
// if (!remainingComponents.isEmpty()) {
6468
// throw new IllegalStateException("I have unhandled components: "+remainingComponents);
6569
// }

cougar-framework/jetty-transport/src/test/java/com/betfair/cougar/transport/impl/protocol/http/AbstractHttpContextResolutionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void init() {
6565

6666
contextResolution = new DehydratedExecutionContextResolutionImpl();
6767
contextResolution.registerFactory(resolverFactory);
68-
contextResolution.onCougarStart();
68+
contextResolution.init(false);
6969
}
7070

7171
protected abstract Protocol getProtocol();

cougar-framework/net-util/src/main/java/com/betfair/cougar/netutil/nio/marshalling/ArgumentMatcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013, The Sporting Exchange Limited
2+
* Copyright 2014, The Sporting Exchange Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2014, The Sporting Exchange Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.betfair.cougar.netutil.nio.marshalling;
18+
19+
import com.betfair.cougar.api.export.Protocol;
20+
import com.betfair.cougar.core.api.builder.DehydratedExecutionContextBuilder;
21+
import com.betfair.cougar.transport.api.DehydratedExecutionContextComponent;
22+
import com.betfair.cougar.transport.api.DehydratedExecutionContextResolver;
23+
import com.betfair.cougar.transport.api.DehydratedExecutionContextResolverFactory;
24+
import com.betfair.cougar.transport.api.RequestTimeResolver;
25+
import com.betfair.cougar.util.RequestUUIDImpl;
26+
import com.betfair.cougar.util.geolocation.GeoIPLocator;
27+
28+
import java.util.Date;
29+
import java.util.Set;
30+
31+
/**
32+
* Default resolver factory for the socket transport
33+
*/
34+
public class DefaultExecutionContextResolverFactory implements DehydratedExecutionContextResolverFactory {
35+
36+
private final GeoIPLocator geoIpLocator;
37+
private final RequestTimeResolver<Long> requestTimeResolver;
38+
39+
public DefaultExecutionContextResolverFactory(GeoIPLocator geoIpLocator, RequestTimeResolver<Long> requestTimeResolver) {
40+
this.geoIpLocator = geoIpLocator;
41+
this.requestTimeResolver = requestTimeResolver;
42+
}
43+
44+
@SuppressWarnings("unchecked")
45+
@Override
46+
public <T, B> DehydratedExecutionContextResolver<T, B>[] resolvers(Protocol protocol) {
47+
if (protocol == Protocol.SOCKET) {
48+
return new DehydratedExecutionContextResolver[] {
49+
new SocketResolver()
50+
};
51+
}
52+
return null;
53+
}
54+
55+
private class SocketResolver implements DehydratedExecutionContextResolver<SocketContextResolutionParams, Void> {
56+
private Set<DehydratedExecutionContextComponent> handling;
57+
58+
@Override
59+
public DehydratedExecutionContextComponent[] supportedComponents() {
60+
return new DehydratedExecutionContextComponent[] {
61+
DehydratedExecutionContextComponent.IdentityTokens,
62+
DehydratedExecutionContextComponent.Location,
63+
DehydratedExecutionContextComponent.ReceivedTime,
64+
DehydratedExecutionContextComponent.RequestedTime,
65+
DehydratedExecutionContextComponent.RequestUuid,
66+
DehydratedExecutionContextComponent.TraceLoggingEnabled,
67+
DehydratedExecutionContextComponent.TransportSecurityStrengthFactor
68+
};
69+
}
70+
71+
@Override
72+
public void resolving(Set<DehydratedExecutionContextComponent> handling) {
73+
this.handling = handling;
74+
}
75+
76+
@Override
77+
public void resolve(SocketContextResolutionParams params, Void ignore, DehydratedExecutionContextBuilder builder) {
78+
if (handling.contains(DehydratedExecutionContextComponent.IdentityTokens)) {
79+
builder.setIdentityTokens(params.getTokens());
80+
}
81+
if (handling.contains(DehydratedExecutionContextComponent.Location)) {
82+
GeoLocationParameters geoParams = params.getGeo();
83+
builder.setLocation(geoIpLocator.getGeoLocation(geoParams.getRemoteAddress(), geoParams.getAddressList(), geoParams.getInferredCountry()));
84+
}
85+
if (handling.contains(DehydratedExecutionContextComponent.ReceivedTime)) {
86+
builder.setReceivedTime(new Date());
87+
}
88+
if (handling.contains(DehydratedExecutionContextComponent.RequestedTime)) {
89+
builder.setRequestTime(requestTimeResolver.resolveRequestTime(params.getRequestTime()));
90+
}
91+
if (handling.contains(DehydratedExecutionContextComponent.TraceLoggingEnabled)) {
92+
builder.setTraceLoggingEnabled(params.isTraceEnabled());
93+
}
94+
if (handling.contains(DehydratedExecutionContextComponent.TransportSecurityStrengthFactor)) {
95+
builder.setTransportSecurityStrengthFactor(params.getTransportSecurityStrengthFactor());
96+
}
97+
if (handling.contains(DehydratedExecutionContextComponent.RequestUuid)) {
98+
if (params.getUuid() != null) {
99+
builder.setRequestUUID(new RequestUUIDImpl(params.getUuid()));
100+
}
101+
else {
102+
builder.setRequestUUID(new RequestUUIDImpl());
103+
}
104+
}
105+
106+
}
107+
}
108+
109+
@Override
110+
public String getName() {
111+
return "Default Socket ContextResolverFactory";
112+
}
113+
}

cougar-framework/net-util/src/main/java/com/betfair/cougar/netutil/nio/marshalling/DefaultSocketTimeResolver.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2014, The Sporting Exchange Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.betfair.cougar.netutil.nio.marshalling;
218

319
import com.betfair.cougar.transport.api.protocol.CougarObjectOutput;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2014, The Sporting Exchange Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.betfair.cougar.netutil.nio.marshalling;
18+
19+
import java.util.List;
20+
21+
/**
22+
*
23+
*/
24+
public class GeoLocationParameters {
25+
private final String remoteAddress;
26+
private final List<String> addressList;
27+
private final String inferredCountry;
28+
29+
public GeoLocationParameters(String remoteAddress, List<String> addressList, String inferredCountry) {
30+
this.remoteAddress = remoteAddress;
31+
this.addressList = addressList;
32+
this.inferredCountry = inferredCountry;
33+
}
34+
35+
public String getRemoteAddress() {
36+
return remoteAddress;
37+
}
38+
39+
public List<String> getAddressList() {
40+
return addressList;
41+
}
42+
43+
public String getInferredCountry() {
44+
return inferredCountry;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2014, The Sporting Exchange Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.betfair.cougar.netutil.nio.marshalling;
18+
19+
import com.betfair.cougar.api.security.IdentityToken;
20+
21+
import java.util.Date;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
/**
26+
* Parameters for resolving the execution context for the socket transport. Order of reading data from the transport is critical to maintain
27+
* backwards compatibility of the protocol, so all data is extracted by the marshaller and only then provided to the context resolvers.
28+
*/
29+
public class SocketContextResolutionParams {
30+
private final List<IdentityToken> tokens;
31+
private final String uuid;
32+
private final GeoLocationParameters geo;
33+
private final Date receivedTime;
34+
private final boolean traceEnabled;
35+
private final int transportSecurityStrengthFactor;
36+
private final Long requestTime;
37+
private final Map<String, String> additionalData;
38+
39+
public SocketContextResolutionParams(List<IdentityToken> tokens, String uuid, GeoLocationParameters geo, Date receivedTime, boolean traceEnabled, int transportSecurityStrengthFactor, Long requestTime, Map<String,String> additionalData) {
40+
this.tokens = tokens;
41+
this.uuid = uuid;
42+
this.geo = geo;
43+
this.receivedTime = receivedTime;
44+
this.traceEnabled = traceEnabled;
45+
this.transportSecurityStrengthFactor = transportSecurityStrengthFactor;
46+
this.requestTime = requestTime;
47+
this.additionalData = additionalData;
48+
}
49+
50+
public List<IdentityToken> getTokens() {
51+
return tokens;
52+
}
53+
54+
public String getUuid() {
55+
return uuid;
56+
}
57+
58+
public GeoLocationParameters getGeo() {
59+
return geo;
60+
}
61+
62+
public Date getReceivedTime() {
63+
return receivedTime;
64+
}
65+
66+
public boolean isTraceEnabled() {
67+
return traceEnabled;
68+
}
69+
70+
public int getTransportSecurityStrengthFactor() {
71+
return transportSecurityStrengthFactor;
72+
}
73+
74+
public Long getRequestTime() {
75+
return requestTime;
76+
}
77+
78+
public Map<String, String> getAdditionalData() {
79+
return additionalData;
80+
}
81+
}

0 commit comments

Comments
 (0)