Java Examples for io.bitsquare.common.ByteArrayUtils

The following java examples will help you to understand the usage of io.bitsquare.common.ByteArrayUtils. These source code samples are taken from different open source projects.

Example 1
Project: bitcoin-exchange-master  File: Connection.java View source code
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
// Called form various threads
public void sendMessage(Message message) {
    if (!stopped) {
        if (!isCapabilityRequired(message) || isCapabilitySupported(message)) {
            try {
                Log.traceCall();
                // Throttle outbound messages
                long now = System.currentTimeMillis();
                long elapsed = now - lastSendTimeStamp;
                if (elapsed < 20) {
                    log.debug("We got 2 sendMessage requests in less than 20 ms. We set the thread to sleep " + "for 50 ms to avoid flooding our peer. lastSendTimeStamp={}, now={}, elapsed={}", lastSendTimeStamp, now, elapsed);
                    Thread.sleep(50);
                }
                lastSendTimeStamp = now;
                String peersNodeAddress = peersNodeAddressOptional.isPresent() ? peersNodeAddressOptional.get().toString() : "null";
                int size = ByteArrayUtils.objectToByteArray(message).length;
                if (message instanceof Ping || message instanceof RefreshTTLMessage) {
                    // pings and offer refresh msg we dont want to log in production
                    log.trace("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + "Sending direct message to peer" + "Write object to outputStream to peer: {} (uid={})\ntruncated message={} / size={}" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", peersNodeAddress, uid, Utilities.toTruncatedString(message), size);
                } else if (message instanceof PrefixedSealedAndSignedMessage && peersNodeAddressOptional.isPresent()) {
                    setPeerType(Connection.PeerType.DIRECT_MSG_PEER);
                    log.debug("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + "Sending direct message to peer" + "Write object to outputStream to peer: {} (uid={})\ntruncated message={} / size={}" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", peersNodeAddress, uid, Utilities.toTruncatedString(message), size);
                } else if (message instanceof GetDataResponse && ((GetDataResponse) message).isGetUpdatedDataResponse) {
                    setPeerType(Connection.PeerType.PEER);
                } else {
                    log.debug("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + "Write object to outputStream to peer: {} (uid={})\ntruncated message={} / size={}" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", peersNodeAddress, uid, Utilities.toTruncatedString(message), size);
                }
                if (!stopped) {
                    objectOutputStreamLock.lock();
                    objectOutputStream.writeObject(message);
                    objectOutputStream.flush();
                    statistic.addSentBytes(size);
                    statistic.addSentMessage(message);
                    // We don't want to get the activity ts updated by ping/pong msg
                    if (!(message instanceof KeepAliveMessage))
                        statistic.updateLastActivityTimestamp();
                }
            } catch (IOException e) {
                sharedModel.handleConnectionException(e);
            } catch (Throwable t) {
                log.error(t.getMessage());
                t.printStackTrace();
                sharedModel.handleConnectionException(t);
            } finally {
                if (objectOutputStreamLock.isLocked())
                    objectOutputStreamLock.unlock();
            }
        }
    } else {
        log.debug("called sendMessage but was already stopped");
    }
}
Example 2
Project: bitsquare-master  File: Connection.java View source code
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
// Called form various threads
public void sendMessage(Message message) {
    if (!stopped) {
        if (!isCapabilityRequired(message) || isCapabilitySupported(message)) {
            try {
                Log.traceCall();
                // Throttle outbound messages
                long now = System.currentTimeMillis();
                long elapsed = now - lastSendTimeStamp;
                if (elapsed < 20) {
                    log.debug("We got 2 sendMessage requests in less than 20 ms. We set the thread to sleep " + "for 50 ms to avoid flooding our peer. lastSendTimeStamp={}, now={}, elapsed={}", lastSendTimeStamp, now, elapsed);
                    Thread.sleep(50);
                }
                lastSendTimeStamp = now;
                String peersNodeAddress = peersNodeAddressOptional.isPresent() ? peersNodeAddressOptional.get().toString() : "null";
                int size = ByteArrayUtils.objectToByteArray(message).length;
                if (message instanceof Ping || message instanceof RefreshTTLMessage) {
                    // pings and offer refresh msg we dont want to log in production
                    log.trace("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + "Sending direct message to peer" + "Write object to outputStream to peer: {} (uid={})\ntruncated message={} / size={}" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", peersNodeAddress, uid, Utilities.toTruncatedString(message), size);
                } else if (message instanceof PrefixedSealedAndSignedMessage && peersNodeAddressOptional.isPresent()) {
                    setPeerType(Connection.PeerType.DIRECT_MSG_PEER);
                    log.debug("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + "Sending direct message to peer" + "Write object to outputStream to peer: {} (uid={})\ntruncated message={} / size={}" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", peersNodeAddress, uid, Utilities.toTruncatedString(message), size);
                } else if (message instanceof GetDataResponse && ((GetDataResponse) message).isGetUpdatedDataResponse) {
                    setPeerType(Connection.PeerType.PEER);
                } else {
                    log.debug("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + "Write object to outputStream to peer: {} (uid={})\ntruncated message={} / size={}" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", peersNodeAddress, uid, Utilities.toTruncatedString(message), size);
                }
                if (!stopped) {
                    objectOutputStreamLock.lock();
                    objectOutputStream.writeObject(message);
                    objectOutputStream.flush();
                    statistic.addSentBytes(size);
                    statistic.addSentMessage(message);
                    // We don't want to get the activity ts updated by ping/pong msg
                    if (!(message instanceof KeepAliveMessage))
                        statistic.updateLastActivityTimestamp();
                }
            } catch (IOException e) {
                sharedModel.handleConnectionException(e);
            } catch (Throwable t) {
                log.error(t.getMessage());
                t.printStackTrace();
                sharedModel.handleConnectionException(t);
            } finally {
                if (objectOutputStreamLock.isLocked())
                    objectOutputStreamLock.unlock();
            }
        }
    } else {
        log.debug("called sendMessage but was already stopped");
    }
}