Mina 应用之——代理服务器


/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 a.proxy.test;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;


public class ClientToProxyIoHandler extends AbstractProxyIoHandler {
private final ServerToProxyIoHandler connectorHandler = new ServerToProxyIoHandler();

private final IoConnector connector;

private final SocketAddress[] remoteAddress;

public ClientToProxyIoHandler(IoConnector connector,
SocketAddress[] remoteAddress) {
this.connector = connector;
this.remoteAddress = remoteAddress;
connector.setHandler(connectorHandler);
}
InetSocketAddress isas[]=new InetSocketAddress[]{
new InetSocketAddress("113.31.18.232", 22),
new InetSocketAddress("113.31.18.233", 22),
new InetSocketAddress("113.31.18.234", 22),
new InetSocketAddress("113.31.18.235", 22),
new InetSocketAddress("113.31.18.236", 22),


};
int k=0;
@Override
public void sessionOpened(final IoSession session) throws Exception {


//isa[(k++)%isa.length]
SocketAddress isa= remoteAddress[(k++)%remoteAddress.length];
System.out.println("#$#$"+isa);

connector.connect(isa).addListener(new IoFutureListener<ConnectFuture>() {
public void operationComplete(ConnectFuture future) {

try {
future.getSession().setAttribute(OTHER_IO_SESSION, session);
session.setAttribute(OTHER_IO_SESSION, future.getSession());
IoSession session2 = future.getSession();
session2.resumeRead();
session2.resumeWrite();
} catch (RuntimeIoException e) {
// Connect failed
session.close(true);
} finally {
session.resumeRead();
session.resumeWrite();

}
}
});
}
}





/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 a.proxy.test;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;


public abstract class AbstractProxyIoHandler extends IoHandlerAdapter {
private static final Charset CHARSET = Charset.forName("iso8859-1");
public static final String OTHER_IO_SESSION = AbstractProxyIoHandler.class.getName()+".OtherIoSession";


@Override
public void sessionCreated(IoSession session) throws Exception {
session.suspendRead();
session.suspendWrite();
}

@Override
public void sessionClosed(IoSession session) throws Exception {
if (session.getAttribute( OTHER_IO_SESSION ) != null) {
IoSession sess = (IoSession) session.getAttribute(OTHER_IO_SESSION);
sess.setAttribute(OTHER_IO_SESSION, null);
sess.close(false);
session.setAttribute(OTHER_IO_SESSION, null);
}
}

@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
IoBuffer rb = (IoBuffer) message;
IoBuffer wb = IoBuffer.allocate(rb.remaining());
rb.mark();
wb.put(rb);
wb.flip();
((IoSession) session.getAttribute(OTHER_IO_SESSION)).write(wb);
rb.reset();

}
}




/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 a.proxy.test;

import java.net.InetSocketAddress;

import org.apache.mina.core.service.IoConnector;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class Main {
public static void main(String[] args2) throws Exception {
// Create TCP/IP acceptor.
NioSocketAcceptor acceptor = new NioSocketAcceptor();
IoConnector connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(30*1000L);
ClientToProxyIoHandler handler = new ClientToProxyIoHandler(connector, new InetSocketAddress[]{ new InetSocketAddress("127.0.0.1", 80)});
acceptor.setHandler(handler);
acceptor.bind(new InetSocketAddress(777));
System.out.println("Listening on port " + 777);
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧农业物联网平台-后端 智慧农业物联网平台-MINA代理端 终端通讯协议设计 数据包格式 名称 偏移位置 长度 值范围 包头 0 1 固定字符‘*’ 流水号 1 4 0 类型 5 1 0-127 数据长度 6 2 数据字节数 数据 8 n 协议类型决定 校验码 n+8 2 类型、数据长度、数据三部分的所有字节的CRC-16码 注 1:其中字符''为转意符,所有除头尾外的字节,如果是'*','#',''在通讯时换成"*","#","\" 注 2:凡涉及多字节数据均为低字节在前 心跳包格式 包名称 心跳包 发送方 服务端或客户端 接收方 客户端或服务端 类型码 00H 数据长度 0字节 数据 无 说明 客户端和服务端之间无通讯每隔两分钟互发该数据包,通知对方连接正常 注册包格式 包名称 注册包 发送方 服客户端 接收方 服务端 类型码 01H 数据长度 13字节 数据 偏移位置 大小 说明 0 1 设备型号 1 12 设备ID 说明 客户端与服务端建立连接以后,发送本包内容,在服务端未收到服务端回应包以前,不发送其他数据包,间隔10秒发送本包 注册包应答格式 包名称 注册包 发送方 服务端 接收方 客户端 类型码 F1H 数据长度 13字节 数据 偏移位置 大小 说明 0 1 0-成功; 1-失败 1 12 回应token码 说明 当接受到客户端注册包以后,服务端代理与后端交互,将得到的注册结果发回给客户端。如果注册结果为失败,发送以后,主动断开网络连接。 温度数据格式 包名称 湿度包 发送方 客户端 接收方 服务端 类型码 05H 数据长度 2字节 数据 偏移位置 大小 说明 0 1 整数部分 1 1 小数部分 说明 温度数据包 温度数据格式 包名称 温度包 发送方 客户端 接收方 服务端 类型码 06H 数据长度 2字节 数据 偏移位置 大小 说明 0 1 整数部分 1 1 小数部分 说明 温度数据包

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值