Example usage for org.apache.commons.net.ntp TimeStamp TimeStamp

List of usage examples for org.apache.commons.net.ntp TimeStamp TimeStamp

Introduction

In this page you can find the example usage for org.apache.commons.net.ntp TimeStamp TimeStamp.

Prototype

public TimeStamp(Date d) 

Source Link

Document

Constructs a newly allocated NTP timestamp object that represents the Java Date argument.

Usage

From source file:com.eventsourcing.hlc.HybridTimestamp.java

public HybridTimestamp(PhysicalTimeProvider physicalTimeProvider) {
    this(physicalTimeProvider, new TimeStamp(new Date(0)).ntpValue(), 0);
}

From source file:com.bigdata.hbase.HBase.java

public void writeIntoTable(String colFamily, String colQualifier, String value) throws Exception {
    Connection c = pool.borrowObject();
    TableName tableName = TableName.valueOf(this.table);
    Table t = c.getTable(tableName);//from w  w  w  . j a  v  a 2 s . c  o m
    TimeStamp ts = new TimeStamp(new Date());
    Date d = ts.getDate();
    Put p = new Put(Bytes.toBytes(d.toString()));
    p.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(colQualifier), Bytes.toBytes(value));
    t.put(p);
    t.close();
    pool.returnObject(c);
}

From source file:com.eventsourcing.hlc.HybridTimestampTest.java

@Test
public void initialTimestamp() {
    HybridTimestamp timestamp = new HybridTimestamp(physicalTimeProvider);
    TimeStamp ntpTime = new TimeStamp(timestamp.getLogicalTime());
    assertEquals(ntpTime.getDate(), new Date(0));
}

From source file:com.eventsourcing.hlc.HybridTimestamp.java

/**
 * Compares two NTP timestamps (non-numerically)
 *
 * @param time1//www.  j a v a 2s .co  m
 * @param time2
 * @return 0 if equal, less than 0 if time1 < time2, more than 0 if time1 > time2
 */
public static int compare(long time1, long time2) {
    TimeStamp t1 = new TimeStamp(time1);
    TimeStamp t2 = new TimeStamp(time2);
    return compare(t1, t2);
}

From source file:com.eventsourcing.repository.RepositoryTest.java

@BeforeClass
public void setUpEnv() throws Exception {
    startTime = new TimeStamp(new Date());
    repository = new StandardRepository();
    repository.addCommandSetProvider(//  w  ww.  ja  v a2  s . c  om
            new PackageCommandSetProvider(new Package[] { RepositoryTest.class.getPackage() }));
    repository.addEventSetProvider(
            new PackageEventSetProvider(new Package[] { RepositoryTest.class.getPackage() }));
    journal = createJournal();
    repository.setJournal(journal);
    timeProvider = new NTPServerTimeProvider(new String[] { "localhost" });
    repository.setPhysicalTimeProvider(timeProvider);
    indexEngine = createIndexEngine();
    repository.setIndexEngine(indexEngine);
    lockProvider = new LocalLockProvider();
    repository.setLockProvider(lockProvider);
    repository.startAsync().awaitRunning();

    long size = journal.size(EntityLayoutIntroduced.class);
    assertTrue(size > 0);
    // make sure layout introductions don't duplicate
    repository.publish(
            new IntroduceEntityLayouts(Iterables.concat(repository.getCommands(), repository.getEvents())))
            .join();
    assertEquals(journal.size(EntityLayoutIntroduced.class), size);
}

From source file:com.eventsourcing.hlc.NTPServerTimeProvider.java

TimeStamp getTimestamp() throws TimeoutException {
    if (timestamp == null) {
        throw new TimeoutException();
    }/*from  www  . j a v a2s .c  o m*/
    TimeStamp ts = new TimeStamp(timestamp.ntpValue());
    long fraction = ts.getFraction();
    long seconds = ts.getSeconds();
    long nanoTime = System.nanoTime();
    long l = (nanoTime - nano) / 1_000_000_000;
    double v = (nanoTime - nano) / 1_000_000_000.0 - l;
    long i = (long) (v * 1_000_000_000);
    long fraction_ = fraction + i;
    if (fraction_ >= 1_000_000_000) {
        fraction_ -= 1_000_000_000;
        l++;
    }
    return new TimeStamp((seconds + l) << 32 | fraction_);
}

From source file:com.eventsourcing.hlc.HybridTimestamp.java

public String toString() {
    String logical = new TimeStamp(logicalTime).toUTCString();
    String timeStamp = new TimeStamp(timestamp()).toUTCString();
    return "<HybridTimestamp logical=" + logical + "@" + logicalCounter + " NTP=" + timeStamp + "/"
            + timestamp() + ">";
}

