View Javadoc

1   package net.sf.josceleton.connection.impl;
2   
3   import java.util.Date;
4   
5   import net.sf.josceleton.connection.api.ConnectionListener;
6   import net.sf.josceleton.connection.api.service.user.UserService;
7   import net.sf.josceleton.connection.impl.osc.OscMessageTransformer;
8   import net.sf.josceleton.connection.impl.osc.OscPort;
9   import net.sf.josceleton.connection.impl.service.user.UserServiceInternal;
10  import net.sf.josceleton.core.api.entity.message.JointMessage;
11  import net.sf.josceleton.core.api.entity.message.UserMessage;
12  import net.sf.josceleton.core.impl.async.CloseableAndAsyncSkeleton;
13  
14  import com.google.inject.Inject;
15  import com.google.inject.assistedinject.Assisted;
16  import com.illposed.osc.OSCMessage;
17  
18  class ConnectionImpl
19  	extends CloseableAndAsyncSkeleton<ConnectionListener>
20  	implements ConnectionInternal, OscMessageAddressRouterCallback {
21  	
22  	private final OscPort oscPort;
23  	
24  	private final OscMessageAddressRouter addressRouter;
25  	
26  	private final OscMessageTransformer transformer;
27  	
28  	private final UserServiceInternal userService;
29  	
30  	/** state-full property */
31  	private boolean yetEstablished = false;
32  	
33  	
34  	@Inject ConnectionImpl(
35  			@Assisted final OscPort oscPort,
36  			final OscMessageAddressRouter addressRouter,
37  			final OscMessageTransformer transformer,
38  			final UserServiceInternal userService
39  			) {
40  		this.oscPort = oscPort;
41  		this.addressRouter = addressRouter;
42  		this.transformer = transformer;
43  		this.userService = userService;
44  	}
45  	
46  	/** {@inheritDoc} from {@link ConnectionInternal} */
47  	@Override public final void establish() {
48  		// NO: this.checkYetClosed(); ... impossible invariant
49  		if(this.yetEstablished == true) {
50  			throw new IllegalStateException("Connection was already established!");
51  		}
52  		this.yetEstablished = true;
53  		
54  		this.oscPort.establish();
55  		this.addressRouter.reroute(this.oscPort, this);
56  	}
57  	
58  	/** {@inheritDoc} from {@link CloseableAndAsyncSkeleton} */
59  	@Override protected final void prepareToClose() {
60  		// NO: assert(yetClosed == false) ... already done by skeleton
61  		// assert(yetEstablished == true);
62  		
63  		// if(userService != null) removeListener(userService) ?
64  		// or maybe just removeAllListeners()... no, would have no effect,
65  		// as OscPort will not dispatch anything anymore (at, least ... should not ;)
66  		
67  		// TODO ??? remove all listeners when connection closed? yes, i think so...
68  		// 		then we have to loosen conditions for removeListener(x) whereas x not added is listener;
69  		// 		actually nothing bad here, as postconditions is on method begin already fullfilled => perfectly fine
70  		
71  		this.oscPort.close();
72  	}
73  
74  	/** {@inheritDoc} from {@link OscMessageAddressRouterCallback} */
75  	@Override public final void onAcceptedJointMessage(final Date date, final OSCMessage oscMessage) {
76  		// assert(yetClosed == false); && assert(yetEstablished == true);
77  		
78  		// TODO @EXCEPTION CODE if we are receiving an malformed oscMessage => catch it!
79  		//  and CHAIN with proper exception (message: "Received malformed bla")
80  		// try {
81  		final JointMessage message = this.transformer.transformJointMessage(oscMessage, this.userService);
82  		// } catch(...Exception ex) {
83  		//   throw new WrappingException("Could not transform malformed joint message " + oscMessage, ex);
84  		// }
85  		for (final ConnectionListener currentListener : this.getListeners()) {
86  			currentListener.onJointMessage(message);
87  		}
88  	}
89  
90  	/** {@inheritDoc} from {@link OscMessageAddressRouterCallback} */
91  	@Override public final void onAcceptedUserMessage(final Date date, final OSCMessage oscMessage) {
92  		// assert(yetClosed == false); && assert(yetEstablished == true);
93  		final UserMessage message = this.transformer.transformUserMessage(oscMessage, this.userService);
94  		
95  		for (final ConnectionListener currentListener : this.getListeners()) {
96  			currentListener.onUserMessage(message);
97  		}
98  	}
99  	
100 	/** {@inheritDoc} from {@link Connection} */
101 	@Override public final UserService getUserService() {
102 		return this.userService;
103 	}
104 	
105 	@Override public final String toString() {
106 		return "ConnectionImpl[yetClosed=" + this.isYetClosed() + ", oscPort=" + this.oscPort + "]";
107 	}
108 	
109 }