001: package vicazh.hyperpool.stream.net.http.html;
002:
003: import java.util.*;
004: import java.io.*;
005: import vicazh.hyperpool.stream.net.http.*;
006: import vicazh.hyperpool.stream.Stream;
007: import vicazh.hyperpool.stream.Connection;
008:
009: public class Task extends Service implements Serializable {
010: int id;
011:
012: public boolean equals(Object obj) {
013: return id == ((Task) obj).id;
014: }
015:
016: transient private TaskService service;
017:
018: private String address;
019:
020: public String getAddress() {
021: return address;
022: }
023:
024: public void setAddress(String address) {
025: this .address = address;
026: }
027:
028: private int level;
029:
030: public int getLevel() {
031: return level;
032: }
033:
034: public void setLevel(int level) {
035: this .level = level;
036: }
037:
038: private String start;
039:
040: public String getStart() {
041: return start;
042: }
043:
044: public void setStart(String start) {
045: this .start = start;
046: }
047:
048: private int threads;
049:
050: public int getThreads() {
051: return threads;
052: }
053:
054: public void setThreads(int threads) {
055: this .threads = threads;
056: }
057:
058: private String dir;
059:
060: public String getDir() {
061: return dir;
062: }
063:
064: private String name;
065:
066: public void setDir(String dir) {
067: this .dir = dir;
068: name = new File(dir2path()).getName();
069: }
070:
071: public String dir2path() {
072: return dir.replace('/', File.separatorChar);
073: }
074:
075: public void path2dir(String dir) {
076: setDir(dir.replace(File.separatorChar, '/'));
077: }
078:
079: private boolean autorun;
080:
081: public boolean getAutorun() {
082: return autorun;
083: }
084:
085: public void setAutorun(boolean autorun) {
086: this .autorun = autorun;
087: }
088:
089: enum Status {
090: READY, RUN, STOP, BREAK
091: };
092:
093: Status status = Status.READY;
094:
095: int index;
096:
097: int size;
098:
099: transient private List<String> list;
100:
101: transient private List<Integer> list2;
102:
103: void go() throws IOException {
104: index = 0;
105: list.clear();
106: list2.clear();
107: status = Status.RUN;
108: ExplorerService s = new ExplorerService(0, service.writer,
109: service.cache, service.base);
110: s.setDir(dir);
111: s.setSize(Long.MAX_VALUE);
112: s.setMax(Long.MAX_VALUE);
113: s.setElement(service.getElement());
114: setElement(new CacheService(service.reader, service.cache,
115: service.base));
116: ((CacheService) getElement()).setDir(dir);
117: ((CacheService) getElement()).setElement(s);
118: list.add(address);
119: list2.add(new Integer(0));
120: size = 1;
121: next();
122: }
123:
124: synchronized private void next() throws IOException {
125: TaskConnection connection = (TaskConnection) ((Stream) get(null)).connection;
126: connection.address = list.get(index);
127: connection.level = list2.get(index++).intValue();
128: new Thread(connection).start();
129: }
130:
131: synchronized void add(String address, int level) throws IOException {
132: if (status != Status.RUN || list.contains(address))
133: return;
134: list.add(address);
135: list2.add(new Integer(level));
136: size++;
137: if (getConnections().size() < threads)
138: next();
139: }
140:
141: synchronized void action(TaskConnection connection)
142: throws IOException {
143: if (status == Status.RUN && index < list.size()) {
144: connection.address = list.get(index);
145: connection.level = list2.get(index++).intValue();
146: new Thread(connection).start();
147: } else
148: connection.getClient().close();
149: }
150:
151: synchronized public void remove(Connection connection) {
152: super .remove(connection);
153: if (status == Status.RUN && index < list.size())
154: try {
155: next();
156: } catch (IOException e) {
157: }
158: else if (getConnections().size() == 0)
159: status = Status.READY;
160: }
161:
162: public Connection getConnection() {
163: return new TaskConnection(this );
164: }
165:
166: synchronized void cancel() {
167: status = Status.STOP;
168: }
169:
170: synchronized void close() {
171: status = Status.BREAK;
172: while (getConnections().size() > 0)
173: try {
174: getConnections().get(0).close();
175: } catch (Exception e) {
176: }
177: }
178:
179: public String toString() {
180: return name;
181: }
182:
183: void set(TaskService service) {
184: this .service = service;
185: id = hashCode();
186: list = new ArrayList<String>();
187: list2 = new ArrayList<Integer>();
188: }
189: }
|