From source file:com.eventsourcing.hlc.HybridTimestamp.java

@Override
public BigInteger getSerializableComparable() {
    TimeStamp t = new TimeStamp(logicalTime);
    return BigInteger.valueOf(t.getSeconds()).shiftLeft(64)
            .add(BigInteger.valueOf(t.getFraction()).shiftLeft(32)).add(BigInteger.valueOf(logicalCounter));
}

From source file:com.eventsourcing.repository.RepositoryTest.java

@Test
@SneakyThrows/*from w  w  w  .java  2s. c o m*/
public void initialTimestamp() {
    HybridTimestamp t = repository.getTimestamp();
    long ts = t.timestamp();
    TimeStamp soon = new TimeStamp(new Date(new Date().toInstant().plus(1, ChronoUnit.SECONDS).toEpochMilli()));
    TimeStamp t1 = new TimeStamp(ts);
    assertTrue(HybridTimestamp.compare(t1, startTime) > 0);
    assertTrue(HybridTimestamp.compare(t1, soon) < 0);
}

From source file:org.fiware.cybercaptor.server.dra.Alert.java

/**
 * Load an alert from a idmef:Alert element
 *
 * @param alertElement the DOM element idmef:Alert
 *//*  w w w  .  jav  a  2 s.c om*/
public Alert(Element alertElement) {
    Namespace idmefNamespace = Namespace.getNamespace("http://iana.org/idmef");
    if (alertElement == null || !alertElement.getName().equals("Alert")) {
        throw new IllegalStateException("The IDMEF alert to parse is not valid");
    }

    Element createTimeElement = alertElement.getChild("CreateTime", idmefNamespace);
    if (createTimeElement != null) {
        timestamp = new TimeStamp(createTimeElement.getAttributeValue("ntpstamp").replaceAll("0x", ""))
                .getDate();
    }
    Element detectTimeElement = alertElement.getChild("DetectTime", idmefNamespace);
    if (detectTimeElement != null) {
        timestamp = new TimeStamp(detectTimeElement.getAttributeValue("ntpstamp").replaceAll("0x", ""))
                .getDate();
    }
    if (timestamp == null)
        throw new IllegalStateException("invalid timestamp for the IDMEF alert");

    //sources
    for (Element sourceElement : alertElement.getChildren("Source", idmefNamespace)) {
        Element sourceNode = sourceElement.getChild("Node", idmefNamespace);
        if (sourceNode != null) {
            Element sourceAddress = sourceNode.getChild("Address", idmefNamespace);
            //If there is an address
            if (sourceAddress != null) {
                Element sourceIP = sourceAddress.getChild("address", idmefNamespace);
                Element sourceMask = sourceAddress.getChild("netmask", idmefNamespace);
                if (sourceIP != null && sourceMask != null) {
                    this.sources.add(sourceIP.getText() + "/" + sourceMask);
                } else if (sourceIP != null) {
                    this.sources.add(sourceIP.getText());
                }
            } else { // There is no address, their must be a "name"
                Element sourceName = sourceNode.getChild("name", idmefNamespace);
                if (sourceName != null) {
                    this.sources.add(sourceName.getText());
                }
            }
        }
    }

    //targets
    for (Element targetElement : alertElement.getChildren("Target", idmefNamespace)) {
        Element targetNode = targetElement.getChild("Node", idmefNamespace);
        if (targetNode != null) {
            Element targetAddress = targetNode.getChild("Address", idmefNamespace);
            //If there is an address
            if (targetAddress != null) {
                Element targetIP = targetAddress.getChild("address", idmefNamespace);
                Element targetMask = targetAddress.getChild("netmask", idmefNamespace);
                if (targetIP != null && targetMask != null) {
                    this.targets.add(targetIP.getText() + "/" + targetMask);
                } else if (targetIP != null) {
                    this.targets.add(targetIP.getText());
                }
            } else { // There is no address, their must be a "name"
                Element targetName = targetNode.getChild("name", idmefNamespace);
                if (targetName != null) {
                    this.targets.add(targetName.getText());
                }
            }
        }
    }

    //add classification information
    Element classificationElement = alertElement.getChild("Classification", idmefNamespace);
    if (classificationElement != null) {
        this.name = classificationElement.getAttributeValue("text");
        for (Element referenceElement : classificationElement.getChildren("Reference", idmefNamespace)) {
            switch (referenceElement.getAttributeValue("origin")) {
            case "cve":
                cveLinks.put(referenceElement.getChild("name", idmefNamespace).getText(),
                        referenceElement.getChild("url", idmefNamespace).getText());
                break;
            }
        }
    }
}