
com.github.autermann.sockets.server.RequestSocketServer Maven / Gradle / Ivy
/*
* Copyright 2013 Christian Autermann
*
* 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 com.github.autermann.sockets.server;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.Executor;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
/**
* TODO JavaDoc
*
* @author Christian Autermann
*/
public class RequestSocketServer extends StreamingSocketServer {
RequestSocketServer(ServerSocketFactory serverSocketFactory,
Supplier> coderFactory,
Supplier> handlerFactory,
Executor executor, List shutdownHooks,
int port) {
super(serverSocketFactory, createStreamingHandlerFactory(coderFactory, handlerFactory),
executor, shutdownHooks, port);
}
private static Supplier createStreamingHandlerFactory(
Supplier> coderFactory,
Supplier> handlerFactory) {
return Suppliers.ofInstance(
new HandlerImpl(coderFactory, handlerFactory));
}
private static class HandlerImpl implements
StreamingSocketServerHandler {
private final Supplier> coderFactory;
private final Supplier> handlerFactory;
HandlerImpl(Supplier> coderFactory,
Supplier> handlerFactory) {
this.coderFactory = checkNotNull(coderFactory);
this.handlerFactory = checkNotNull(handlerFactory);
}
@Override
public void handle(InputStream in, OutputStream out)
throws IOException {
RequestSocketServerCoder coder = coderFactory.get();
RequestSocketServerHandler handler = handlerFactory.get();
I request = coder.decode(in);
O response = handler.handle(request);
coder.encode(response, out);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy