Permalink
Browse files

Remove the Mina support, we don't need it anymore.

  • Loading branch information...
1 parent 2ce00fd commit 88f6a2c2bdfa0c9825b989705a631aadb5dcd5d9 @green-coder green-coder committed Mar 26, 2012
View
@@ -41,16 +41,6 @@
</dependency>
<dependency>
- <groupId>org.apache.mina</groupId>
- <artifactId>mina-core</artifactId>
- </dependency>
-
- <!--dependency>
- <groupId>org.apache.mina</groupId>
- <artifactId>mina-example</artifactId>
- </dependency-->
-
- <dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</dependency>
@@ -1,48 +0,0 @@
-package com.lemoulinstudio.forest.platform.mina;
-
-import javax.crypto.Cipher;
-import org.apache.mina.core.buffer.IoBuffer;
-import org.apache.mina.core.filterchain.IoFilter.NextFilter;
-import org.apache.mina.core.session.AttributeKey;
-import org.apache.mina.core.session.IoSession;
-import org.apache.mina.core.write.WriteRequest;
-import org.apache.mina.filter.util.WriteRequestFilter;
-
-public class CipherFilter extends WriteRequestFilter {
-
- public static final AttributeKey ENCRYPTION_CIPHER =
- new AttributeKey(CipherFilter.class, "encryptionCipher");
-
- public static final AttributeKey DECRYPTION_CIPHER =
- new AttributeKey(CipherFilter.class, "decryptionCipher");
-
- private static final CipherFilter instance = new CipherFilter();
-
- public static CipherFilter getInstance() {
- return instance;
- }
-
- private CipherFilter() {
- }
-
- @Override
- public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
- IoBuffer cipherTextBuffer = (IoBuffer) message;
-
- Cipher decryptionCipher = (Cipher) session.getAttribute(DECRYPTION_CIPHER);
- byte[] plainText = decryptionCipher.doFinal(cipherTextBuffer.array(), 0, cipherTextBuffer.limit());
-
- nextFilter.messageReceived(session, plainText);
- }
-
- @Override
- protected Object doFilterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
- byte[] plainText = (byte[]) writeRequest.getMessage();
-
- Cipher encryptionCipher = (Cipher) session.getAttribute(ENCRYPTION_CIPHER);
- IoBuffer cipherTextBuffer = IoBuffer.wrap(encryptionCipher.doFinal(plainText));
-
- return cipherTextBuffer;
- }
-
-}
@@ -1,27 +0,0 @@
-package com.lemoulinstudio.forest.platform.mina;
-
-import com.lemoulinstudio.forest.platform.user.Contact;
-import com.lemoulinstudio.forest.platform.user.User;
-import java.net.InetSocketAddress;
-import org.apache.mina.core.future.ConnectFuture;
-import org.apache.mina.core.service.IoHandler;
-import org.apache.mina.core.session.IoSession;
-import org.apache.mina.transport.socket.nio.NioSocketConnector;
-
-public class Client {
-
- public IoSession connect(User user, Contact contact) {
- return connect(user, contact, ForestIoHandler.getInstance());
- }
-
- public IoSession connect(User user, Contact contact, IoHandler ioHandler) {
- NioSocketConnector connector = new NioSocketConnector();
- connector.getFilterChain().addLast("handshake", new ClientHandshakeFilter(user, contact));
- connector.setHandler(ForestIoHandler.getInstance());
-
- ConnectFuture connectFuture = connector.connect(new InetSocketAddress(contact.getInternetAddress(), contact.getPort()));
- connectFuture.awaitUninterruptibly();
- return connectFuture.getSession();
- }
-
-}
@@ -1,97 +0,0 @@
-package com.lemoulinstudio.forest.platform.mina;
-
-import com.lemoulinstudio.forest.platform.handshake.ClientSecureConnectionHandler;
-import com.lemoulinstudio.forest.platform.user.Contact;
-import com.lemoulinstudio.forest.platform.user.User;
-import org.apache.mina.core.buffer.IoBuffer;
-import org.apache.mina.core.filterchain.IoFilterAdapter;
-import org.apache.mina.core.session.AttributeKey;
-import org.apache.mina.core.session.IdleStatus;
-import org.apache.mina.core.session.IoSession;
-
-public class ClientHandshakeFilter extends IoFilterAdapter {
-
- private final AttributeKey HANDSHAKE_HANDLER = new AttributeKey(ClientHandshakeFilter.class, "handshakeHandler");
-
- private final User user;
- private final Contact contact;
-
- public ClientHandshakeFilter(User user, Contact contact) {
- this.user = user;
- this.contact = contact;
- }
-
- @Override
- public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
- // Don't forward the event to the next filter.
- }
-
- @Override
- public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
- // Creates an handshake handler and attach it to the session.
- ClientSecureConnectionHandler handshakeHandler = new ClientSecureConnectionHandler(user, contact);
- session.setAttribute(HANDSHAKE_HANDLER, handshakeHandler);
-
- // Create and send the connection request.
- byte[] connectionRequest = handshakeHandler.createConnectionRequest();
- session.write(IoBuffer.wrap(connectionRequest));
-
- // Don't forward the event to the next filter.
- }
-
- @Override
- public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
- // Don't forward the event to the next filter.
- }
-
- @Override
- public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
- // Don't forward the event to the next filter.
- }
-
- @Override
- public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
- ClientSecureConnectionHandler handshakeHandler = (ClientSecureConnectionHandler) session.getAttribute(HANDSHAKE_HANDLER);
-
- IoBuffer connectionResponse = (IoBuffer) message;
- byte[] responseData = new byte[connectionResponse.limit()];
- connectionResponse.get(responseData);
- handshakeHandler.handleConnectionResponse(responseData);
-
- // We detach the handshake handler from the session.
- session.removeAttribute(HANDSHAKE_HANDLER);
-
- // We attach the encryption and decryption ciphers to the session.
- session.setAttribute(CipherFilter.ENCRYPTION_CIPHER, handshakeHandler.getEncryptionCipher());
- session.setAttribute(CipherFilter.DECRYPTION_CIPHER, handshakeHandler.getDecryptionCipher());
-
- // We attach some information about the connection.
- session.setAttribute(ForestIoHandler.INITIATED_BY_ME, Boolean.TRUE);
- session.setAttribute(ForestIoHandler.USER, user);
- session.setAttribute(ForestIoHandler.CONTACT, contact);
-
- // We create the cipher filter and add it to the filter chain.
- session.getFilterChain().getEntry(this).addAfter("cipher", CipherFilter.getInstance());
-
- // We create the codec filter and add it to the filter chain.
- session.getFilterChain().getEntry("cipher").addAfter("codec", PacketFilter.getInstance());
-
- // We remove this filter from the filter chain.
- session.getFilterChain().remove(this);
-
- // We notify the next filter about a created session.
- nextFilter.sessionCreated(session);
- nextFilter.sessionOpened(session);
-
- // Don't forward the event to the next filter.
- }
-
- @Override
- public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
- cause.printStackTrace();
- session.close(true);
-
- // Don't forward the event to the next filter.
- }
-
-}
@@ -1,136 +0,0 @@
-package com.lemoulinstudio.forest.platform.mina;
-
-import com.lemoulinstudio.forest.platform.crypto.CryptoUtil;
-import com.lemoulinstudio.forest.platform.service.Configuration;
-import com.lemoulinstudio.forest.platform.service.NodeService;
-import com.lemoulinstudio.forest.platform.service.NodeServiceImpl;
-import com.lemoulinstudio.forest.platform.user.Contact;
-import com.lemoulinstudio.forest.platform.user.User;
-import com.lemoulinstudio.small.AbstractConfiguration;
-import com.lemoulinstudio.small.MessageSender;
-import com.lemoulinstudio.small.SmallSession;
-import com.lemoulinstudio.small.SmallSessionImpl;
-import java.net.InetSocketAddress;
-import org.apache.mina.core.service.IoHandlerAdapter;
-import org.apache.mina.core.session.AttributeKey;
-import org.apache.mina.core.session.IoSession;
-
-public final class ForestIoHandler extends IoHandlerAdapter {
-
- private static final ForestIoHandler instance = new ForestIoHandler();
-
- public static ForestIoHandler getInstance() {
- return instance;
- }
-
- private static final AbstractConfiguration configuration = new Configuration();
-
- public static final AttributeKey INITIATED_BY_ME = new AttributeKey(ForestIoHandler.class, "initiatedByMe");
- public static final AttributeKey USER = new AttributeKey(ForestIoHandler.class, "user");
- public static final AttributeKey CONTACT = new AttributeKey(ForestIoHandler.class, "contact");
-
- private static final AttributeKey SMALL_SESSION = new AttributeKey(ForestIoHandler.class, "smallSession");
-
- public static SmallSession getSmallSession(IoSession session) {
- return (SmallSession) session.getAttribute(SMALL_SESSION);
- }
-
- private ForestIoHandler() {
- }
-
- @Override
- public void sessionCreated(final IoSession session) throws Exception {
- // This connection was initiated by me? (i.e. Did I connected this contact?)
- boolean initiatedByMe = (Boolean) session.getAttribute(INITIATED_BY_ME);
- session.removeAttribute(INITIATED_BY_ME);
-
- // Identify the current user.
- User user = (User) session.getAttribute(USER);
- //session.removeAttribute(USER);
-
- // Identify the caller.
- Contact contact = (Contact) session.getAttribute(CONTACT);
- //session.removeAttribute(CONTACT);
-
- // We handle the case where a new connection is made while an existing one is already there.
- if (contact.getIoSession() != null) {
- // We already have a connection, we need to cut one of them.
- // We choose to cut the connection initiated by the peer with the smallest public key.
- boolean myPublicKeyIsSmaller = CryptoUtil.isSmallerThan(user.getKeyPair().getPublic(), contact.getPublicKey());
-
- boolean contactIpStillTheSame = ((InetSocketAddress) session.getRemoteAddress())
- .getAddress().equals(contact.getInternetAddress());
-
- if (contactIpStillTheSame && (myPublicKeyIsSmaller == initiatedByMe)) {
- // We close the new connection.
- session.close(true);
- return;
- }
- else {
- // We close the existing connection.
- contact.getIoSession().close(true);
- }
- }
-
- // Create the Small session.
- SmallSession smallSession = new SmallSessionImpl(configuration);
- session.setAttribute(SMALL_SESSION, smallSession);
-
- smallSession.setCallerObject(contact);
-
- // Configure the message sender.
- smallSession.setMessageSender(new MessageSender() {
- @Override
- public void sendMessage(byte[] binaryMessage) {
- session.write(binaryMessage);
- }
- });
-
- // Define our local service.
- smallSession.bind(new NodeServiceImpl(contact, session), NodeService.class);
-
- // Create a proxy to the remove service.
- NodeService nodeServiceProxy = smallSession.createProxy(NodeService.class);
-
- // Attach the session to the contact object.
- synchronized(contact) {
- contact.setIoSession(session);
- contact.setNodeServiceProxy(nodeServiceProxy);
- }
- }
-
- @Override
- public void sessionOpened(IoSession session) throws Exception {
- Contact contact = (Contact) session.getAttribute(CONTACT);
-
- contact.getNodeServiceProxy().pingToTarget("helloWorldTarget".getBytes());
- }
-
- @Override
- public void sessionClosed(IoSession session) throws Exception {
- Contact contact = (Contact) getSmallSession(session).getCallerObject();
-
- synchronized(contact) {
- if (contact.getIoSession() == session) {
- contact.setIoSession(null);
- contact.setNodeServiceProxy(null);
- }
- }
-
- session.removeAttribute(USER);
- session.removeAttribute(CONTACT);
- session.removeAttribute(SMALL_SESSION);
- }
-
- @Override
- public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
- cause.printStackTrace();
- session.close(false);
- }
-
- @Override
- public void messageReceived(IoSession session, Object message) throws Exception {
- getSmallSession(session).decodeAndExecute((byte[]) message);
- }
-
-}
Oops, something went wrong.

0 comments on commit 88f6a2c

Please sign in to comment.