📄 registrationstable.java
字号:
/* * RegistrationTable.java * * Created on October 10, 2002, 11:19 PM */package gov.nist.examples.busy.gateway.registrar;import javax.sip.message.*; import javax.sip.header.*;import javax.sip.address.*;import java.util.*;/** * * @author olivier * @version 1.0 */public class RegistrationsTable{ protected Registrar registrar; protected Hashtable registrations; protected Hashtable expiresTaskTable; /** Creates new RegistrationTable */ public RegistrationsTable(Registrar registrar) { this.registrar=registrar; registrations=new Hashtable(); expiresTaskTable=new Hashtable(); } public Hashtable getRegistrations() { return registrations; } public Hashtable getExpiresTaskTable() { return expiresTaskTable; } public synchronized boolean hasRegistration(String key) { boolean res=registrations.containsKey(key.toLowerCase()); if (res) System.out.println ("RegistrationsTable, hasRegistration(), Checking registration for \"" +key.toLowerCase()+"\" : registered"); else { System.out.println ("RegistrationsTable, hasRegistration(), Checking registration for \"" +key.toLowerCase()+"\" : not registered"); } return res; } protected void addRegistration(String key,Request request) throws Exception{ Vector contacts=Registrar.getContactHeaders(request); int expiresTimeHeader=-1; Registration registration=new Registration(); registration.key=key; ExpiresHeader expiresHeader= (ExpiresHeader)request.getHeader(ExpiresHeader.NAME); if (expiresHeader!=null) { expiresTimeHeader=expiresHeader.getExpires(); if (expiresTimeHeader > Registrar.EXPIRES_TIME_MAX || expiresTimeHeader < Registrar.EXPIRES_TIME_MIN ) expiresTimeHeader=Registrar.EXPIRES_TIME_MAX; } else expiresTimeHeader=Registrar.EXPIRES_TIME_MAX; for( int i=0; i<contacts.size();i++) { ContactHeader contactHeader=(ContactHeader)contacts.elementAt(i); if (contactHeader.getExpires()==-1 ) { contactHeader.setExpires(expiresTimeHeader); } registration.addContactHeader(contactHeader); startTimer(key,contactHeader.getExpires(),contactHeader); } ToHeader toHeader=(ToHeader)request.getHeader(ToHeader.NAME); Address toAddress=toHeader.getAddress(); String displayName=toAddress.getDisplayName(); if (displayName !=null) registration.setDisplayName(displayName); // Store the to and from headers for binding to the responder. registration.toHeader = toHeader; FromHeader fromHeader = (FromHeader)request.getHeader(FromHeader.NAME); registration.fromHeader = fromHeader; registrations.put(key,registration); System.out.println ("RegistrationsTable, addRegistration(), registration "+ " added for the key: "+key); printRegistrations(); } protected void addRegistration(Registration registration) throws Exception{ Vector contacts=registration.getContactsList(); if (contacts==null || contacts.isEmpty()) { throw new Exception("contact list is empty, registration not added!"); } String key=registration.getKey(); if (key==null) throw new Exception("key is null, registration not added!"); for( int i=0; i<contacts.size();i++) { ContactHeader contactHeader=(ContactHeader)contacts.elementAt(i); if (contactHeader.getExpires()==-1 ) { contactHeader.setExpires(Registrar.EXPIRES_TIME_MAX); } startTimer(key,contactHeader.getExpires(),contactHeader); } registrations.put(key,registration); System.out.println("RegistrationsTable, addRegistration(), registration "+ " added for the key: "+key); printRegistrations(); } public void removeRegistration(String key) { System.out.println("RegistrationsTable, removeRegistration(), "+ " registration removed"+ " for the key: "+key); Registration registration=(Registration)registrations.get(key); registrations.remove(key); printRegistrations(); } public void removeContact(String key,ContactHeader contactHeader) { System.out.println("RegistrationsTable, removeContact(), "+ " contact removed for the key: "+key); Registration registration=(Registration)registrations.get(key); if (registration!=null) { registration.removeContactHeader(contactHeader); printRegistrations(); if ( !registration.hasContacts()) { System.out.println("RegistrationsTable, removeContact(), the registration: "+ key+ " does not contain any contacts, we remove it"); removeRegistration(key); } } } public void updateRegistration(String key,Request request) throws Exception { System.out.println("RegistrationsTable, updateRegistration(), registration updated"+ " for the key: "+key); Vector contacts=Registrar.getContactHeaders(request); Registration registration=(Registration)registrations.get(key); System.out.println("RegistrationsTable, updateRegistration(), hello1"); int expiresTime=Registrar.EXPIRES_TIME_MAX; for( int i=0; i<contacts.size();i++) { ContactHeader contactHeader=(ContactHeader)contacts.elementAt(i); if (contactHeader.getExpires()!=-1 ) { expiresTime=contactHeader.getExpires(); } else { ExpiresHeader expiresHeader=(ExpiresHeader)request.getHeader(ExpiresHeader.NAME); if (expiresHeader!=null) { expiresTime=expiresHeader.getExpires(); } } if (expiresTime==0) { removeContact(key,contactHeader); } else { if (expiresTime > Registrar.EXPIRES_TIME_MAX || expiresTime < Registrar.EXPIRES_TIME_MIN) expiresTime=Registrar.EXPIRES_TIME_MAX; contactHeader.setExpires(expiresTime); System.out.println("RegistrationsTable, updateRegistration(), hello2"); if (registration.hasContactHeader(contactHeader)) { System.out.println("RegistrationsTable, updateRegistration(), hello3"); registration.updateContactHeader(contactHeader); } else registration.addContactHeader(contactHeader); startTimer(key,expiresTime,contactHeader); expiresTime=Registrar.EXPIRES_TIME_MAX; } } printRegistrations(); } public Vector getContactHeaders(String key) { Registration registration=(Registration)registrations.get(key); if (registration==null) return null; else return registration.getContactsList(); } public void startTimer (String key,int expiresTime,ContactHeader contactHeader) { // we kill the precedent timer related to this key if there is one: Address address=contactHeader.getAddress(); javax.sip.address.URI cleanedUri=Registrar.getCleanUri(address.getURI() ); String contactURI=cleanedUri.toString(); java.util.Timer oldTimer=(java.util.Timer)expiresTaskTable.get(contactURI); if (oldTimer !=null) { System.out.println("RegistrationsTable, startTimer(), An old timer has "+ " been stopped for the contact: "+contactURI); oldTimer.cancel(); } // Let's start a timer for this contact... ExpiresTask expiresTask=new ExpiresTask(key,contactHeader,this); java.util.Timer timer=new java.util.Timer(); timer.schedule(expiresTask,expiresTime*1000); expiresTaskTable.put(contactURI,timer); System.out.println("RegistrationsTable, startTimer(), timer started "+ " for the contact: "+contactURI+" , expiresTime:"+expiresTime); } protected void printRegistrations(){ System.out.println("********* Registration record *****************"); System.out.println(); for (Enumeration e = registrations.keys() ; e.hasMoreElements() ;) { String keyTable=(String)e.nextElement(); Registration registration=(Registration)registrations.get(keyTable); System.out.println("registered user: \""+keyTable+"\""); registration.print(); System.out.println(); } System.out.println("************************************************"); System.out.println(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -