본문으로 바로가기

ClientIFTest

category 자바 2017. 7. 21. 13:38

import java.util.List;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import com.google.common.collect.Lists;

import com.google.protobuf.ByteString;


import io.netty.bootstrap.Bootstrap;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelOption;

import io.netty.channel.ChannelPipeline;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.nio.NioSocketChannel;

import io.netty.handler.codec.bytes.ByteArrayDecoder;

import io.netty.handler.codec.bytes.ByteArrayEncoder;

import io.netty.handler.codec.protobuf.ProtobufDecoder;

import io.netty.handler.codec.protobuf.ProtobufEncoder;

import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;

import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

import io.netty.handler.logging.LogLevel;

import io.netty.handler.logging.LoggingHandler;


public class ClientIFTest

{

    private static final Logger logger = LoggerFactory.getLogger(ClientIFTest.class);


    private EventLoopGroup  workerGroup = null;


    private String ip = "";

    private int port = -1;

    

   


    private ClientIFTestHandler clientIFTestHandler;


    /**

     * 

     * <pre>

     * 0 : 아이피 1 : PORT 2 : Type 3 이상 : 삭제 태그 아이디 리스트.

     * 

     * <pre>

     * 

     * @param args

     */

    public static void main(final String[] args)

    {

   

        final ClientIFTest clientIFTest = new ClientIFTest(args);

        try

        {

clientIFTest.connect(); //초기 접속 

    ByteBuf finsNodeAddressData  = Unpooled.buffer(34);

        

    // 46494e53

        finsNodeAddressData.writeByte(0x46);

        finsNodeAddressData.writeByte(0x49);

        finsNodeAddressData.writeByte(0x4e);

        finsNodeAddressData.writeByte(0x53);

        

        // 0000000c

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x1a);//0c

        

        // 00000000

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x02);

       

        // 00000000

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        

        // 00000000

        finsNodeAddressData.writeByte(0x80);//

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x02);

        finsNodeAddressData.writeByte(0x02);//

        

        // 00000000

        finsNodeAddressData.writeByte(0xb0);//PLC node 

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x02);

        finsNodeAddressData.writeByte(0xef);//PC node

        

        // 00000000

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x01);

        finsNodeAddressData.writeByte(0x01);

        

        // 00000000

        finsNodeAddressData.writeByte(0x82);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        

        // 00000000

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x01);

       

        sendObject(finsNodeAddressData.array());

       

        final ChannelHandlerContext channelHandlerContext = this.clientIFTestHandler

                    .getChannelHandlerContext();


            if (channelHandlerContext != null)

            {

                channelHandlerContext.writeAndFlush(dataByf);

            } else

            {

                // 연결이 되지 않았을 경우.. 로그 에러 남기기.


            }


            // clientIFTest.connect();

        } catch (final Exception e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }


    }


    /**

     * 

     * <pre>

     * 생성자.

     * 

     * <pre>

     * 

     * @param args

     */

    ClientIFTest(final String[] args)

    {

        this.ip = "192.168.123.176";

        this.port = 9600;

    }


    /**

     * <pre>

     * HOM WEB 연결

     * 

     * <pre>

     */

    public void connect() throws Exception

    {


        this.workerGroup = new NioEventLoopGroup();


        final Bootstrap bootstrap = new Bootstrap();

        bootstrap.group(this.workerGroup); // IO 담당 스레드 그룹

        bootstrap.channel(NioSocketChannel.class); // 모드설정( IO, NIO)

        bootstrap.handler(new LoggingHandler(LogLevel.DEBUG)); // IO에서 발생하는 로그

        bootstrap.handler(new ChannelInitializer<io.netty.channel.socket.SocketChannel>()

        {

            @Override

            public void initChannel(final io.netty.channel.socket.SocketChannel socketChannel)

                    throws Exception

            {

                final ChannelPipeline channelPipeline = socketChannel.pipeline();


                channelPipeline.addLast("bytearray-encoder", new ByteArrayEncoder());


                channelPipeline.addLast("bytearray-decoder", new ByteArrayDecoder());


                // 핸들러 추가

                ClientIFTest.this.clientIFTestHandler = new ClientIFTestHandler();

                channelPipeline.addLast(ClientIFTest.this.clientIFTestHandler);

            }

        });


        // 소켓 채널에 소켓 옵션을 설정

        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);

        bootstrap.option(ChannelOption.TCP_NODELAY, true);// Nagle 알고리즘

                                                          // 사용.(false-비활성화)


        /** IP, Port bind */

        final ChannelFuture channelFuture = bootstrap.connect(this.ip, this.port).sync();

        

        

        ByteBuf finsNodeAddressData  = Unpooled.buffer(20);

        

        

        // 46494e53

        finsNodeAddressData.writeByte(0x46);

        finsNodeAddressData.writeByte(0x49);

        finsNodeAddressData.writeByte(0x4e);

        finsNodeAddressData.writeByte(0x53);

        

        // 0000000c

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x0c);//0c

        

        // 00000000

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

       

        // 00000000

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);


        // 00000000

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        finsNodeAddressData.writeByte(0x00);

        

        System.out.println("Send Data : ");

        System.out.println(ByteUtils.byteArrayToHex(finsNodeAddressData.array()));

        

        

        sendObject(finsNodeAddressData.array());

 

        // Wait until the connection is closed.

        channelFuture.channel().closeFuture().sync();

    }


    


    /**

     * 

     * <pre>

     * HOM Web 으로 데이터 전송.

     * 

     * <pre>

     * 

     * @param dataObject

     */

    public void sendObject(final byte[] dataByf)

    {


        final ChannelHandlerContext channelHandlerContext = this.clientIFTestHandler

                .getChannelHandlerContext();


        if (channelHandlerContext != null)

        {

            channelHandlerContext.writeAndFlush(dataByf);

        } else

        {

            // 연결이 되지 않았을 경우.. 로그 에러 남기기.


        }

    }


    /**

     * <pre>

     * HOM WEB 연결 해제

     * 

     * <pre>

     */

    @SuppressWarnings("restriction")

    public void disConnect()

    {

        if (this.workerGroup != null)

        {

            this.workerGroup.shutdownGracefully();

        }

    }

}



'자바' 카테고리의 다른 글

[프로그래머스] 신고 결과 받기 for JAVA  (0) 2022.02.01
[자바] tomcat 에서 보이는 memory leak 해결 방법  (0) 2021.02.01
ClientIFTestHandler  (0) 2017.07.21
Generic  (0) 2017.06.22
BufferedReader VS scanner  (0) 2017.06.21