Search in sources :

Example 1 with ENetworkProtocol

use of com.helger.network.port.ENetworkProtocol in project ph-web by phax.

the class MainCreateDefaultPorts method main.

public static void main(final String[] args) throws Exception {
    final StringBuilder aSB = new StringBuilder();
    final IMicroDocument aDoc = MicroReader.readMicroXML(new File("src/test/resources/Service Name and Transport Protocol Port Number Registry.xml"));
    for (final IMicroElement aRecord : aDoc.getDocumentElement().getAllChildElements("record")) {
        final String sProtocol = MicroHelper.getChildTextContent(aRecord, "protocol");
        final ENetworkProtocol eProtocol = ENetworkProtocol.getFromIDOrNull(sProtocol);
        if (eProtocol == null) {
            // E.g. sctp
            continue;
        }
        final String sNumber = MicroHelper.getChildTextContent(aRecord, "number");
        if (sNumber == null)
            continue;
        int nMin;
        int nMax;
        if (sNumber.indexOf('-') > 0) {
            final String[] aParts = StringHelper.getExplodedArray('-', sNumber, 2);
            nMin = StringParser.parseInt(aParts[0], -1);
            if (nMin < 0)
                throw new IllegalStateException("From part of '" + sNumber + "' is unknown");
            nMax = StringParser.parseInt(aParts[1], -1);
            if (nMax < 0)
                throw new IllegalStateException("To part of '" + sNumber + "' is unknown");
        } else {
            final int nNumber = StringParser.parseInt(sNumber, -1);
            if (nNumber < 0)
                throw new IllegalStateException("Number '" + sNumber + "' is unknown");
            nMin = nMax = nNumber;
        }
        final String sName = StringHelper.getNotNull(MicroHelper.getChildTextContent(aRecord, "name"));
        final String sDescription = StringHelper.getNotNull(MicroHelper.getChildTextContent(aRecord, "description"));
        if ("Unassigned".equals(sDescription))
            continue;
        if (sDescription.contains("IANA assigned this well-formed service name as a replacement for"))
            continue;
        for (int nPort = nMin; nPort <= nMax; ++nPort) {
            if (nPort < 1024)
                aSB.append("public static final INetworkPort " + eProtocol.name() + "_" + nPort + (StringHelper.hasText(sName) ? "_" + _id(sName) : "") + " = _registerPort (" + nPort + ", ENetworkProtocol." + eProtocol.name() + ", " + _quote(sName) + ", " + _quote(sDescription) + ");\n");
        }
    }
    LOGGER.info(aSB.toString());
}
Also used : IMicroElement(com.helger.xml.microdom.IMicroElement) ENetworkProtocol(com.helger.network.port.ENetworkProtocol) IMicroDocument(com.helger.xml.microdom.IMicroDocument) File(java.io.File)

Example 2 with ENetworkProtocol

use of com.helger.network.port.ENetworkProtocol in project ph-web by phax.

the class MainPortScanner method main.

public static void main(final String[] args) {
    final int nStartPort = CNetworkPort.MINIMUM_PORT_NUMBER;
    final int nEndPort = CNetworkPort.MAXIMUM_PORT_NUMBER;
    final EnumSet<ENetworkProtocol> aTypes = EnumSet.allOf(ENetworkProtocol.class);
    final StopWatch aSW = StopWatch.createdStarted();
    for (int nPort = nStartPort; nPort <= nEndPort; ++nPort) for (final ENetworkProtocol eType : aTypes) {
        final int nFinalPort = nPort;
        final boolean bIsUsed = eType.isPortUsed(nPort);
        if (bIsUsed) {
            LOGGER.info(eType.name() + " Port " + nPort + " is used");
            DefaultNetworkPorts.forEachPort(x -> x.getPort() == nFinalPort && x.getProtocol() == eType, x -> LOGGER.info("  " + StringHelper.getConcatenatedOnDemand(x.getName(), ": ", x.getDescription())));
        }
    }
    aSW.stop();
    final int nCount = (nEndPort - nStartPort) * aTypes.size();
    LOGGER.info(nCount + " ports checked in " + aSW.getMillis() + " ms");
}
Also used : DefaultNetworkPorts(com.helger.network.port.DefaultNetworkPorts) Logger(org.slf4j.Logger) StringHelper(com.helger.commons.string.StringHelper) StopWatch(com.helger.commons.timing.StopWatch) LoggerFactory(org.slf4j.LoggerFactory) ENetworkProtocol(com.helger.network.port.ENetworkProtocol) EnumSet(java.util.EnumSet) CNetworkPort(com.helger.network.port.CNetworkPort) ENetworkProtocol(com.helger.network.port.ENetworkProtocol) StopWatch(com.helger.commons.timing.StopWatch)

Aggregations

ENetworkProtocol (com.helger.network.port.ENetworkProtocol)2 StringHelper (com.helger.commons.string.StringHelper)1 StopWatch (com.helger.commons.timing.StopWatch)1 CNetworkPort (com.helger.network.port.CNetworkPort)1 DefaultNetworkPorts (com.helger.network.port.DefaultNetworkPorts)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 File (java.io.File)1 EnumSet (java.util.EnumSet)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1