|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ServerProcess.java | 16,7% | 50% | 62,5% | 45,7% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Replica is published under the terms
|
|
| 3 |
* of the Apache Software License.
|
|
| 4 |
*/
|
|
| 5 |
package replica.server;
|
|
| 6 |
|
|
| 7 |
import java.io.BufferedReader;
|
|
| 8 |
import java.io.BufferedWriter;
|
|
| 9 |
import java.io.IOException;
|
|
| 10 |
import java.io.InputStream;
|
|
| 11 |
import java.io.InputStreamReader;
|
|
| 12 |
import java.io.OutputStreamWriter;
|
|
| 13 |
import java.net.Socket;
|
|
| 14 |
import java.util.logging.Level;
|
|
| 15 |
import java.util.logging.Logger;
|
|
| 16 |
|
|
| 17 |
import replica.command.*;
|
|
| 18 |
import replica.core.ApplicationMessage;
|
|
| 19 |
import replica.session.SessionManager;
|
|
| 20 |
|
|
| 21 |
/**
|
|
| 22 |
* @author Pedro Costa
|
|
| 23 |
* @author Helder Silva
|
|
| 24 |
* @since 15/Jan/2004
|
|
| 25 |
*/
|
|
| 26 |
public class ServerProcess implements Runnable { |
|
| 27 |
|
|
| 28 |
static Logger logger = Logger.getLogger(ServerProcess.class.getName()); |
|
| 29 |
|
|
| 30 |
private Socket socket;
|
|
| 31 |
private boolean alive = true; |
|
| 32 |
private CommandProcessor commandProcessor;
|
|
| 33 |
private SessionManager sessionManager;
|
|
| 34 |
private BufferedWriter writer;
|
|
| 35 |
|
|
| 36 |
String sessionId; |
|
| 37 |
|
|
| 38 |
private final static char RETURN = '\r'; |
|
| 39 |
private final static char NEW_LINE = '\n'; |
|
| 40 |
private final static char SPACE = ' '; |
|
| 41 |
private char endOfCommand = ';'; |
|
| 42 |
|
|
| 43 |
/**
|
|
| 44 |
* Default constructor.
|
|
| 45 |
*
|
|
| 46 |
*/
|
|
| 47 | 5 |
public ServerProcess(){
|
| 48 | 5 |
super();
|
| 49 |
} |
|
| 50 |
|
|
| 51 |
/**
|
|
| 52 |
* Start a new session connected to the process.
|
|
| 53 |
*
|
|
| 54 |
*/
|
|
| 55 | 2 |
void initSession(){
|
| 56 | 2 |
sessionId = sessionManager.createSession(null, null); |
| 57 | 2 |
sessionManager.getSession(sessionId).putObject("SERVER_PROCESS", this); |
| 58 |
|
|
| 59 | 2 |
logger.log(Level.INFO,"Created session id:[" + sessionId + "] for " + remoteAddressToString() + "."); |
| 60 |
} |
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* Constructor that receives the socket to be used by this server process.
|
|
| 64 |
*
|
|
| 65 |
* @param socket
|
|
| 66 |
*/
|
|
| 67 | 0 |
public ServerProcess( Socket socket ){
|
| 68 | 0 |
this();
|
| 69 | 0 |
setSocket( socket ); |
| 70 |
} |
|
| 71 |
|
|
| 72 |
/* (non-Javadoc)
|
|
| 73 |
* @see java.lang.Runnable#run()
|
|
| 74 |
*/
|
|
| 75 | 2 |
public void run() { |
| 76 | 2 |
try{
|
| 77 | 2 |
initSession(); |
| 78 | 2 |
processStream( getSocket().getInputStream() ); |
| 79 |
} |
|
| 80 |
catch(IOException e){
|
|
| 81 | 0 |
e.printStackTrace(); |
| 82 |
} |
|
| 83 |
finally{
|
|
| 84 | 2 |
logger.log(Level.INFO,"Lost connection to " + remoteAddressToString() + "."); |
| 85 |
|
|
| 86 | 2 |
if( getSocket() != null ){ |
| 87 | 2 |
try{ getSocket().close(); }catch(IOException e){ } |
| 88 |
} |
|
| 89 | 2 |
destroySession(); |
| 90 |
} |
|
| 91 |
} |
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* Process commands from the given input stream.
|
|
| 95 |
*/
|
|
| 96 | 2 |
void processStream(InputStream stream) throws IOException{ |
| 97 |
|
|
| 98 | 2 |
BufferedReader reader = null;
|
| 99 |
|
|
| 100 | 2 |
try{
|
| 101 |
|
|
| 102 | 2 |
reader = new BufferedReader( new InputStreamReader( stream ) ); |
| 103 |
|
|
| 104 | 2 |
int read = -1;
|
| 105 | 2 |
StringBuffer buffer = new StringBuffer();
|
| 106 | ? |
while( ( read = reader.read() ) != -1 ){
|
| 107 | 0 |
if( read != NEW_LINE && read != RETURN ){
|
| 108 | 0 |
if( read == endOfCommand ){
|
| 109 | 0 |
buffer.append(SPACE).append((char)read);
|
| 110 | 0 |
logger.info("Received from "
|
| 111 |
+remoteAddressToString()+" msg ["
|
|
| 112 |
+buffer.toString()+"].");
|
|
| 113 | 0 |
ApplicationMessage msg = new ApplicationMessage(
|
| 114 |
sessionId, buffer.toString() ); |
|
| 115 | 0 |
msg.setOriginator(ApplicationMessage.SERVER_MODULE); |
| 116 | 0 |
Object res = getCommandProcessor().process( msg ); |
| 117 | 0 |
if( res != null && res.toString().trim().length() > 0 ) |
| 118 | 0 |
sendResponse( res.toString() ); |
| 119 | 0 |
buffer = new StringBuffer();
|
| 120 |
} |
|
| 121 |
else{
|
|
| 122 | 0 |
buffer.append((char)read);
|
| 123 |
} |
|
| 124 |
} |
|
| 125 |
else{
|
|
| 126 | 0 |
buffer.append( SPACE ); |
| 127 |
} |
|
| 128 |
} |
|
| 129 |
|
|
| 130 |
// Connection Lost
|
|
| 131 |
} |
|
| 132 |
finally{
|
|
| 133 | 2 |
if( reader != null ){ |
| 134 | 2 |
try{ reader.close(); }catch(Throwable t){ } |
| 135 |
} |
|
| 136 |
} |
|
| 137 |
} |
|
| 138 |
|
|
| 139 | 0 |
public void sendResponse(String response){ |
| 140 |
|
|
| 141 | 0 |
logger.info("Received response : [" + response + "]."); |
| 142 |
|
|
| 143 | 0 |
try{
|
| 144 | 0 |
if( writer == null ) |
| 145 | 0 |
writer = new BufferedWriter( new OutputStreamWriter( |
| 146 |
getSocket().getOutputStream() ) ); |
|
| 147 |
|
|
| 148 | 0 |
writer.write(response); |
| 149 | 0 |
if(!response.endsWith("\n")); |
| 150 | 0 |
writer.write("\n");
|
| 151 | 0 |
writer.flush(); |
| 152 |
} |
|
| 153 |
catch(IOException e){
|
|
| 154 | 0 |
logger.log( |
| 155 |
Level.SEVERE, "Problem sending message to user. msg:["+response+"], addr:" + remoteAddressToString(), e); |
|
| 156 |
} |
|
| 157 |
} |
|
| 158 |
|
|
| 159 | 2 |
public void destroySession(){ |
| 160 | 2 |
sessionManager.destroySession(sessionId); |
| 161 | 2 |
logger.log(Level.INFO,"Removed session id:[" + sessionId + "] for " + remoteAddressToString() + "."); |
| 162 |
} |
|
| 163 |
|
|
| 164 |
/**
|
|
| 165 |
* @return
|
|
| 166 |
*/
|
|
| 167 | 25 |
public Socket getSocket() {
|
| 168 | 25 |
return socket;
|
| 169 |
} |
|
| 170 |
|
|
| 171 |
/**
|
|
| 172 |
* @param socket
|
|
| 173 |
*/
|
|
| 174 | 3 |
public void setSocket(Socket socket) { |
| 175 | 3 |
this.socket = socket;
|
| 176 |
} |
|
| 177 |
|
|
| 178 |
/**
|
|
| 179 |
* @return
|
|
| 180 |
*/
|
|
| 181 | 0 |
public synchronized boolean isAlive() { |
| 182 | 0 |
return alive;
|
| 183 |
} |
|
| 184 |
|
|
| 185 |
/**
|
|
| 186 |
* @param b
|
|
| 187 |
*/
|
|
| 188 | 0 |
public synchronized void setAlive(boolean b) { |
| 189 | 0 |
alive = b; |
| 190 |
} |
|
| 191 |
/**
|
|
| 192 |
* @return
|
|
| 193 |
*/
|
|
| 194 | 0 |
public CommandProcessor getCommandProcessor() {
|
| 195 | 0 |
return commandProcessor;
|
| 196 |
} |
|
| 197 |
|
|
| 198 |
/**
|
|
| 199 |
* @param processor
|
|
| 200 |
*/
|
|
| 201 | 3 |
public void setCommandProcessor(CommandProcessor processor) { |
| 202 | 3 |
commandProcessor = processor; |
| 203 |
} |
|
| 204 |
|
|
| 205 |
/**
|
|
| 206 |
* Returns the connected remote address as a string.
|
|
| 207 |
* Mostly used for logging.
|
|
| 208 |
* @return
|
|
| 209 |
*/
|
|
| 210 | 6 |
private String remoteAddressToString(){
|
| 211 | 6 |
if(getSocket() == null) |
| 212 | 0 |
return "[]"; |
| 213 |
|
|
| 214 | 6 |
return "[" + getSocket().getInetAddress() + ":" + getSocket().getPort() + "]"; |
| 215 |
} |
|
| 216 |
|
|
| 217 |
/**
|
|
| 218 |
* @return
|
|
| 219 |
*/
|
|
| 220 | 0 |
public SessionManager getSessionManager() {
|
| 221 | 0 |
return sessionManager;
|
| 222 |
} |
|
| 223 |
|
|
| 224 |
/**
|
|
| 225 |
* @param manager
|
|
| 226 |
*/
|
|
| 227 | 3 |
public void setSessionManager(SessionManager manager) { |
| 228 | 3 |
sessionManager = manager; |
| 229 |
} |
|
| 230 |
|
|
| 231 |
} |
|
| 232 |
|
|
||||||||||