001package ball.upnp.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * UPnP/SSDP Implementation Classes
005 * $Id: SSDPDiscoverTask.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-upnp/trunk/src/main/java/ball/upnp/ant/taskdefs/SSDPDiscoverTask.java $
007 * %%
008 * Copyright (C) 2013 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.swing.table.ArrayListTableModel;
024import ball.upnp.ssdp.SSDPDiscoveryCache;
025import ball.upnp.ssdp.SSDPDiscoveryRequest;
026import ball.upnp.ssdp.SSDPDiscoveryThread;
027import ball.upnp.ssdp.SSDPMessage;
028import ball.util.ant.taskdefs.AnnotatedAntTask;
029import ball.util.ant.taskdefs.AntTask;
030import ball.util.ant.taskdefs.ClasspathDelegateAntTask;
031import ball.util.ant.taskdefs.ConfigurableAntTask;
032import lombok.Getter;
033import lombok.NoArgsConstructor;
034import lombok.Setter;
035import lombok.ToString;
036import lombok.experimental.Accessors;
037import org.apache.http.HttpHeaders;
038import org.apache.tools.ant.BuildException;
039import org.apache.tools.ant.Task;
040import org.apache.tools.ant.util.ClasspathUtils;
041
042/**
043 * {@link.uri http://ant.apache.org/ Ant} {@link Task} to run SSDP
044 * discovery.
045 *
046 * {@ant.task}
047 *
048 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
049 * @version $Revision: 5285 $
050 */
051@AntTask("ssdp-discover")
052@NoArgsConstructor @ToString
053public class SSDPDiscoverTask extends Task
054                              implements AnnotatedAntTask,
055                                         ClasspathDelegateAntTask,
056                                         ConfigurableAntTask,
057                                         SSDPDiscoveryThread.Listener {
058    @Getter @Setter @Accessors(chain = true, fluent = true)
059    private ClasspathUtils.Delegate delegate = null;
060    @Getter @Setter
061    private int timeout = 60;
062
063    @Override
064    public void init() throws BuildException {
065        super.init();
066        ClasspathDelegateAntTask.super.init();
067        ConfigurableAntTask.super.init();
068    }
069
070    @Override
071    public void execute() throws BuildException {
072        super.execute();
073        AnnotatedAntTask.super.execute();
074
075        ClassLoader loader = Thread.currentThread().getContextClassLoader();
076
077        try {
078            Thread.currentThread().setContextClassLoader(getClassLoader());
079
080            SSDPDiscoveryCache cache = new SSDPDiscoveryCache();
081            SSDPDiscoveryThread thread = new SSDPDiscoveryThread(getTimeout());
082
083            thread.addListener(this);
084            thread.addListener(cache);
085            thread.start();
086
087            Thread.sleep(getTimeout() * 1000);
088
089            log(new TableModelImpl(cache.values()));
090        } catch (BuildException exception) {
091            throw exception;
092        } catch (Throwable throwable) {
093            throwable.printStackTrace();
094            throw new BuildException(throwable);
095        } finally {
096            Thread.currentThread().setContextClassLoader(loader);
097        }
098    }
099
100    @Override
101    public void sendEvent(SSDPDiscoveryThread thread, SSDPMessage message) {
102        log(String.valueOf(message));
103    }
104
105    @Override
106    public void receiveEvent(SSDPDiscoveryThread thread, SSDPMessage message) {
107        log(String.valueOf(message));
108    }
109
110    private class TableModelImpl
111                  extends ArrayListTableModel<SSDPDiscoveryCache.Value> {
112        private static final long serialVersionUID = 5540353564214745627L;
113
114        public TableModelImpl(Iterable<SSDPDiscoveryCache.Value> iterable) {
115            super(iterable,
116                  SSDPMessage.USN, SSDPMessage.ST,
117                  HttpHeaders.EXPIRES, HttpHeaders.LOCATION);
118        }
119
120        @Override
121        protected Object getValueAt(SSDPDiscoveryCache.Value row, int x) {
122            Object object = null;
123
124            switch (x) {
125            default:
126            case 0:
127                object = row.getSSDPMessage().getUSN();
128                break;
129
130            case 1:
131                object = row.getSSDPMessage().getST();
132                break;
133
134            case 2:
135                object = row.getExpiration();
136                break;
137
138            case 3:
139                object = row.getSSDPMessage().getLocation();
140                break;
141            }
142
143            return object;
144        }
145    }
146}