public static String formatLocalDateTime(Long dateTime){ return toLocalDateTime(dateTime).format(dateTimeFormatter); }
public static <T> T deserialize(byte[] data, Class<T> cls) { try { T message = cls.newInstance(); Schema<T> schema = getSchema(cls); ProtostuffIOUtil.mergeFrom(data, message, schema); return message; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
private String formatMessage(String originalText, Response response) { ResponseHeader header = response.getHeader(); StringBuilder sb = new StringBuilder(); sb.append(originalText) .append(header.getSender()) .append(": ") .append(new String(response.getBody(), charset)) .append(" ") .append(DateTimeUtil.formatLocalDateTime(header.getTimestamp())) .append("\n"); return sb.toString(); } }
@Override public final void run() { try { info.getReceiver().write(ByteBuffer.wrap(ProtoStuffUtil.serialize(process()))); } catch (IOException e) { e.printStackTrace(); throw new TaskException(info); } catch (InterruptedException e) { e.printStackTrace(); throw new TaskException(info); } }
public void run() { try { while (connected) { int size = 0; selector.select(); for (Iterator<SelectionKey> it = selector.selectedKeys().iterator(); it.hasNext(); ) { SelectionKey selectionKey = it.next(); it.remove(); if (selectionKey.isReadable()) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((size = clientChannel.read(buf)) > 0) { buf.flip(); baos.write(buf.array(), 0, size); buf.clear(); } byte[] bytes = baos.toByteArray(); baos.close(); Response response = ProtoStuffUtil.deserialize(bytes, Response.class); handleResponse(response); } } } } catch (IOException e) { JOptionPane.showMessageDialog(null, "服务器关闭,请重新尝试连接"); isLogin = false; } }
String path = JOptionPane.showInputDialog("请输入保存的文件路径"); byte[] buf = response.getBody(); FileUtil.save(path, buf); if(path.endsWith("jpg")){
.timestamp(header.getTimestamp()) .build(), ZipUtil.zipCompress(result,suffix));
private void login() { String username = JOptionPane.showInputDialog("请输入用户名"); String password = JOptionPane.showInputDialog("请输入密码"); Message message = new Message( MessageHeader.builder() .type(MessageType.LOGIN) .sender(username) .timestamp(System.currentTimeMillis()) .build(), password.getBytes(charset)); try { clientChannel.write(ByteBuffer.wrap(ProtoStuffUtil.serialize(message))); } catch (IOException e) { e.printStackTrace(); } this.username = username; }
@Override public void handle(Message message, Selector server, SelectionKey client, BlockingQueue<Task> queue, AtomicInteger onlineUsers) throws InterruptedException { TaskDescription taskDescription = ProtoStuffUtil.deserialize(message.getBody(), TaskDescription.class); Task task = new Task((SocketChannel) client.channel(), taskDescription.getType(), taskDescription.getDesc(), message); try { queue.put(task); log.info("{}已放入阻塞队列",task.getReceiver().getRemoteAddress()); }catch (IOException e) { e.printStackTrace(); } } }
public static <T> byte[] serialize(T obj) { Class<T> cls = (Class<T>) obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema<T> schema = getSchema(cls); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } }
private void logout() { if (!isLogin) { return; } System.out.println("客户端发送下线请求"); Message message = new Message( MessageHeader.builder() .type(MessageType.LOGOUT) .sender(username) .timestamp(System.currentTimeMillis()) .build(), null); try { clientChannel.write(ByteBuffer.wrap(ProtoStuffUtil.serialize(message))); } catch (IOException e) { e.printStackTrace(); } }
byte[] bytes = baos.toByteArray(); baos.close(); Message message = ProtoStuffUtil.deserialize(bytes, Message.class); MessageHandler messageHandler = SpringContextUtil.getBean("MessageHandler", message.getHeader().getType().toString().toLowerCase()); try {
public void handle(SocketChannel channel,Message message) { try { byte[] response = ProtoStuffUtil.serialize( new Response( ResponseHeader.builder() .type(ResponseType.PROMPT) .sender(message.getHeader().getSender()) .timestamp(message.getHeader().getTimestamp()).build(), PromptMsgProperty.SERVER_ERROR.getBytes(PromptMsgProperty.charset))); channel.write(ByteBuffer.wrap(response)); } catch (IOException e) { e.printStackTrace(); } } }
@Override public void uncaughtException(Thread t, Throwable e) { try { if (e instanceof TaskException) { TaskException taskException = (TaskException) e; Task task = taskException.getInfo(); Message message = task.getMessage(); byte[] response = ProtoStuffUtil.serialize( new Response( ResponseHeader.builder() .type(ResponseType.PROMPT) .sender(message.getHeader().getSender()) .timestamp(message.getHeader().getTimestamp()).build(), PromptMsgProperty.TASK_FAILURE.getBytes(PromptMsgProperty.charset))); log.info("返回任务执行失败信息"); task.getReceiver().write(ByteBuffer.wrap(response)); } } catch (IOException e1) { e1.printStackTrace(); } } }
@Override public void handle(Message message, Selector server, SelectionKey client, BlockingQueue<Task> queue, AtomicInteger onlineUsers) { try { byte[] response = ProtoStuffUtil.serialize( new Response( ResponseHeader.builder() .type(ResponseType.NORMAL) .sender(message.getHeader().getSender()) .timestamp(message.getHeader().getTimestamp()).build(), message.getBody())); super.broadcast(response,server); } catch (IOException e) { e.printStackTrace(); } } }
SocketChannel clientChannel = (SocketChannel) client.channel(); userManager.logout(clientChannel); byte[] response = ProtoStuffUtil.serialize( new Response(ResponseHeader.builder() .type(ResponseType.PROMPT) onlineUsers.decrementAndGet(); byte[] logoutBroadcast = ProtoStuffUtil.serialize( new Response( ResponseHeader.builder()
try { if (userManager.login(clientChannel, username, password)) { byte[] response = ProtoStuffUtil.serialize( new Response( ResponseHeader.builder() byte[] loginBroadcast = ProtoStuffUtil.serialize( new Response( ResponseHeader.builder() byte[] response = ProtoStuffUtil.serialize( new Response( ResponseHeader.builder()