public void setStatus(Status status) { if (status != Status.RECEIVED && sent == null && status != Status.DRAFT) { sent = UnixTime.now(); } this.status = status; }
public Builder defaults(long clientNonce) { version = BitmessageContext.CURRENT_VERSION; services = 1; timestamp = UnixTime.now(); userAgent = "/Jabit:0.0.1/"; streamNumbers = new long[]{1}; nonce = clientNonce; return this; }
private Connection(InternalContext context, Mode mode, Socket socket, Map<InventoryVector, Long> commonRequestedObjects, NetworkAddress node, long syncTimeout) { super(context, mode, node, commonRequestedObjects, syncTimeout); this.startTime = UnixTime.now(); this.socket = socket; }
private boolean checkOpenRequests() { return !requestedObjects.isEmpty() && lastObjectTime > 0 && (UnixTime.now() - lastObjectTime) > 2 * MINUTE; }
public Builder defaults() { version = BitmessageContext.CURRENT_VERSION; services = 1; timestamp = UnixTime.now(); nonce = new Random().nextInt(); userAgent = "/Jabit:0.0.1/"; streamNumbers = new long[]{1}; return this; }
private void cleanupIvCache() { Long fiveMinutesAgo = UnixTime.now(-5 * MINUTE); for (Map.Entry<InventoryVector, Long> entry : ivCache.entrySet()) { if (entry.getValue() < fiveMinutesAgo) { ivCache.remove(entry.getKey()); } } }
public NetworkAddress build() { if (time == 0) { time = UnixTime.now(); } return new NetworkAddress(this); } }
@Override protected void send(MessagePayload payload) { sendingQueue.add(payload); if (payload instanceof GetData) { Long now = UnixTime.now(); List<InventoryVector> inventory = ((GetData) payload).getInventory(); requestedObjects.addAll(inventory); for (InventoryVector iv : inventory) { commonRequestedObjects.put(iv, now); } } }
@Override public List<InventoryVector> getInventory(long... streams) { List<InventoryVector> result = new LinkedList<>(); for (long stream : streams) { getCache(stream).entrySet().stream() .filter(e -> e.getValue() > now()) .forEach(e -> result.add(e.getKey())); } return result; }
private void cleanUp() { try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement( "DELETE FROM Node WHERE time<?") ) { ps.setLong(1, now(-28 * DAY)); ps.executeUpdate(); } catch (SQLException e) { LOG.error(e.getMessage(), e); } }
@Override public void cleanup() { try ( Connection connection = config.getConnection(); Statement stmt = connection.createStatement() ) { stmt.executeUpdate("DELETE FROM Inventory WHERE expires < " + now(-5 * MINUTE)); } catch (SQLException e) { LOG.debug(e.getMessage(), e); } for (Map<InventoryVector, Long> c : cache.values()) { c.entrySet().removeIf(e -> e.getValue() < now(-5 * MINUTE)); } } }
public NetworkAddress build() { if (time == 0) { time = UnixTime.now(); } return new NetworkAddress(this); } }
private void updateIvCache(List<InventoryVector> inventory) { cleanupIvCache(); Long now = UnixTime.now(); for (InventoryVector iv : inventory) { ivCache.put(iv, now); } }
public void updateNextTry() { if (to != null) { if (nextTry == null) { if (sent != null && to.has(Feature.DOES_ACK)) { nextTry = UnixTime.now(+ttl); retries++; } } else { nextTry = nextTry + (1 << retries) * ttl; retries++; } } }
private static byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException { BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now()); LOG.debug("TTL: " + TTL + "s"); BigInteger numerator = TWO.pow(64); BigInteger powLength = BigInteger.valueOf(object.getPayloadBytesWithoutNonce().length + extraBytes); BigInteger denominator = BigInteger.valueOf(nonceTrialsPerByte).multiply(powLength.add(powLength.multiply(TTL).divide(BigInteger.valueOf(2).pow(16)))); return Bytes.expand(numerator.divide(denominator).toByteArray(), 8); }
@SuppressWarnings("RedundantIfStatement") protected boolean syncFinished(NetworkMessage msg) { if (mode != SYNC) { return false; } if (Thread.interrupted()) { return true; } if (state != ACTIVE) { return false; } if (syncTimeout < UnixTime.now()) { LOG.info("Synchronization timed out"); return true; } if (!sendingQueue.isEmpty()) { syncReadTimeout = System.currentTimeMillis() + 1000; return false; } if (msg == null) { return syncReadTimeout < System.currentTimeMillis(); } else { syncReadTimeout = System.currentTimeMillis() + 1000; return false; } }
@Override public byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) { if (nonceTrialsPerByte == 0) nonceTrialsPerByte = NETWORK_NONCE_TRIALS_PER_BYTE; if (extraBytes == 0) extraBytes = NETWORK_EXTRA_BYTES; BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now()); BigInteger powLength = BigInteger.valueOf(object.getPayloadBytesWithoutNonce().length + extraBytes); BigInteger denominator = BigInteger.valueOf(nonceTrialsPerByte) .multiply( powLength.add( powLength.multiply(TTL).divide(TWO_POW_16) ) ); return Bytes.expand(TWO_POW_64.divide(denominator).toByteArray(), 8); }
private Map<InventoryVector, Long> getCache(long stream) { Map<InventoryVector, Long> result = cache.get(stream); if (result == null) { synchronized (cache) { if (cache.get(stream) == null) { result = new ConcurrentHashMap<>(); cache.put(stream, result); try ( Connection connection = config.getConnection(); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT hash, expires FROM Inventory " + "WHERE expires > " + now(-5 * MINUTE) + " AND stream = " + stream) ) { while (rs.next()) { result.put(new InventoryVector(rs.getBytes("hash")), rs.getLong("expires")); } } catch (SQLException e) { LOG.error(e.getMessage(), e); } } } } return result; }
@Override public List<Plaintext> findMessagesToResend() { return find("status='" + Plaintext.Status.SENT.name() + "'" + " AND next_try < " + UnixTime.now()); }
private void activateConnection() { LOG.info("Successfully established connection with node " + node); state = ACTIVE; node.setTime(UnixTime.now()); if (mode != SYNC) { sendAddresses(); ctx.getNodeRegistry().offerAddresses(Collections.singletonList(node)); } sendInventory(); }