My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package org.jogre.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.ConcurrentModificationException;

import nanoxml.XMLElement;
import nanoxml.XMLParseException;

import org.jogre.common.comm.ITransmittable;
import org.jogre.common.util.JogreLogger;

/**
* Represents client/server communication over a socket object
* @author jens
*
*/
public class SocketBasedMessageBus implements MessageBus {

/** Logging */
private final JogreLogger logger = new JogreLogger (this.getClass());

/** When the boolean loop becomes false the Thread finishes. */
private boolean loop = true;

private final BufferedReader in;
private final PrintStream out;
private final Socket socket;

private Thread executingThread;

public SocketBasedMessageBus(final Socket socket) {
try {
if (socket != null) {
this.socket = socket;
in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
out = new PrintStream (socket.getOutputStream());
} else {
throw new NullPointerException("thread must not be null");
}
} catch (IOException e) {
throw new IllegalArgumentException("Could not connect to socket", e);
}
}

private void doLoop(final MessageParser parser) {
logger.debug("run", "Staring thread.");

try {
while (loop) {
// listen for input from the user
String inString = "";

while (loop && inString!=null && inString.equals("")) {
if (in == null) {
parser.cleanup ();
return;
}
inString = in.readLine();
}

// Check input.
if (in == null) {
parser.cleanup ();
return;
}

// Ensure that the communication is XML (starts with a '<' character)
// as any sort of client could send communication to the server.
if (inString != null) {
if (inString.startsWith("<")) {

// Starts with an '<' so try and parse this XML
XMLElement message = new XMLElement ();

try {
message.parseString (inString);

// parse this element
if (message != null)
parser.parse(message);
}
catch (XMLParseException xmlParseEx) {
logger.error ("run", "problem parsing: " + inString);
logger.stacktrace (xmlParseEx);
}
}
}
}
}
catch (SocketException sEx) {
logger.debug ("run", "Connection lost");
logger.stacktrace (sEx);
}
catch (IOException ioEx) {
logger.error ("run", "IO Exception: ");
logger.stacktrace (ioEx);
}
catch (Throwable genEx) {
genEx.printStackTrace();
logger.error ("run", "General Exception: ");
logger.stacktrace (genEx);
}
try {
parser.cleanup ();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
logger.error ("run", "Could not close socket: ");
logger.stacktrace (e);
}
}
}

/* (non-Javadoc)
* @see org.jogre.common.MessageBus#send(org.jogre.common.comm.ITransmittable)
*/
public void send (ITransmittable transObject) {
// Retrieve XMLElement from the object and flatten to a String.
String message = transObject.flatten().toString();

// Send down the socket to the receiving end
out.println (message);
}

/* (non-Javadoc)
* @see org.jogre.common.MessageBus#close()
*/
public void close () {
this.loop = false;
}

/* (non-Javadoc)
* @see org.jogre.common.MessageBus#open(org.jogre.common.AbstractConnectionThread)
*/
public void open(final MessageParser parser) {
if (parser == null) {
throw new NullPointerException("parser must not be null");
}
if (executingThread != null) {
throw new ConcurrentModificationException("Cannot call startLoop more than once!");
}
executingThread = new Thread() {
@Override
public void run() {
doLoop(parser);
}
};
executingThread.start();
}
}
Show details Hide details

Change log

r15 by schefflerjens on May 20, 2009   Diff
Refactoring: extract interface from
AbstractConnectionThread
(MessageBus.MessageParser).

Go to: 
Project members, sign in to write a code review

Older revisions

r14 by schefflerjens on May 20, 2009   Diff
refactoring: pushed down MessageBus
creation into subclasses of
AbstractConnectionThread. The base
class is not completely
unaware of sockets.
r13 by schefflerjens on May 20, 2009   Diff
refactoring: extract interface
"MessageBus"
r12 by schefflerjens on May 20, 2009   Diff
refactoring: method renames in
SocketBasedMessageBus (final names
before interface gets extracted)
All revisions of this file

File info

Size: 4305 bytes, 157 lines
Hosted by Google Code