comparison src/org/dancres/blitz/remote/transport/Server.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.remote.transport;
2
3 import java.net.InetSocketAddress;
4 import java.rmi.RemoteException;
5 import java.util.Iterator;
6
7 import net.jini.core.transaction.CannotJoinException;
8 import net.jini.core.transaction.UnknownTransactionException;
9 import net.jini.core.transaction.server.CrashCountException;
10 import net.jini.core.transaction.server.TransactionParticipant;
11
12 import org.dancres.blitz.SpaceImpl;
13 import org.dancres.blitz.txn.TxnGateway;
14 import org.dancres.blitz.txn.TxnId;
15 import org.apache.mina.common.IoAcceptor;
16 import org.apache.mina.common.ByteBuffer;
17 import org.apache.mina.common.SimpleByteBufferAllocator;
18 import org.apache.mina.filter.codec.ProtocolCodecFilter;
19 import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
20 import org.apache.mina.transport.socket.nio.SocketAcceptor;
21 import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
22 import org.apache.mina.transport.socket.nio.SocketSessionConfig;
23 import org.apache.mina.transport.socket.nio.SocketConnector;
24 import edu.emory.mathcs.backport.java.util.concurrent.Executors;
25
26 /**
27 */
28 public class Server {
29 private static final int SERVER_PORT = 8080;
30 // Set this to false to use object serialization instead of custom codec.
31 private static final boolean USE_CUSTOM_CODEC = true;
32
33 public static void main(String[] args) throws Throwable {
34 SpaceImpl mySpace = new SpaceImpl(new TxnGatewayImpl(null));
35
36 IoAcceptor acceptor = new SocketAcceptor();
37 // new SocketAcceptor(2, Executors.newFixedThreadPool(2));
38
39 // Prepare the service configuration.
40 SocketAcceptorConfig cfg = new SocketAcceptorConfig();
41 cfg.setReuseAddress(true);
42 cfg.getFilterChain().addLast(
43 "codec",
44 new ProtocolCodecFilter(new MessageCodecFactory()));
45
46 SocketSessionConfig mySConfig = (SocketSessionConfig)
47 cfg.getSessionConfig();
48 mySConfig.setTcpNoDelay(true);
49
50 // cfg.getFilterChain().addLast(
51 // "codec",
52 // new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
53
54 // cfg.getFilterChain().addLast("logger", new LoggingFilter());
55
56 acceptor.bind(
57 new InetSocketAddress(SERVER_PORT),
58 new ServerSessionHandler(mySpace), cfg);
59
60 System.out.println("Listening on port " + SERVER_PORT);
61 }
62
63 static class TxnGatewayImpl implements TxnGateway {
64 private long theCrashCount = System.currentTimeMillis();
65 private TransactionParticipant theParticipantStub;
66
67 TxnGatewayImpl(TransactionParticipant aStub) {
68 theParticipantStub = aStub;
69 }
70
71 public void join(TxnId anId)
72 throws UnknownTransactionException, CannotJoinException,
73 CrashCountException, RemoteException {
74
75 anId.getManager().join(anId.getId(), theParticipantStub,
76 theCrashCount);
77 }
78
79 public int getState(TxnId anId)
80 throws UnknownTransactionException, RemoteException {
81
82 return anId.getManager().getState(anId.getId());
83 }
84 }
85 }