1 package net.sf.josceleton.connection.impl.osc;
2
3 import net.sf.josceleton.commons.exception.InvalidArgumentException;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 import com.google.inject.Inject;
9 import com.google.inject.assistedinject.Assisted;
10 import com.illposed.osc.OSCListener;
11 import com.illposed.osc.OSCPortIn;
12
13 class OscPortImpl implements OscPort {
14
15 private static final Log LOG = LogFactory.getLog(OscPortImpl.class);
16
17 private final OSCPortIn openedPort;
18
19 private boolean yetClosed = false;
20
21 private boolean yetEstablished = false;
22
23
24
25
26 @Inject OscPortImpl(@Assisted final OSCPortIn openedPort) {
27 this.openedPort = openedPort;
28 }
29
30
31 @Override public final void establish() {
32 if(this.yetEstablished == true) {
33 throw new IllegalStateException("Port was already established!");
34 }
35 this.checkYetClosed();
36 this.yetEstablished = true;
37
38 this.openedPort.startListening();
39 }
40
41
42 @Override public final void addListenerFor(final OscAddress address, final OSCListener listener) {
43 LOG.debug("addListenerForAddress(address=" + address + ", listener=" + listener + ")");
44
45 if (address == null) { throw InvalidArgumentException.newNotNull("address"); }
46 if (listener == null) { throw InvalidArgumentException.newNotNull("listener"); }
47
48 this.checkYetEstablished();
49 this.checkYetClosed();
50
51 this.openedPort.addListener(address.getAddress(), listener);
52 }
53
54
55 @Override public final void close() {
56 this.checkYetEstablished();
57 if(this.yetClosed == true) {
58 LOG.warn("already closed; aborting");
59 return;
60 }
61 this.yetClosed = true;
62
63 this.openedPort.stopListening();
64
65 System.err.println("The following SocketException can be ignored ;)");
66 this.openedPort.close();
67 }
68
69 private void checkYetClosed() {
70 if(this.yetClosed == true) {
71 throw new IllegalStateException("Connection already closed!");
72 }
73 }
74 private void checkYetEstablished() {
75 if(this.yetEstablished == false) {
76 throw new IllegalStateException("Port not yet established!");
77 }
78 }
79
80 @Override public final String toString() {
81 return "OscPortImpl[yetClosed=" + this.yetClosed + "]";
82 }
83
84 }