Skip to content

Commit 2d1a21e

Browse files
committed
implemented new connection listening - helps a lot for the app and its ui
1 parent 2bf293f commit 2d1a21e

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

src/main/java/net/sharksystem/hub/HubConnectionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Interface for applications. It allows connection management with hubs.
1212
*/
13-
public interface HubConnectionManager {
13+
public interface HubConnectionManager extends NewHubConnectionListenerManagement {
1414
/**
1515
* Connect a hub
1616
* @param hcd Hub description

src/main/java/net/sharksystem/hub/HubConnectionManagerImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
import java.io.IOException;
1111

12+
/**
13+
* That class merges encounter and asap hub management
14+
*/
1215
public class HubConnectionManagerImpl extends BasicHubConnectionManager
13-
implements HubConnectionManager, HubConnectionManagerMessageHandler {
16+
implements HubConnectionManagerMessageHandler {
1417
private final ASAPPeer asapPeer;
1518
private ASAPHubManagerImpl hubManager;
1619

@@ -40,6 +43,20 @@ public HubConnector getHubConnector(HubConnectorDescription hcd) throws SharkExc
4043
}
4144

4245
private Thread hubManangerThread = null;
46+
47+
//////////////// hub new connection listener management
48+
public void addNewConnectedHubListener(NewHubConnectedListener connectedHubListener) {
49+
if(this.hubManager != null) {
50+
this.hubManager.addNewConnectedHubListener(connectedHubListener);
51+
}
52+
}
53+
54+
public void removeNewConnectedHubListener(NewHubConnectedListener connectedHubListener) {
55+
if(this.hubManager != null) {
56+
this.hubManager.removeNewConnectedHubListener(connectedHubListener);
57+
}
58+
}
59+
4360
public HubConnectionManagerImpl(
4461
ASAPEncounterManager encounterManager, ASAPPeer asapPeer, int waitIntervalInSeconds) {
4562
this.asapPeer = asapPeer;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.sharksystem.hub;
2+
3+
import net.sharksystem.hub.peerside.HubConnectorDescription;
4+
5+
public interface NewHubConnectedListener {
6+
void newHubConnected(HubConnectorDescription hcd);
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.sharksystem.hub;
2+
3+
public interface NewHubConnectionListenerManagement {
4+
void addNewConnectedHubListener(NewHubConnectedListener connectedHubListener);
5+
void removeNewConnectedHubListener(NewHubConnectedListener connectedHubListener);
6+
}

src/main/java/net/sharksystem/hub/peerside/ASAPHubManagerImpl.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.sharksystem.SharkException;
44
import net.sharksystem.asap.*;
5-
import net.sharksystem.hub.Connector;
5+
import net.sharksystem.hub.*;
66
import net.sharksystem.utils.AlarmClock;
77
import net.sharksystem.utils.AlarmClockListener;
88
import net.sharksystem.utils.Log;
@@ -11,7 +11,8 @@
1111
import java.io.IOException;
1212
import java.util.*;
1313

14-
public class ASAPHubManagerImpl implements ASAPHubManager, Runnable, NewConnectionListener, AlarmClockListener,
14+
public class ASAPHubManagerImpl implements ASAPHubManager, NewHubConnectionListenerManagement,
15+
Runnable, NewConnectionListener, AlarmClockListener,
1516
HubConnectorStatusListener {
1617
private static final int FORCE_NEW_ROUND_KEY = 1;
1718
private final ASAPEncounterManager asapEncounterManager;
@@ -135,6 +136,29 @@ public HubConnector getHubConnector(HubConnectorDescription hcd) throws SharkExc
135136
throw new SharkException("no connection to described hub available");
136137
}
137138

139+
////////////////////////////////////////////////////////////////////////////////////////////////////////
140+
// new hub connector listener management //
141+
////////////////////////////////////////////////////////////////////////////////////////////////////////
142+
private Set<NewHubConnectedListener> connectedHubListenerList = new HashSet<>();
143+
public void addNewConnectedHubListener(NewHubConnectedListener connectedHubListener) {
144+
this.connectedHubListenerList.add(connectedHubListener);
145+
}
146+
147+
public void removeNewConnectedHubListener(NewHubConnectedListener connectedHubListener) {
148+
this.connectedHubListenerList.remove(connectedHubListener);
149+
}
150+
151+
private void notifyNewHubConnectionListener(HubConnectorDescription hcd) {
152+
for(NewHubConnectedListener connectedHubListener : this.connectedHubListenerList) {
153+
connectedHubListener.newHubConnected(hcd);
154+
}
155+
}
156+
157+
private void hubConnectorStarted(HubConnectorDescription hcd) {
158+
this.runningConnectorDescriptions.add(hcd);
159+
this.notifyNewHubConnectionListener(hcd);
160+
}
161+
138162
public void connectASAPHubs(Collection<HubConnectorDescription> descriptions,
139163
ASAPPeer asapPeer, boolean killConnectionIfNotInList) {
140164
// for each description
@@ -164,7 +188,8 @@ public void run() {
164188
Log.writeLog(ASAPHubManagerImpl.this,ASAPHubManagerImpl.this.toString(),
165189
"hub connector initialized: " + hcd);
166190
// remember - it is running now
167-
ASAPHubManagerImpl.this.runningConnectorDescriptions.add(hcd);
191+
// ASAPHubManagerImpl.this.runningConnectorDescriptions.add(hcd);
192+
ASAPHubManagerImpl.this.hubConnectorStarted(hcd);
168193
} catch (IOException | ASAPException e) {
169194
Log.writeLog(ASAPHubManagerImpl.this,ASAPHubManagerImpl.this.toString(),
170195
"cannot create hub connector: " + e.getLocalizedMessage());

0 commit comments

Comments
 (0)