org.bermuda.cartilage.core.Connection.java Source code

Java tutorial

Introduction

Here is the source code for org.bermuda.cartilage.core.Connection.java

Source

/**
 * Copyright (c) 2014 Samuel Hammersley - Gonsalves, Cartilage
 *
 * 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 org.bermuda.cartilage.core;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.bermuda.cartilage.Client;
import org.bermuda.cartilage.Credentials;
import org.bermuda.cartilage.music.MusicPlayer;
import org.bermuda.cartilage.ui.UserInterface;

import javax.annotation.Nonnull;
import java.nio.file.Paths;

/**
 * Creates a connection between the client and the server
 * @author Cartilage
 */
public class Connection {

    @Nonnull
    private ChannelFuture cf;

    /**
     * {@code MusicPlayer} instance
     */
    private MusicPlayer player = new MusicPlayer();

    public void connect() {
        Bootstrap bootstrap = new Bootstrap().group(new NioEventLoopGroup()).channel(NioSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new PacketEncoder(), new ConnectionController());
                    }
                });
        try {
            cf = bootstrap.connect("127.0.0.1", 8080).sync();
            cf.addListener(future -> {
                if (future.isSuccess()) {
                    System.out.println("Connection successful");
                }
            });
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    public void stop() {
        try {
            cf.channel().closeFuture().sync();
            System.out.println("Connection closed");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void processLogin(Client c) {
        try {
            Credentials.getInstance().writeDetails(c);
            c.setUI(UserInterface.CHAT_UI);
            player.readDirectory(Paths.get("C:\\Users\\AYUSHARYA\\Music"));
            player.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}