Skip to content

Commit

Permalink
Merge pull request #68 from ServiceInventoryManagementSystem/fix/mino…
Browse files Browse the repository at this point in the history
…r-bugs

Fixed some minor bugs cause quite a bit of issues
  • Loading branch information
arwassa authored May 30, 2018
2 parents a7393f0 + 657bee4 commit 4ffd667
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/sims/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static void main(String[] args) {
resourceManager, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true
);

resourceManager.postInit();

DiscoveryManager manager = new DiscoveryManager(resourceManager, config.probeInterval);

WsDiscovery.WsSettings wsSettings = new WsDiscovery.WsSettings(config.address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public abstract class BasicResourceManager implements IResourceManager{
Expand Down Expand Up @@ -71,6 +73,7 @@ private void remove(String remoteId){
serviceMap.remove(localRef);
reverseMap.remove(remoteId);
System.out.println("Removed service from map " + remoteId + " " + localRef);
flush();
}
}

Expand All @@ -95,6 +98,9 @@ public void stopWatching(String id){
this.remove(id);
}

public List<String> getAllServiceIds(){
return new ArrayList<String>(this.serviceMap.values());
}

@Override
public void dispose(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class HybernateResourceManager extends BasicResourceManager{
@Autowired
Expand All @@ -22,7 +23,20 @@ public class HybernateResourceManager extends BasicResourceManager{

public HybernateResourceManager(){
super();

}

public void postInit(){
for(String id : getAllServiceIds()){
Optional<Service> optional = serviceRepo.findOne(getExample(id));
if(!optional.isPresent()){
stopWatching(id);
}else{
System.out.println("Service is present: " + id);
}
}
}


private Example<Service> getExample(String id){
Service exampleService = new Service();
Expand Down Expand Up @@ -59,7 +73,7 @@ public String getId() {
}

public Single<IService> getById(String id){
return null;
return null;//serviceRepo.findOne(getExample(id)).get();
}

public Single<List<IService>> getOwnedServices(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public interface IResourceManager{

// Finish anything important
public void dispose();

public void postInit();
}
16 changes: 10 additions & 6 deletions src/main/java/org/sims/discovery/mdns/DnsDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DnsDiscovery implements IDiscoveryService, ServiceListener{
private boolean alive = false;
private boolean running = false;
private Subject<IService> dnsSubject;
private Thread probeThread;
private Subject<IService> serviceAddSubject = PublishSubject.create();
private Subject<IService> serviceUpdateSubject = PublishSubject.create();
private Subject<IService> serviceRemoveSubject = PublishSubject.create();
Expand Down Expand Up @@ -66,32 +67,35 @@ public Single<List<IService>> probeServices(){
if(!alive){
return Single.just(new ArrayList<IService>(0));
}
if(dnsSubject == null){
dnsSubject = PublishSubject.create();
if(dnsSubject == null && probeThread == null){
final Subject<IService> dnssub = PublishSubject.create();
dnsSubject = dnssub;
if(!jmdns.isProbing()){
jmdns.startProber();
}
Thread t = new Thread(){
final Thread t = new Thread(){
public void run(){
try{

while(jmdns.isProbing()){
sleep(100);
sleep(500);
}
}catch(Exception e){
System.err.println(e);
}
for(String type : settings.types){
for(ServiceInfo info : jmdns.list(type)){
DnsService service = new DnsService(info);
dnsSubject.onNext(service);
dnssub.onNext(service);
}
}
dnsSubject.onComplete();
dnssub.onComplete();
dnsSubject = null;
probeThread = null;
}
};
t.start();
probeThread = t;
}


Expand Down
15 changes: 11 additions & 4 deletions src/main/java/org/sims/discovery/ws/WsDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.ReplaySubject;
import io.reactivex.subjects.Subject;

import org.hibernate.cfg.annotations.Nullability;
import org.sims.discovery.IDiscoveryService;
import org.sims.discovery.models.IService;

Expand All @@ -35,6 +37,7 @@ public class WsDiscovery implements IDiscoveryService{

private boolean run = false;
private Thread notifyThread;
private Thread probeThread;
private WsSettings settings;
public WsDiscovery(DiscoverySettings settings){
WsDiscoveryConstants.loggerLevel = Level.OFF;
Expand Down Expand Up @@ -137,8 +140,9 @@ public Single<List<IService>> probeServices(){
if(!alive){
return Single.just(new ArrayList<IService>(0));
}
if(wsSubject == null){
wsSubject = ReplaySubject.create();
if(wsSubject == null && probeThread == null){
final Subject<IService> wssub = ReplaySubject.create();
wsSubject = wssub;
try{
//Clear out service inventory
server.getServiceDirectory().clear();
Expand All @@ -148,23 +152,26 @@ public Single<List<IService>> probeServices(){
System.err.println(e);
}
/* Create thread that waits 2 seconds for services to accumelate */

final Thread t = new Thread(){
public void run(){
try{
sleep(2000);

for(WsDiscoveryService service : server.getServiceDirectory().matchAll()){
IService iservice = new WsService(service);
wsSubject.onNext(iservice);
wssub.onNext(iservice);
}
} catch(Exception e){
System.err.println(e);
}
wsSubject.onComplete();
wssub.onComplete();
wsSubject = null;
probeThread = null;
}
};
t.start();
probeThread = t;
}

return wsSubject.buffer(Integer.MAX_VALUE).first(new ArrayList<IService>(0));
Expand Down

0 comments on commit 4ffd667

Please # to comment.