1 package cz.cuni.amis.pogamut.base.communication.connection.impl.socket;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.OutputStreamWriter;
6 import java.io.Reader;
7 import java.io.Writer;
8 import java.net.InetSocketAddress;
9 import java.net.Socket;
10 import java.nio.charset.Charset;
11
12 import com.google.inject.Inject;
13 import com.google.inject.name.Named;
14
15 import cz.cuni.amis.pogamut.base.communication.connection.exception.ConnectionException;
16 import cz.cuni.amis.pogamut.base.communication.connection.impl.AbstractConnection;
17 import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
18 import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
19 import cz.cuni.amis.pogamut.base.utils.DefaultPogamutPlatform;
20 import cz.cuni.amis.pogamut.base.utils.Pogamut;
21 import cz.cuni.amis.pogamut.base.utils.PogamutPlatform;
22 import cz.cuni.amis.pogamut.base.utils.PogamutPlatformProxy;
23 import cz.cuni.amis.pogamut.base.utils.PogamutProperty;
24 import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
25 import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
26 import java.util.logging.Level;
27
28 @AgentScoped
29 public class SocketConnection extends AbstractConnection<ISocketConnectionAddress> {
30
31 public static final String CONNECTION_DEPENDENCY = "ConnectionDependency";
32 public static final String CONNECTION_ADDRESS_DEPENDENCY = "ConnectionAddressDependency";
33
34 private Socket socket = null;
35
36 private InputStreamReader socketReader = null;
37
38 private OutputStreamWriter socketWriter = null;
39
40 private Charset encoding;
41
42
43
44 @Inject
45 public SocketConnection(@Named(CONNECTION_ADDRESS_DEPENDENCY) ISocketConnectionAddress address, @Named(CONNECTION_DEPENDENCY) ComponentDependencies dependencies, IComponentBus bus, IAgentLogger logger) {
46 super(address, dependencies, bus, logger);
47 initProperties();
48 }
49
50 public SocketConnection(ComponentDependencies dependencies, IComponentBus bus, IAgentLogger logger) {
51 super(dependencies, bus, logger);
52 initProperties();
53 }
54
55 private void initProperties() {
56 String encoding = Pogamut.getPlatform().getProperty(PogamutProperty.POGAMUT_SOCKETCONNECTION_ENCODING.getKey());
57 if (encoding == null) {
58 log.warning("Missing property: " + PogamutProperty.POGAMUT_SOCKETCONNECTION_ENCODING.getKey() + ", using default encoding 'default'");
59 encoding = "default";
60 }
61 if (encoding.equals("default")) {
62 encoding = Charset.defaultCharset().name();
63 }
64
65 log.info("Using encoding: " + encoding);
66 this.encoding = Charset.forName(encoding);
67
68
69
70
71
72
73 }
74
75 @Override
76 protected Reader getConnectionReader() throws ConnectionException {
77 return socketReader;
78 }
79
80 @Override
81 protected Writer getConnectionWriter() throws ConnectionException {
82 return socketWriter;
83 }
84
85 @Override
86 protected void unsyncClose() {
87 if (socket != null) {
88 try {
89 socket.close();
90 } catch (Exception e) {
91 if (log.isLoggable(Level.SEVERE)) log.severe("Can't close socket - " + e.getMessage());
92 }
93 try {
94 socketReader.close();
95 } catch (Exception e) {
96 if (log.isLoggable(Level.SEVERE)) log.severe("Can't close socket reader - " + e.getMessage());
97 }
98 try {
99 socketWriter.close();
100 } catch (Exception e) {
101 if (log.isLoggable(Level.SEVERE)) log.severe("Can't close socket writer - " + e.getMessage());
102 }
103 socket = null;
104 }
105 }
106
107 @Override
108 protected void unsyncConnect(ISocketConnectionAddress address) throws ConnectionException {
109 log.info("Using encoding: " + encoding);
110 socket = new Socket();
111 try {
112 socket.connect(new InetSocketAddress(address.getHost(), address.getPort()));
113 socketReader = new InputStreamReader(socket.getInputStream(), encoding);
114 socketWriter = new OutputStreamWriter(socket.getOutputStream(), encoding);
115 } catch (IOException e) {
116 throw new ConnectionException(e + " (" + address.getHost() + ":" + address.getPort() + ")", log);
117 }
118 }
119
120 @Override
121 public String toString() {
122 return "SocketConnection["+String.valueOf(address)+",connected:"+(controller == null ? "false" : controller.isRunning())+")";
123
124 }
125
126 }