public static byte[] streamableToByteBuffer(Streamable obj) throws Exception { int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() : 512; final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size); obj.writeTo(out); return Arrays.copyOf(out.buffer(), out.position()); }
public static byte[] collectionToByteBuffer(Collection<Address> c) throws Exception { final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)Util.size(c)); Util.writeAddresses(c,out); return Arrays.copyOf(out.buffer(), out.position()); }
protected void _sendBundledMessages() { try { if(target_list.isEmpty()) return; output.position(0); if(target_list.size() == 1) { sendSingleMessage(target_list.get(0)); // avg_batch_size.add(1); } else { avg_batch_size.add(target_list.size()); sendMessageList(target_dest, target_list.get(0).getSrc(), target_list); if(transport.statsEnabled()) transport.incrBatchesSent(1); } } finally { target_list.clear(); count=0; } }
/** * Sends all messages in the map. Messages for the same destination are bundled into a message list. * The map will be cleared when done. */ @GuardedBy("lock") protected void sendBundledMessages() { for(Map.Entry<Address,List<Message>> entry: msgs.entrySet()) { List<Message> list=entry.getValue(); if(list.isEmpty()) continue; output.position(0); if(list.size() == 1) sendSingleMessage(list.get(0)); else { Address dst=entry.getKey(); sendMessageList(dst, list.get(0).getSrc(), list); if(transport.statsEnabled()) transport.incrBatchesSent(1); } } clearMessages(); count=0; }
/** * Serializes/Streams an object into a byte buffer. * The object has to implement interface Serializable or Externalizable or Streamable. */ public static byte[] objectToByteBuffer(Object obj) throws Exception { if(obj == null) return TYPE_NULL_ARRAY; if(obj instanceof Streamable) { int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() : 512; final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size, true); out.write(TYPE_STREAMABLE); writeGenericStreamable((Streamable)obj,out); return Arrays.copyOf(out.buf,out.position()); } Byte type=PRIMITIVE_TYPES.get(obj.getClass()); if(type == null) { // will throw an exception if object is not serializable final ByteArrayDataOutputStream out_stream=new ByteArrayDataOutputStream(512, true); out_stream.write(TYPE_SERIALIZABLE); try(ObjectOutputStream out=new ObjectOutputStream(new OutputStreamAdapter(out_stream))) { out.writeObject(obj); out.flush(); return Arrays.copyOf(out_stream.buffer(), out_stream.position()); } } return marshalPrimitiveType(type, obj); }
protected synchronized void writeRequest(GossipData req) throws Exception { int size=req.serializedSize(); ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(size+5); req.writeTo(out); client.send(remote, out.buffer(), 0, out.position()); }
msg.writeTo(dos); byte[] buffer=dos.buffer(); byte[][] fragments=Util.fragmentBuffer(buffer, frag_size, dos.position()); num_frags=fragments.length; num_sent_frags+=num_frags;
@Override protected void sendMcastDiscoveryRequest(Message msg) { try { if(msg.getSrc() == null) msg.setSrc(local_addr); ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)msg.size()); msg.writeTo(out); for(int i=bind_port; i <= bind_port+port_range; i++) { DatagramPacket packet=new DatagramPacket(out.buffer(), 0, out.position(), dest_addr, i); sock.send(packet); } } catch(Exception ex) { log.error(Util.getMessage("FailedSendingDiscoveryRequest"), ex); } }
protected void getStateFrom(JChannel channel, Protocol prot, String stack, String ch, DataOutputStream out) throws Exception { ByteArrayDataOutputStream output=new ByteArrayDataOutputStream(1024); OutputStreamAdapter out_ad=new OutputStreamAdapter(output); Event evt=new Event(Event.STATE_TRANSFER_OUTPUTSTREAM, out_ad); if(channel != null) channel.up(evt); else prot.up(evt); int len=output.position(); if(len > 0) { Bits.writeString(stack, out); Bits.writeString(ch, out); out.writeInt(len); out.write(output.buffer(), 0, len); log.trace("%s: fetched %d bytes from %s:%s", local_addr, len, stack, ch); } }
protected void handleGetMembersRequest(Address sender, DataInput in) { GossipData req=readRequest(in, GossipType.GET_MBRS); if(req == null) return; GossipData rsp=new GossipData(GossipType.GET_MBRS_RSP, req.getGroup(), null); Map<Address,Entry> members=address_mappings.get(req.getGroup()); if(members != null) { for(Map.Entry<Address,Entry> entry : members.entrySet()) { Address logical_addr=entry.getKey(); PhysicalAddress phys_addr=entry.getValue().phys_addr; String logical_name=entry.getValue().logical_name; PingData data=new PingData(logical_addr, true, logical_name, phys_addr); rsp.addPingData(data); } } ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(rsp.serializedSize()); try { rsp.writeTo(out); server.send(sender, out.buffer(), 0, out.position()); } catch(Exception ex) { log.error("failed sending %d to %s: %s", GossipType.GET_MBRS_RSP, sender, ex); } }
protected void sendToMember(Address dest, GossipData request) { ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.serializedSize()); try { request.writeTo(out); server.send(dest, out.buffer(), 0, out.position()); } catch(Exception ex) { log.error("failed sending unicast message to %s: %s", dest, ex); } }
protected void sendToAllMembersInGroup(Set<Map.Entry<Address,Entry>> dests, GossipData request) { ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.serializedSize()); try { request.writeTo(out); } catch(Exception ex) { log.error("failed marshalling gossip data %s: %s; dropping request", request, ex); return; } for(Map.Entry<Address,Entry> entry: dests) { Entry e=entry.getValue(); if(e == null /* || e.phys_addr == null */) continue; try { server.send(e.client_addr, out.buffer(), 0, out.position()); } catch(Exception ex) { log.error("failed sending message to %s (%s): %s", e.logical_name, e.phys_addr, ex); } } }
protected Object _down(Message msg) throws Exception { Address dest=msg.dest(); setSourceAddress(msg); // very important !! listToBuffer() will fail with a null src address !! int size=(int)msg.size(); ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(size + Global.INT_SIZE); out.writeInt(size); msg.writeTo(out); if(dest != null) // unicast sendTo(dest, out.buffer(), 0, out.position()); else { // multicast Collection<Address> dests=view != null? view.getMembers(): addr_table.keySet(); for(Address dst: dests) { try { sendTo(dst, out.buffer(), 0, out.position()); } catch(Throwable t) { log.error("failed sending multicast message to " + dst, t); } } } return null; }
protected static Buffer streamableToBuffer(byte req_or_rsp, byte type, Streamable obj) throws Exception { int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() : 100; ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size); out.writeByte(req_or_rsp); out.writeByte(type); obj.writeTo(out); return new Buffer(out.buffer(), 0, out.position()); }
protected void sendSingleMessage(final Message msg, final ByteArrayDataOutputStream output) { Address dest=msg.getDest(); try { output.position(0); Util.writeMessage(msg, output, dest == null); transport.doSend(output.buffer(), 0, output.position(), dest); if(transport.statsEnabled()) transport.incrNumSingleMsgsSent(1); } catch(SocketException | SocketTimeoutException sock_ex) { log.trace(Util.getMessage("SendFailure"), transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), sock_ex.toString(), msg.printHeaders()); } catch(Throwable e) { log.error(Util.getMessage("SendFailure"), transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), e.toString(), msg.printHeaders()); } }
public void run() { try { int drained=rb.drainToBlocking(remove_queue); if(drained == 1) { output.position(0); sendSingleMessage(remove_queue[0]); return; } for(int i=0; i < drained; i++) { Message msg=remove_queue[i]; long size=msg.size(); if(count + size >= transport.getMaxBundleSize()) sendBundledMessages(); addMessage(msg, msg.size()); } sendBundledMessages(); } catch(Throwable t) { } }
protected void sendSingleMessage(final Message msg) { Address dest=msg.getDest(); try { Util.writeMessage(msg, output, dest == null); transport.doSend(output.buffer(), 0, output.position(), dest); if(transport.statsEnabled()) transport.incrNumSingleMsgsSent(1); } catch(SocketException | SocketTimeoutException sock_ex) { log.trace(Util.getMessage("SendFailure"), transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), sock_ex.toString(), msg.printHeaders()); } catch(Throwable e) { log.error(Util.getMessage("SendFailure"), transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), e.toString(), msg.printHeaders()); } }
protected void sendMessageList(final Address dest, final Address src, final List<Message> list) { try { Util.writeMessageList(dest, src, transport.cluster_name.chars(), list, output, dest == null, transport.getId()); transport.doSend(output.buffer(), 0, output.position(), dest); } catch(SocketException | SocketTimeoutException sock_ex) { log.debug(Util.getMessage("FailureSendingMsgBundle"), transport.localAddress(),sock_ex); } catch(Throwable e) { log.error(Util.getMessage("FailureSendingMsgBundle"), transport.localAddress(), e); } }
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.serializedSize()); request.writeTo(out); route(request.group, request.addr, out.buffer(), 0, out.position());