13:22, EEST
May 28, 2014
I am trying to follow the Java SDK Server tutorial from scratch, and I cannot make the server work.
The provided sample works well, but it’s a bit too rich for my needs. I would love to be able to understand what I’m doing wrong.
This the the log when I try to run my application:
05/28/2014 15:13:31.158 INFO Reading application certificate from C:\Users\…\OpcUAServer.der
05/28/2014 15:13:31.286 INFO Reading private key from keystore C:\Users\…\OpcUAServer.pem
05/28/2014 15:13:35.549 WARN Could not register server (offline) to Discovery Server at opc.tcp://localhost:4840 Cause: Failed to retrieve endpoints. The server is not available: opc.tcp://localhost:4840
05/28/2014 15:13:35.564 INFO Server endpoint bound to opc.tcp://**[hidden]**:0
05/28/2014 15:13:35.565 INFO Server endpoint bound to opc.tcp://**[hidden]**:0
05/28/2014 15:13:36.676 WARN Could not register server (online) to Discovery Server at opc.tcp://localhost:4840 Cause: Failed to retrieve endpoints. The server is not available: opc.tcp://localhost:4840
For some reason, the server is binding to port 0, even though I’ve called server.setPort(52520).
Here is my entire code. It really isn’t much!
package opcuaclient;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.cert.CertificateParsingException;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.Locale;
import static java.lang.System.out;
import org.opcfoundation.ua.builtintypes.DateTime;
import org.opcfoundation.ua.builtintypes.LocalizedText;
import org.opcfoundation.ua.core.ApplicationDescription;
import org.opcfoundation.ua.core.ApplicationType;
import org.opcfoundation.ua.core.UserTokenPolicy;
import org.opcfoundation.ua.transport.security.Cert;
import org.opcfoundation.ua.transport.security.SecurityMode;
import com.prosysopc.ua.ApplicationIdentity;
import com.prosysopc.ua.CertificateValidationListener;
import com.prosysopc.ua.PkiFileBasedCertificateValidator;
import com.prosysopc.ua.PkiFileBasedCertificateValidator.CertificateCheck;
import com.prosysopc.ua.PkiFileBasedCertificateValidator.ValidationResult;
import com.prosysopc.ua.SecureIdentityException;
import com.prosysopc.ua.server.*;
import com.prosysopc.ua.server.nodes.opcua.BuildInfoType;
public class OpcUAClient {
private static UaServer server;
protected final static CertificateValidationListener validationListener = new CertificateValidationListener(){
public ValidationResult onValidate(Cert certificate,
ApplicationDescription applicationDescription, EnumSet passedChecks) {
// Do not mind about URI…
if (passedChecks.containsAll(EnumSet.of(CertificateCheck.Trusted, CertificateCheck.Validity, CertificateCheck.Signature))) {
if (!passedChecks.contains(CertificateCheck.Uri))
try {
System.out.println(“Client’s ApplicationURI (“
+ applicationDescription.getApplicationUri()
+ “) does not match the one in certificate: “
+ PkiFileBasedCertificateValidator.getApplicationUriOfCertificate(certificate));
} catch (CertificateParsingException e) {
throw new RuntimeException(e);
}
return ValidationResult.AcceptPermanently;
}
return ValidationResult.Reject;
}
};
public static void main(String[] args) {
try {
server = new UaServer();
ApplicationDescription appDescription = new ApplicationDescription();
appDescription.setApplicationName(new LocalizedText(“OpcUAServer”, Locale.ENGLISH));
appDescription.setApplicationUri(“urn:localhost:UA:OpcUAServer”);
appDescription.setProductUri(“urn:prosysopc.com:UA:OpcUAServer”);
appDescription.setApplicationType(ApplicationType.Server);
final PkiFileBasedCertificateValidator validator = new PkiFileBasedCertificateValidator();
server.setCertificateValidator(validator);
validator.setValidationListener(validationListener);
final ApplicationIdentity identity = ApplicationIdentity.loadOrCreateCertificate(appDescription, “****”, “opcua”, new File(validator.getBaseDir(), “private”), true, server.getHostNames());
server.setApplicationIdentity(identity);
server.setPort(52520);
server.setUseLocalhost(true);
server.setServerName(“OPCUA/OpcUAServer”);
server.setUseAllIpAddresses(true);
server.setSecurityModes(SecurityMode.ALL);
// Define the supported user Token policies
server.addUserTokenPolicy(UserTokenPolicy.ANONYMOUS);
server.setDiscoveryServerUrl(“opc.tcp://localhost:4840”);
server.init();
// Initialize BuildInfo – using the version info from the SDK
// You should replace this with your own build information
final BuildInfoType buildInfo = server.getNodeManagerRoot()
.getServerData().getServerStatus().getBuildInfo();
// Fetch version information from the package manifest
final Package sdkPackage = UaServer.class.getPackage();
final String implementationVersion = sdkPackage
.getImplementationVersion();
if (implementationVersion != null) {
int splitIndex = implementationVersion.lastIndexOf(“.”);
final String softwareVersion = implementationVersion.substring(0,
splitIndex);
String buildNumber = implementationVersion
.substring(splitIndex + 1);
buildInfo.setManufacturerName(sdkPackage.getImplementationVendor());
buildInfo.setSoftwareVersion(softwareVersion);
buildInfo.setBuildNumber(buildNumber);
}
final URL classFile = UaServer.class
.getResource(“/src/opcuaclient/OpcUAClient.class”);
if (classFile != null) {
final File mfFile = new File(classFile.getFile());
GregorianCalendar c = new GregorianCalendar();
c.setTimeInMillis(mfFile.lastModified());
buildInfo.setBuildDate(new DateTime(c));
}
server.getSessionManager().setMaxSessionCount(50);
server.getSessionManager().setMaxSessionTimeout(3600000); // one hour
server.getSubscriptionManager().setMaxSubscriptionCount(50);
server.start();
while(true);
} catch (SecureIdentityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UaServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Thanks in advance
13:24, EEST
May 28, 2014
Here is the code on PasteBin, for better readability.
http://pastebin.com/sEv0VZaQ
15:41, EEST
December 21, 2011
6:26, EEST
May 28, 2014
10:00, EEST
December 21, 2011
14:44, EEST
May 28, 2014
Well, after a lot of digging around, and basically removing everything in the Sample Demo Server to find out what I was doing wrong, it turns out I was creating the ApplicationIdentity too early.
Seems like calling
server.getHostNames()
before calling
server.setPort(52520);
server.setUseLocalhost(true);
server.setServerName(“OPCUA/OpcUAServer”);
server.setUseAllIpAddresses(true);
Can’t manage to bind to anything.
I would think that in order to make the experience better for future users, you guys should either specify it more clearly in the docs, or show an error message that makes a bit of sense!
7:44, EEST
December 21, 2011
Great – that you found it out. Pity that you had to search for it that long.
Actually, this comment is from SampleConsoleServer:
// certificate.
// We do this after defining the endpoints to be able to use
// getHostNames()
Nevertheless, I did not know that it can fail like this. HostNames was supposed to be used for the identity only, but it seems to have this nasty side-effect.
The problem seems to be that getHostNames() initialises the serverUris (which is used to bind the endpoints), which is then cached and not reset after you modify the settings. I fixed this and improved the documentation a bit. Will be available in the upcoming version 1.5.0.
Thanks for your report.
Most Users Ever Online: 1919
Currently Online:
64 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Heikki Tahvanainen: 402
hbrackel: 144
rocket science: 88
pramanj: 86
Francesco Zambon: 83
Ibrahim: 78
Sabari: 62
kapsl: 57
gjevremovic: 49
Xavier: 43
Member Stats:
Guest Posters: 0
Members: 738
Moderators: 7
Admins: 1
Forum Stats:
Groups: 3
Forums: 15
Topics: 1524
Posts: 6451
Newest Members:
jonathonmcintyre, fannielima, kristiewinkle8, rust, christamcdowall, redaahern07571, nigelbdhmp, travistimmons, AnnelCib, dalenegettingerModerators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1026, Jimmy Ni: 26, Matti Siponen: 346, Lusetti: 0
Administrators: admin: 1