Skip to content

Instantly share code, notes, and snippets.

@kaspernielsen
Created March 11, 2013 12:09
Show Gist options
  • Save kaspernielsen/5133820 to your computer and use it in GitHub Desktop.
Save kaspernielsen/5133820 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2008 Kasper Nielsen.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dk.dma.enav.communication.service;
import static java.util.Objects.requireNonNull;
import dk.dma.enav.communication.service.spi.MaritimeServiceMessage;
import dk.dma.enav.model.voyage.Route;
/**
*
* @author Kasper Nielsen
*/
public class RouteSuggestionService {
/** An initiation point */
public static final ServiceInitiationPoint<RouteSuggestionMessage> INIT = new ServiceInitiationPoint<>(
RouteSuggestionMessage.class);
public static class RouteSuggestionMessage extends MaritimeServiceMessage<RouteSuggestionAck> {
private Route route;
public RouteSuggestionMessage() {}
public RouteSuggestionMessage(Route route) {
this.route = requireNonNull(route);
}
/**
* @return the route
*/
public Route getRoute() {
return route;
}
/**
* @param route
* the route to set
*/
public void setRoute(Route route) {
this.route = route;
}
}
public static class RouteSuggestionAck extends MaritimeServiceMessage<Void> {
private String message;
public RouteSuggestionAck() {}
/**
* @param message
*/
public RouteSuggestionAck(String message) {
this.message = message;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
}
}
/*
* Copyright (c) 2008 Kasper Nielsen.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dk.dma.enav.communication.service;
import static org.junit.Assert.assertEquals;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import test.AbstractNetworkTest;
import dk.dma.enav.communication.MaritimeNetworkConnection;
import dk.dma.enav.communication.NetworkFuture;
import dk.dma.enav.communication.service.RouteSuggestionService.RouteSuggestionAck;
import dk.dma.enav.communication.service.RouteSuggestionService.RouteSuggestionMessage;
import dk.dma.enav.model.MaritimeId;
import dk.dma.enav.model.voyage.Route;
import dk.dma.enav.model.voyage.Waypoint;
/**
*
* @author Kasper Nielsen
*/
public class SkibsTest extends AbstractNetworkTest {
public static final MaritimeId SKIB = MaritimeId.create("mmsi://1");
public static final MaritimeId SHORE = MaritimeId.create("mmsi://6");
@Test
public void oneClient() throws Exception {
MaritimeNetworkConnection c1 = newClient(SKIB);
c1.serviceRegister(
RouteSuggestionService.INIT,
new InvocationCallback<RouteSuggestionService.RouteSuggestionMessage, RouteSuggestionService.RouteSuggestionAck>() {
public void process(RouteSuggestionMessage message,
InvocationCallback.Context<RouteSuggestionAck> context) {
System.out.println(message.getRoute());
context.complete(new RouteSuggestionAck("Det lyder helt fint"));
}
}).awaitRegistered(4, TimeUnit.SECONDS);
MaritimeNetworkConnection c2 = newClient(SHORE);
ServiceEndpoint<RouteSuggestionMessage, RouteSuggestionAck> end = c2
.serviceFindOne(RouteSuggestionService.INIT).get(6, TimeUnit.SECONDS);
assertEquals(SKIB, end.getId());
Route r = new Route();
r.getWaypoints().add(new Waypoint().setLatitude(12).setLongitude(16).setEta(new Date()));
NetworkFuture<RouteSuggestionAck> f = end.invoke(new RouteSuggestionService.RouteSuggestionMessage(r));
assertEquals("Det lyder helt fint", f.get(4, TimeUnit.SECONDS).getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment