diff --git a/platform-operate-api/src/main/java/com/lyms/hospitalapi/NIOServer.java b/platform-operate-api/src/main/java/com/lyms/hospitalapi/NIOServer.java new file mode 100644 index 0000000..265cb86 --- /dev/null +++ b/platform-operate-api/src/main/java/com/lyms/hospitalapi/NIOServer.java @@ -0,0 +1,163 @@ +package com.lyms.hospitalapi; + +import com.lyms.platform.common.utils.PropertiesUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.*; +import java.util.Iterator; + +/** + * Created by riecard on 2016/11/17. + */ +@Service +public class NIOServer { + + public static final String NIO_SERVER_PORT = PropertiesUtils.getPropertyValue("nio_server_port"); + public static final String NIO_SERVER_START = PropertiesUtils.getPropertyValue("nio_server_start"); + + //通道管理器 + private Selector selector; + + @PostConstruct + public void init() throws IOException { + if ("1".equals(NIO_SERVER_START)) { + NIOServer server = new NIOServer(); + server.initServer(Integer.valueOf(NIO_SERVER_PORT)); + server.listen(); + } + } + + /** + * 获得一个ServerSocket通道,并对该通道做一些初始化的工作 + * @param port 绑定的端口号 + * @throws IOException + */ + public void initServer(int port) throws IOException { + // 获得一个ServerSocket通道 + ServerSocketChannel serverChannel = ServerSocketChannel.open(); + // 设置通道为非阻塞 + serverChannel.configureBlocking(false); + // 将该通道对应的ServerSocket绑定到port端口 + serverChannel.socket().bind(new InetSocketAddress(port)); + // 获得一个通道管理器 + this.selector = Selector.open(); + //将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后, + //当该事件到达时,selector.select()会返回,如果该事件没到达selector.select()会一直阻塞。 + serverChannel.register(selector, SelectionKey.OP_ACCEPT); + new BeatTask(selector).start(); + } + + /** + * 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理 + * @throws IOException + */ + @SuppressWarnings("unchecked") + public void listen() throws IOException { + System.out.println("服务端启动成功!"); + // 轮询访问selector + while (true) { + //当注册的事件到达时,方法返回;否则,该方法会一直阻塞 + selector.select(); + // 获得selector中选中的项的迭代器,选中的项为注册的事件 + Iterator ite = this.selector.selectedKeys().iterator(); + while (ite.hasNext()) { + SelectionKey key = (SelectionKey) ite.next(); + // 删除已选的key,以防重复处理 + ite.remove(); + // 客户端请求连接事件 + if (key.isAcceptable()) { + ServerSocketChannel server = (ServerSocketChannel) key + .channel(); + // 获得和客户端连接的通道 + SocketChannel channel = server.accept(); + // 设置成非阻塞 + channel.configureBlocking(false); + + //在这里可以给客户端发送信息哦 + channel.write(ByteBuffer.wrap(new String("向客户端发送了一条信息").getBytes())); + //在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。 + channel.register(this.selector, SelectionKey.OP_READ); + + // 获得了可读的事件 + } else if (key.isReadable()) { + int length = read(key); + if (length == 0) { + key.channel().close(); + } + } + + } + + } + } + /** + * 处理读取客户端发来的信息 的事件 + * @param key + * @throws IOException + */ + public int read(SelectionKey key) throws IOException{ + // 服务器可读取消息:得到事件发生的Socket通道 + SocketChannel channel = (SocketChannel) key.channel(); + // 创建读取的缓冲区 + ByteBuffer buffer = ByteBuffer.allocate(10); + channel.read(buffer); + byte[] data = buffer.array(); + String msg = new String(data).trim(); + if (msg.length() == 0) { + return 0; + } + System.out.println("服务端收到信息:"+msg); + ByteBuffer outBuffer = ByteBuffer.wrap("success".getBytes()); + channel.write(outBuffer);// 将消息回送给客户端 + return msg.length(); + } + + class BeatTask extends Thread { + + private Selector selector; + + public BeatTask(Selector selector) { + this.selector = selector; + } + + @Override + public void run() { + while (true) { + for (SelectionKey key:selector.keys()) { + System.out.println(key); + Channel channel = key.channel(); + if (channel instanceof SocketChannel) { + ByteBuffer outBuffer = ByteBuffer.wrap("heartbeat".getBytes()); + try { + ((SocketChannel)channel).write(outBuffer);// 将消息回送给客户端 + } catch (IOException e) { + e.printStackTrace(); + } + } + } + try { + sleep(3000L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + /** + * 启动服务端测试 + * @throws IOException + */ + public static void main(String[] args) throws IOException { + NIOServer server = new NIOServer(); + server.initServer(8000); + server.listen(); + } + + + +} \ No newline at end of file diff --git a/platform-operate-api/src/main/java/com/lyms/hospitalapi/NioClient.java b/platform-operate-api/src/main/java/com/lyms/hospitalapi/NioClient.java new file mode 100644 index 0000000..bee74f7 --- /dev/null +++ b/platform-operate-api/src/main/java/com/lyms/hospitalapi/NioClient.java @@ -0,0 +1,22 @@ +package com.lyms.hospitalapi; + +import com.lyms.platform.common.utils.PropertiesUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.io.IOException; + +/** + * Created by riecard on 2016/11/24. + */ +@Service +public class NioClient { + + public static final String NIO_CLIENT_START = PropertiesUtils.getPropertyValue("nio_client_start"); + + @PostConstruct + public void init() throws IOException { + if ("1".equals(NIO_CLIENT_START)) { + } + } +} diff --git a/platform-operate-api/src/main/java/com/lyms/nio/NIOServer.java b/platform-operate-api/src/main/java/com/lyms/nio/NIOServer.java deleted file mode 100644 index d767a5b..0000000 --- a/platform-operate-api/src/main/java/com/lyms/nio/NIOServer.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.lyms.nio; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.ByteBuffer; -import java.nio.channels.*; -import java.util.Iterator; - -/** - * Created by riecard on 2016/11/17. - */ -public class NIOServer { - //通道管理器 - private Selector selector; - - /** - * 获得一个ServerSocket通道,并对该通道做一些初始化的工作 - * @param port 绑定的端口号 - * @throws IOException - */ - public void initServer(int port) throws IOException { - // 获得一个ServerSocket通道 - ServerSocketChannel serverChannel = ServerSocketChannel.open(); - // 设置通道为非阻塞 - serverChannel.configureBlocking(false); - // 将该通道对应的ServerSocket绑定到port端口 - serverChannel.socket().bind(new InetSocketAddress(port)); - // 获得一个通道管理器 - this.selector = Selector.open(); - //将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后, - //当该事件到达时,selector.select()会返回,如果该事件没到达selector.select()会一直阻塞。 - serverChannel.register(selector, SelectionKey.OP_ACCEPT); - new BeatTask(selector).start(); - } - - /** - * 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理 - * @throws IOException - */ - @SuppressWarnings("unchecked") - public void listen() throws IOException { - System.out.println("服务端启动成功!"); - // 轮询访问selector - while (true) { - //当注册的事件到达时,方法返回;否则,该方法会一直阻塞 - selector.select(); - // 获得selector中选中的项的迭代器,选中的项为注册的事件 - Iterator ite = this.selector.selectedKeys().iterator(); - while (ite.hasNext()) { - SelectionKey key = (SelectionKey) ite.next(); - // 删除已选的key,以防重复处理 - ite.remove(); - // 客户端请求连接事件 - if (key.isAcceptable()) { - ServerSocketChannel server = (ServerSocketChannel) key - .channel(); - // 获得和客户端连接的通道 - SocketChannel channel = server.accept(); - // 设置成非阻塞 - channel.configureBlocking(false); - - //在这里可以给客户端发送信息哦 - channel.write(ByteBuffer.wrap(new String("向客户端发送了一条信息").getBytes())); - //在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。 - channel.register(this.selector, SelectionKey.OP_READ); - - // 获得了可读的事件 - } else if (key.isReadable()) { - int length = read(key); - if (length == 0) { - key.channel().close(); - } - } - - } - - } - } - /** - * 处理读取客户端发来的信息 的事件 - * @param key - * @throws IOException - */ - public int read(SelectionKey key) throws IOException{ - // 服务器可读取消息:得到事件发生的Socket通道 - SocketChannel channel = (SocketChannel) key.channel(); - // 创建读取的缓冲区 - ByteBuffer buffer = ByteBuffer.allocate(10); - channel.read(buffer); - byte[] data = buffer.array(); - String msg = new String(data).trim(); - if (msg.length() == 0) { - return 0; - } - System.out.println("服务端收到信息:"+msg); - ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes()); - channel.write(outBuffer);// 将消息回送给客户端 - return msg.length(); - } - - class BeatTask extends Thread { - - private Selector selector; - - public BeatTask(Selector selector) { - this.selector = selector; - } - - @Override - public void run() { - while (true) { - for (SelectionKey key:selector.keys()) { - System.out.println(key); - Channel channel = key.channel(); - if (channel instanceof SocketChannel) { - ByteBuffer outBuffer = ByteBuffer.wrap("heartbeat".getBytes()); - try { - ((SocketChannel)channel).write(outBuffer);// 将消息回送给客户端 - } catch (IOException e) { - e.printStackTrace(); - } - } - } - try { - sleep(3000L); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - } - - /** - * 启动服务端测试 - * @throws IOException - */ - public static void main(String[] args) throws IOException { - NIOServer server = new NIOServer(); - server.initServer(8000); - server.listen(); - } - - - -} \ No newline at end of file diff --git a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AccessPermissionFacade.java b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AccessPermissionFacade.java index 38648db..a019609 100644 --- a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AccessPermissionFacade.java +++ b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AccessPermissionFacade.java @@ -65,34 +65,12 @@ public class AccessPermissionFacade { }else{ dataPermissionsModel=new DataPermissionsModel(); } - - //如果院内权限 - if(PermissionTypeEnums.Type.USER.getId()==NumberUtils.toInt(accessPermissionAddRequest.getType())){ - //根据角色id拿对应的访问权限 - Roles roles = rolesService.getRoles(Integer.valueOf(accessPermissionAddRequest.getBizId())); - //角色没有绑定访问权限的情况 - if(null==roles.getPermissiontype()){ - return new BaseResponse().setErrorcode(ErrorCodeConstants.BUSINESS_ERROR).setErrormsg("角色没有绑定权限."); - } - - PermissionTypeEnums permissionTypeEnums = PermissionTypeEnums.getEnumsById(roles.getPermissiontype()); - - Assert.notNull(permissionTypeEnums, "权限类型不存在."); - if(CollectionUtils.isNotEmpty(accessPermissionAddRequest.getDeptId())){ - permission.setDeptid(accessPermissionAddRequest.getDeptId()); - } - permission.setType(permissionTypeEnums.getId()); + permission.setType(PermissionTypeEnums.ALL_HOSPITAL.getId()); + if(StringUtils.isNotEmpty(accessPermissionAddRequest.getBizId())){ dataPermissionsModel.getData().clear(); - dataPermissionsModel.addOnePer(roles.getHospitalid(), permission); - }else if(PermissionTypeEnums.Type.ADMIN.getId()==NumberUtils.toInt(accessPermissionAddRequest.getType())){ - //如果是院外的权限,就只需要设置医院的id - permission.setType(PermissionTypeEnums.ALL_HOSPITAL.getId()); - if(StringUtils.isNotEmpty(accessPermissionAddRequest.getBizId())){ - dataPermissionsModel.getData().clear(); - String[] hospitalIds= accessPermissionAddRequest.getBizId().split(","); - for(String id:hospitalIds){ - dataPermissionsModel.addOnePer(id, permission); - } + String[] hospitalIds= accessPermissionAddRequest.getBizId().split(","); + for(String id:hospitalIds){ + dataPermissionsModel.addOnePer(id, permission); } } dataPermissionsModel.setAreaPermission(accessPermissionAddRequest.getAreaPermission()); diff --git a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/AccessPermissionAddRequest.java b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/AccessPermissionAddRequest.java index 7b16d5d..0c62330 100644 --- a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/AccessPermissionAddRequest.java +++ b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/AccessPermissionAddRequest.java @@ -20,7 +20,6 @@ public class AccessPermissionAddRequest { private String type; //角色id或者医院id @FormParam("bizid") - @NotEmpty private String bizId; //用户id/角色id @FormParam("userid") diff --git a/platform-operate-api/src/main/resources/config.properties b/platform-operate-api/src/main/resources/config.properties index fc5270b..5d602a9 100644 --- a/platform-operate-api/src/main/resources/config.properties +++ b/platform-operate-api/src/main/resources/config.properties @@ -13,5 +13,12 @@ his_version=0 #统计中心url center_statistics_url=http://api.healthbaby.com.cn/ +#TCP服务器端口 +nio_server_port=8000 +#TCP服务器是否启动 1:true,2:false +nio_server_start=2 +#TCP客户端是否启动 1:true,2:false +nio_client_start=2 +