001: /**
002: * $RCSfile$
003: * $Revision: $
004: * $Date: $
005: *
006: * Copyright (C) 2007 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.sip.tester.stack;
011:
012: import org.jivesoftware.openfire.sip.tester.comm.CommunicationsListener;
013: import org.jivesoftware.openfire.sip.tester.comm.CommunicationsException;
014: import org.jivesoftware.openfire.sip.tester.Log;
015: import org.jivesoftware.openfire.sip.sipaccount.SipAccount;
016:
017: import java.net.InetAddress;
018: import java.net.UnknownHostException;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: /**
023: * Title: SIP Register Tester
024: *
025: * @author Thiago Rocha Camargo (thiago@jivesoftware.com)
026: */
027:
028: public class SIPTest implements CommunicationsListener {
029:
030: private SipAccount sipAccount;
031: private InetAddress localAddress;
032: private Result result = null;
033: private SipManager sipManager = null;
034: private List<TestListener> listeners = new ArrayList<TestListener>();
035:
036: public enum Result {
037: Successfully, WrongUser, WrongPass, NetworkError, Trying, Timeout, Forbidden, WrongAuthUser
038: }
039:
040: ;
041:
042: public SIPTest(InetAddress localAddress, SipAccount sipAccount) {
043: this .sipAccount = sipAccount;
044: this .localAddress = localAddress;
045: sipManager = new SipManager(localAddress);
046: }
047:
048: public Result getResult() {
049: return result;
050: }
051:
052: private void setResult(Result result) {
053: if (!result.equals(this .result))
054: fireResultChanged(this .result, result);
055: this .result = result;
056: }
057:
058: public void test(int timeout) {
059: test(timeout, 1);
060: }
061:
062: public void test(int timeout, int tries) {
063:
064: for (int i = 0; i < tries
065: && (getResult() == null || Result.Timeout
066: .equals(getResult())); i++) {
067:
068: if (sipManager.isStarted())
069: try {
070: sipManager.stop();
071: } catch (CommunicationsException e) {
072: e.printStackTrace();
073: }
074:
075: setResult(Result.Trying);
076:
077: SIPConfig.setRegistrarAddress(sipAccount.getServer());
078: SIPConfig.setAuthenticationRealm(sipAccount.getServer());
079: SIPConfig.setDefaultDomain(sipAccount.getServer());
080:
081: try {
082: sipManager.start();
083: sipManager.addCommunicationsListener(this );
084: try {
085: sipManager.startRegisterProcess(sipAccount
086: .getSipUsername(), sipAccount
087: .getAuthUsername(), sipAccount
088: .getPassword());
089: } catch (CommunicationsException e) {
090: setResult(Result.NetworkError);
091: Log.error(e);
092: }
093:
094: try {
095: Thread.sleep(timeout);
096: } catch (InterruptedException e) {
097: Log.error(e);
098: }
099:
100: if (getResult().equals(Result.Trying))
101: setResult(Result.Timeout);
102:
103: } catch (CommunicationsException e) {
104: setResult(Result.NetworkError);
105: Log.error(e);
106: } finally {
107: try {
108: sipManager.stop();
109: } catch (CommunicationsException e) {
110: Log.error(e);
111: }
112: }
113: }
114: }
115:
116: public void fireResultChanged(Result old, Result current) {
117: for (TestListener listener : listeners)
118: listener.resultChanged(old, current);
119: }
120:
121: public void addListener(TestListener listener) {
122: listeners.add(listener);
123: }
124:
125: public void removeListener(TestListener listener) {
126: listeners.remove(listener);
127: }
128:
129: public void registered(RegistrationEvent evt) {
130: setResult(Result.Successfully);
131: try {
132: sipManager.unregister();
133: } catch (CommunicationsException e) {
134: Log.error(e);
135: }
136: }
137:
138: public void registering(RegistrationEvent evt) {
139: }
140:
141: public void registrationFailed(RegistrationEvent evt) {
142: if (evt.getType().equals(RegistrationEvent.Type.NotFound))
143: setResult(Result.WrongUser);
144: else if (evt.getType().equals(RegistrationEvent.Type.WrongPass))
145: setResult(Result.WrongPass);
146: else if (evt.getType().equals(RegistrationEvent.Type.Forbidden))
147: setResult(Result.Forbidden);
148: else if (evt.getType().equals(
149: RegistrationEvent.Type.WrongAuthUser))
150: setResult(Result.WrongAuthUser);
151: }
152:
153: public void unregistering(RegistrationEvent evt) {
154: }
155:
156: public void unregistered(RegistrationEvent evt) {
157: try {
158: sipManager.stop();
159: Log.debug("Stopped");
160: } catch (CommunicationsException e) {
161: Log.error(e);
162: }
163: }
164:
165: public static void main(String args[]) {
166:
167: try {
168: InetAddress localAddress = InetAddress.getLocalHost();
169: SipAccount sipAccount = new SipAccount("", "", "", "", "",
170: "", "", false);
171:
172: SIPTest sipTest = new SIPTest(localAddress, sipAccount);
173:
174: sipTest.test(3000, 2);
175:
176: } catch (UnknownHostException e) {
177: e.printStackTrace();
178: }
179:
180: }
181: }
|