package diamClient; import com.hp.opencall.diameter.*; import com.hp.opencall.diameter._3gpp.sh.*; import java.net.InetAddress; import java.util.*; class DiameterShClientApplicationSolution implements DiameterShListener { DiameterStack myStack; DiameterShProvider myShProvider; String localRealm; String localFQDN; String localURI; String localPort = "3869"; String peerPort = "3868"; String peerRealm; String peerURI; // Class constructor DiameterShClientApplicationSolution(String[] args) throws DiameterException { try { //building realm, URI and FQDN strings based on local configuration InetAddress.getLocalHost().getHostName().length(); localFQDN = InetAddress.getLocalHost().getCanonicalHostName() ; localRealm = localFQDN.substring(localFQDN.indexOf(".") + 1); localURI = "aaa://"+localFQDN+":"+localPort; System.out.println("local realm: "+localRealm); System.out.println("local FQDN: "+localFQDN); System.out.println("local URI: "+localURI); // create peer information for a local/loopback config peerPort = "3868"; peerRealm = localRealm; peerURI = "aaa://"+localFQDN+":"+peerPort; System.out.println("\npeer realm: "+peerRealm); System.out.println("peer FQDN: "+localFQDN); System.out.println("peer URI: "+peerURI); // create and set myStack properties Properties myStackProperties = new Properties(); myStackProperties.setProperty("com.hp.opencall.diameter.DISABLE_SCTP", "true"); // creates a DiameterFactory DiameterFactory myFactory; if ((myFactory = DiameterFactory.getInstance()) == null) { System.out.println("Cannot get DiameterFactory instance"); System.exit(0); } // creates a DiameterStack myStack = myFactory.createDiameterStack(localRealm, localFQDN, myStackProperties); // create provider, and attach listener to the provider myShProvider = myStack. createDiameterShProvider(null); myShProvider.setDiameterShListener(this); // create a listening point for incoming messages myStack.createDiameterListeningPoint(localURI); // create a route to peer myStack.createDiameterRoute("Sh", peerRealm, peerURI, 1); } catch (Exception e) {e.printStackTrace() ;} } // end of constructor // - Send Stateless session UDR Sh message // - send message // - Save application data in the session obect. // this data will be retrieved upon reception of the answer message public void sendShUserDataRequest() { try { //check if the connection is established with the peer realm. if not exit. boolean isRealmAvailable = false; isRealmAvailable = myStack.isRealmAvailable(peerRealm,"Sh"); if (isRealmAvailable == false) { System.out.println("peer realm "+peerRealm+" and application Sh unavailable. Exiting."); cleanUpAndExit(); } // get the Sh message factory DiameterShMessageFactory myShMessageFactory = myStack.getDiameterShMessageFactory(); //build UDR message contents, requesting the IMS user state from the HSS. DiameterMessage UDR = myShMessageFactory.createUserDataRequest(peerRealm,"sip:john.doe@hp.com", DiameterShMessageFactory.PUBLIC_IDENTITY_AVP ,DiameterShMessageFactory.IMS_USER_STATE); //send message System.out.println("\nClient sending message :\n"+UDR.toString() ); myShProvider.sendMessage(UDR); } catch (Exception e) {e.printStackTrace() ;} } void cleanUpAndExit() { try { /* Final cleanup before exit */ System.out.println("Cleaning up..."); /* * We use a do/while loop because the iterator cannot be re-used * once deleteDiameterRoute() has been called. Otherwise, a * ConcurrentModification exception is thrown. */ { Iterator it; do { it = myStack.getDiameterRoutes(); if (it.hasNext()) { DiameterRoute route = (DiameterRoute) it.next(); myStack.deleteDiameterRoute(route); route = null; } } while (it.hasNext()); } myStack.deleteDiameterProvider(myShProvider); myShProvider = null; { Iterator it; do { it = myStack.getDiameterListeningPoints(); if (it.hasNext()) { DiameterListeningPoint lp = (DiameterListeningPoint) it.next(); myStack.deleteDiameterListeningPoint(lp); lp = null; } } while (it.hasNext()); } myStack = null; System.out.println("Exiting."); System.gc(); System.exit(0); } catch (DiameterException e) { System.out.println("ERROR: Exception caught: " + e.toString()); e.printStackTrace(); System.exit(0); } } // DiameterListener methods implementation public void processEvent(DiameterEvent event) { // discover type of event if (event instanceof DiameterRealmStateChangeEvent) { // monitor remote realms availability DiameterRealmStateChangeEvent rsc = (DiameterRealmStateChangeEvent) event; if (rsc.isRealmAvailable()) { System.out.println("Realm "+rsc.getRealm()+" is available."); } else { System.out.println("Realm "+rsc.getRealm()+" is unavailable."); } } else { if (event instanceof DiameterMessageEvent) { // process incoming messages System.out.println("Client received message :\n"+((DiameterMessageEvent) event).getDiameterMessage().toString() ) ; } else { if (event instanceof DiameterSessionErrorEvent) { // manage session level errors System.out.println("Session error: "+ event.toString() ); } else { if (event instanceof DiameterTimeoutEvent) { // manage time outs System.out.println("Timeout: "+ event.toString() ); } else { if (event instanceof DiameterSessionDeletedEvent) { // manage session deletion System.out.println("Session deleted: "+ event.toString() ); } else { System.out.println("Event: "+ event.toString() ); } } } } } } public boolean isUnknownPeerAuthorized(DiameterMessage incomingCER) { return true; } public boolean sendHook(DiameterSession session, DiameterMessage message) { return true; } // DiameterShListener methods // the profileUpdateRequestReceived method is not used on the client side (AS) public void profileUpdateRequestReceived(String userIdentity, String userData, DiameterMessageEvent PUR){} // the profileUpdateAnswerReceived method is not used on the server side (HSS) public void profileUpdateAnswerReceived(String resultCode, DiameterMessageEvent PUA){} // the pushNotificationRequestReceived method is not used on the server side // (HSS-server sends this message to AS-client after AS registration) public void pushNotificationRequestReceived(String userIdentity, String userData, DiameterMessageEvent PNR){} // The pushNotificationAnswerReceived method is not used on the client side (AS) public void pushNotificationAnswerReceived(String resultCode, DiameterMessageEvent PNA){} // The subscribeNotificationRequestReceived method is not used on the client side (AS) public void subscribeNotificationRequestReceived(String userIdentity, String dataReference, String subsReqType, String serviceIndication, String serverName, DiameterMessageEvent SNR){} // The subscribeNotificationAnswerReceived method is not used on the server side (HSS) public void subscribeNotificationAnswerReceived(String resultCode, DiameterMessageEvent SNA){} // The subscribeNotificationrequesReceived method is not used on the client side (AS) public void userDataRequestReceived(String userIdentity, String dataReference, String requestedDomain, String currentLocation, String serviceIndication, String serverName, DiameterMessageEvent UDR) {} // The subscribeNotificationrequesReceived method is not used on the client side (AS) public void userDataAnswerReceived(String resultCode, String userData, DiameterMessageEvent UDA) { System.out.println((String) UDA.getDiameterMessage().toString() ); cleanUpAndExit(); } } class DiamShClientSolution { public static void main(String args[]) throws Exception { DiameterShClientApplicationSolution myClient = new DiameterShClientApplicationSolution(args); System.out.println("Client waiting 5 seconds..."); Thread.sleep(5000); myClient.sendShUserDataRequest() ; while (true); } }