1 package org.indi.client;
2
3 import java.io.IOException;
4 import java.nio.ByteBuffer;
5 import java.nio.channels.ClosedChannelException;
6 import java.nio.channels.SelectableChannel;
7 import java.nio.channels.SelectionKey;
8 import java.util.Date;
9 import java.util.Queue;
10 import java.util.concurrent.LinkedBlockingQueue;
11
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.indi.reactor.Connector;
15 import org.indi.reactor.EventHandler;
16 import org.indi.reactor.EventHandlerFactory;
17 import org.indi.reactor.Reactor;
18 import org.xml.sax.SAXException;
19
20 public class IndiClient {
21 protected Dispatcher dispatcher;
22 protected Reactor reactor;
23 Queue<Object> toClientQueue;
24
25 public IndiClient(Reactor r) throws IOException {
26 this.reactor = r;
27 this.toClientQueue = new LinkedBlockingQueue<Object>();
28 this.dispatcher = new Dispatcher(this.toClientQueue);
29 this.reactor.register(this.dispatcher);
30 new Connector(this.reactor, new EventHandlerFactory() {
31 public EventHandler produce(Reactor r, SelectableChannel ch)
32 throws IOException {
33 try {
34 return new ServerHandler(r, ch);
35 } catch (ParserConfigurationException e) {
36
37 e.printStackTrace();
38 } catch (SAXException e) {
39
40 e.printStackTrace();
41 }
42 return null;
43 }
44 }, "localhost", 7624) {
45 @Override
46 public void onConnected(EventHandler h)
47 throws ClosedChannelException, IOException {
48 ((ServerHandler) h).setQueue(IndiClient.this.toClientQueue);
49 h.register(SelectionKey.OP_READ);
50 ByteBuffer output = ByteBuffer.allocate(1000);
51 output.put("<getProperties version='1.5'/>".getBytes());
52 output.flip();
53 ((ServerHandler) h).write(output);
54 }
55 };
56 }
57
58
59
60
61
62 public static void main(String[] args) throws IOException {
63
64 System.out.println("Hello World");
65 Reactor r = new Reactor();
66 IndiClient ic = new IndiClient(r);
67 long time = (new Date()).getTime();
68 while ((new Date()).getTime() - time < 4000) {
69 r.handleEvents(10);
70 }
71 }
72 }