controlspy3.SpyAsServer.java Source code

Java tutorial

Introduction

Here is the source code for controlspy3.SpyAsServer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controlspy3;

/**
 *
 * @author R&D Engineer Junior
 */
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.rtsp.RtspDecoder;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

/**
 * Handles a server-side channel.
 */
public class SpyAsServer extends ChannelInboundHandlerAdapter { // (1)
    public static UnixTime1 ux1;
    private static ArrayList channels = new ArrayList();

    public static void channelSend() {
        System.out.println("\n\nEnvio algo al MDVR");
        for (int i = 0; i < channels.size(); i++) {
            ChannelHandlerContext ctx = (ChannelHandlerContext) channels.get(i);
            ctx.writeAndFlush(ux1);
        }

        //        System.out.print("\nMensaje al Servidor: ");
        //        for(int i=0; i<ux1.value().length; i++){        
        //        System.out.print((char) ux1.value()[i]);
        //        }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("\nrecibio algo Servidor");
        ByteBuf in = (ByteBuf) msg;
        ArrayList in2 = new ArrayList();
        String messageServer = "";
        try {
            while (in.isReadable()) { // (1)
                byte rxByte = in.readByte();
                in2.add(rxByte);
            }
            ux1 = new UnixTime1(in2);
            System.out.println("A");
            SpyAsServer.channelSend();

            messageServer = "";
            for (int i = 0; i < in2.size(); i++) {
                byte rxByte = (byte) in2.get(i);
                messageServer += String.valueOf(Character.toChars(rxByte));
            }

        } catch (Exception e) {
            System.out.println("Entro al ERROR RARO");
        } finally {
            in.release(); // (2)
        }
    }

    @Override
    public void channelActive(final ChannelHandlerContext ctx) throws Exception { // (1)
        System.out.println("\nchannelActive");

        channels.add(ctx);

        ux1 = new UnixTime1();
    }

    @Override
    public void channelInactive(final ChannelHandlerContext ctx) throws Exception { // (1)
        System.out.println("me sali");
        ControlSpy3.ALGO = false;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }

}