Skip to content

Commit 04b96df

Browse files
committed
Fixes #171: When changing network from Wifi -> Mobile or the opposite we need to unregister before shutting down Signalling
1 parent 73ed7de commit 04b96df

File tree

4 files changed

+117
-21
lines changed

4 files changed

+117
-21
lines changed

restcomm.android.client.sdk/src/main/java/org/mobicents/restcomm/android/client/sdk/RCDevice.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,15 @@ public void run() {
230230
if ((reachabilityState == ReachabilityState.REACHABILITY_WIFI && newState == ReachabilityState.REACHABILITY_MOBILE) ||
231231
(reachabilityState == ReachabilityState.REACHABILITY_MOBILE && newState == ReachabilityState.REACHABILITY_WIFI)) {
232232
if (state != DeviceState.OFFLINE) {
233-
Log.w(TAG, "Reachability action: wifi/mobile available from movile/wifi. Device state: " + state);
233+
Log.w(TAG, "Reachability action: switch between wifi and mobile. Device state: " + state);
234234
// stop JAIN
235-
//DeviceImpl.GetInstance().Unregister();
236-
DeviceImpl.GetInstance().Shutdown();
237-
sipProfile = null;
238-
state = DeviceState.OFFLINE;
235+
DeviceImpl.GetInstance().RefreshNetworking();
236+
////DeviceImpl.GetInstance().Unregister();
237+
//DeviceImpl.GetInstance().Shutdown();
238+
//sipProfile = null;
239+
//state = DeviceState.OFFLINE;
239240
// start JAIN
240-
initializeSignalling();
241+
//initializeSignalling();
241242
reachabilityState = newState;
242243
return;
243244
}
@@ -266,6 +267,7 @@ public void shutdown() {
266267
this.listener = null;
267268

268269
if (DeviceImpl.isInitialized()) {
270+
DeviceImpl.GetInstance().Unregister();
269271
DeviceImpl.GetInstance().Shutdown();
270272
}
271273
state = DeviceState.OFFLINE;

sipua/src/main/java/org/mobicents/restcomm/android/sipua/impl/DeviceImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,14 @@ public void run() {
251251
registerRefreshHandler.postDelayed(myRunnable, registrationRefresh*1000);
252252

253253
}
254+
255+
public void RefreshNetworking()
256+
{
257+
this.sipManager.refreshNetworking(registrationExpiry);
258+
}
259+
254260
public void Unregister() {
255-
this.sipManager.Unregister();
261+
this.sipManager.Unregister(null);
256262
}
257263
@Override
258264
public SipManager GetSipManager() {

sipua/src/main/java/org/mobicents/restcomm/android/sipua/impl/SipManager.java

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,86 @@ public boolean shutdown()
191191

192192
return true;
193193
}
194+
195+
public void refreshNetworking(int expiry)
196+
{
197+
// keep the old contact around to use for unregistration
198+
Address oldAddress = createContactAddress();
199+
if (udpListeningPoint != null) {
200+
try {
201+
sipProvider.removeSipListener(this);
202+
sipStack.deleteSipProvider(sipProvider);
203+
sipStack.deleteListeningPoint(udpListeningPoint);
204+
205+
udpListeningPoint = null;
206+
} catch (ObjectInUseException e) {
207+
e.printStackTrace();
208+
}
209+
}
210+
if (udpListeningPoint == null) {
211+
// new network interface is up, let's retrieve its ip address
212+
this.sipProfile.setLocalIp(getIPAddress(true));
213+
try {
214+
udpListeningPoint = sipStack.createListeningPoint(
215+
sipProfile.getLocalIp(), sipProfile.getLocalPort(),
216+
sipProfile.getTransport());
217+
sipProvider = sipStack.createSipProvider(udpListeningPoint);
218+
sipProvider.addSipListener(this);
219+
} catch (TransportNotSupportedException e) {
220+
e.printStackTrace();
221+
} catch (InvalidArgumentException e) {
222+
e.printStackTrace();
223+
} catch (ObjectInUseException e) {
224+
e.printStackTrace();
225+
} catch (TooManyListenersException e) {
226+
e.printStackTrace();
227+
}
228+
}
229+
// unregister the old contact (keep in mind that the new interface will be used to send this request)
230+
Unregister(oldAddress);
231+
// register the new contact with the given expiry
232+
Register(expiry);
233+
}
234+
235+
// release JAIN networking facilities
236+
public void unbind()
237+
{
238+
if (udpListeningPoint != null) {
239+
try {
240+
sipProvider.removeSipListener(this);
241+
sipStack.deleteSipProvider(sipProvider);
242+
sipStack.deleteListeningPoint(udpListeningPoint);
243+
244+
udpListeningPoint = null;
245+
} catch (ObjectInUseException e) {
246+
e.printStackTrace();
247+
}
248+
}
249+
}
250+
251+
// setup JAIN networking facilities
252+
public void bind()
253+
{
254+
if (udpListeningPoint == null) {
255+
this.sipProfile.setLocalIp(getIPAddress(true));
256+
try {
257+
udpListeningPoint = sipStack.createListeningPoint(
258+
sipProfile.getLocalIp(), sipProfile.getLocalPort(),
259+
sipProfile.getTransport());
260+
sipProvider = sipStack.createSipProvider(udpListeningPoint);
261+
sipProvider.addSipListener(this);
262+
} catch (TransportNotSupportedException e) {
263+
e.printStackTrace();
264+
} catch (InvalidArgumentException e) {
265+
e.printStackTrace();
266+
} catch (ObjectInUseException e) {
267+
e.printStackTrace();
268+
} catch (TooManyListenersException e) {
269+
e.printStackTrace();
270+
}
271+
}
272+
}
273+
194274
// *** Setters/Getters *** //
195275
public boolean isInitialized() {
196276
return initialized;
@@ -332,7 +412,7 @@ public void Register(int expiry) {
332412

333413
Register registerRequest = new Register();
334414
try {
335-
final Request r = registerRequest.MakeRequest(this, expiry);
415+
final Request r = registerRequest.MakeRequest(this, expiry, null);
336416
final SipProvider sipProvider = this.sipProvider;
337417
// Send the request statefully, through the client transaction.
338418
Thread thread = new Thread() {
@@ -357,7 +437,7 @@ public void run() {
357437
}
358438
}
359439

360-
public void Unregister() {
440+
public void Unregister(Address contact) {
361441
if (!latestProxyIp.equals(sipProfile.getRemoteIp())) {
362442
// proxy ip address has been updated, need to re-initialize
363443
if (!initialize())
@@ -366,7 +446,7 @@ public void Unregister() {
366446

367447
Register registerRequest = new Register();
368448
try {
369-
final Request r = registerRequest.MakeRequest(this, 0);
449+
final Request r = registerRequest.MakeRequest(this, 0, contact);
370450
final SipProvider sipProvider = this.sipProvider;
371451
// Send the request statefully, through the client transaction.
372452
Thread thread = new Thread() {
@@ -706,21 +786,23 @@ else if (response.getStatusCode() == Response.RINGING) {
706786

707787
// *** JAIN SIP: Exception *** //
708788
public void processIOException(IOExceptionEvent exceptionEvent) {
709-
System.out.println("IOException happened for "
710-
+ exceptionEvent.getHost() + " port = "
711-
+ exceptionEvent.getPort());
789+
Log.e(TAG, "SipManager.processIOException: " + exceptionEvent.toString() + "\n" +
790+
"\thost: " + exceptionEvent.getHost() + "\n" +
791+
"\tport: " + exceptionEvent.getPort());
712792
}
713793

714794
// *** JAIN SIP: Transaction terminated *** //
715-
public void processTransactionTerminated(
716-
TransactionTerminatedEvent transactionTerminatedEvent) {
717-
System.out.println("Transaction terminated event recieved");
795+
public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
796+
Log.i(TAG, "SipManager.processTransactionTerminated: " + transactionTerminatedEvent.toString() + "\n" +
797+
"\tclient transaction: " + transactionTerminatedEvent.getClientTransaction() + "\n" +
798+
"\tserver transaction: " + transactionTerminatedEvent.getServerTransaction() + "\n" +
799+
"\tisServerTransaction: " + transactionTerminatedEvent.isServerTransaction());
718800
}
719801

720802
// *** JAIN SIP: Dialog terminated *** //
721-
public void processDialogTerminated(
722-
DialogTerminatedEvent dialogTerminatedEvent) {
723-
System.out.println("dialogTerminatedEvent");
803+
public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
804+
Log.i(TAG, "SipManager.processDialogTerminated: " + dialogTerminatedEvent.toString() + "\n" +
805+
"\tdialog: " + dialogTerminatedEvent.getDialog());
724806
}
725807

726808
// *** JAIN SIP: Time out *** //

sipua/src/main/java/org/mobicents/restcomm/android/sipua/impl/sipmessages/Register.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
public class Register {
2121

22-
public Request MakeRequest(org.mobicents.restcomm.android.sipua.impl.SipManager sipManager, int expires) throws ParseException,
22+
public Request MakeRequest(org.mobicents.restcomm.android.sipua.impl.SipManager sipManager, int expires, final Address contact) throws ParseException,
2323
InvalidArgumentException {
2424

2525
AddressFactory addressFactory = sipManager.addressFactory;
@@ -36,7 +36,13 @@ public Request MakeRequest(org.mobicents.restcomm.android.sipua.impl.SipManager
3636
+ sipManager.getSipProfile().getRemoteIp());
3737
toAddress.setDisplayName(sipManager.getSipProfile().getSipUserName());
3838

39-
Address contactAddress = sipManager.createContactAddress();
39+
Address contactAddress;
40+
if (contact == null) {
41+
contactAddress = sipManager.createContactAddress();
42+
}
43+
else {
44+
contactAddress = contact;
45+
}
4046
ArrayList<ViaHeader> viaHeaders = sipManager.createViaHeader();
4147
URI requestURI = addressFactory.createAddress(
4248
"sip:" + sipManager.getSipProfile().getRemoteEndpoint())

0 commit comments

Comments
 (0)