forked from NKI-GCF/Metrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetrixThread.java
More file actions
111 lines (91 loc) · 3.48 KB
/
Copy pathMetrixThread.java
File metadata and controls
111 lines (91 loc) · 3.48 KB
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
// Metrix - A server / client interface for Illumina Sequencing Metrics.
// Copyright (C) 2013 Bernd van der Veen
// This program comes with ABSOLUTELY NO WARRANTY;
// This is free software, and you are welcome to redistribute it
// under certain conditions; for more information please see LICENSE.txt
import java.net.*;
import java.io.*;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.logging.*;
import nki.objects.Command;
import nki.objects.Summary;
import nki.objects.SummaryCollection;
import nki.constants.Constants;
import nki.exceptions.CommandValidityException;
import nki.exceptions.InvalidCredentialsException;
import nki.exceptions.UnimplementedCommandException;
import nki.io.DataStore;
import nki.parsers.metrix.CommandProcessor;
import nki.constants.Constants;
public class MetrixThread extends Thread {
private SocketChannel sChannel = null;
private boolean timedBool = false;
// SERVER LOGGING HERE FOR INSTANTIATION OF CLIENT...
final Logger metrixLogger = Logger.getLogger(MetrixThread.class.getName());
public MetrixThread(SocketChannel sChannel){
super("MetrixThread");
this.sChannel = sChannel;
}
public void run(){
try{
String clientSocketDetails = sChannel.socket().getRemoteSocketAddress().toString();
metrixLogger.log(Level.INFO, "[SERVER] Client connection accepted at: " + clientSocketDetails);
// Create OutputStream for sending objects.
ObjectOutputStream oos = new ObjectOutputStream(sChannel.socket().getOutputStream());
// Cteate Inputstream for receiving objects.
ObjectInputStream ois = new ObjectInputStream(sChannel.socket().getInputStream());
// DataStore instantiation
DataStore ds = new DataStore();
try{
Command commandClient;
while (( commandClient = (Command) ois.readObject()) != null){
String mode = "";
if(commandClient instanceof Command){
mode = commandClient.getMode();
CommandProcessor cp;
try{
// Mode Check
if(mode.equals(Constants.COM_MODE_TIMED)){ // Keep alive repetitive command
timedBool = true;
while(timedBool){
cp = new CommandProcessor(commandClient, oos, ds);
Thread.sleep(commandClient.getTimedInterval());
}
}
if(mode.equals(Constants.COM_MODE_CALL)){ // Single call
cp = new CommandProcessor(commandClient, oos, ds);
}
// Server Exceptions and important logging.
}catch(CommandValidityException CVE){
System.out.println("Command Validity Exception! " + CVE);
}catch(InvalidCredentialsException ICE){
System.out.println("Invalid Credentials Exception! " + ICE);
}finally{
// Close all channels and client streams.
ds = null;
sChannel.socket().close();
sChannel.close();
ois.close();
oos.close();
}
}else{
metrixLogger.log(Level.WARNING, "[SERVER] Command not understood [" + commandClient + "]");
}
metrixLogger.log(Level.INFO, "[SERVER] Finished processing command");
}
}catch(ClassNotFoundException CNFE){
CNFE.printStackTrace();
}catch(Exception Ex){
// metrixLogger.log(Level.INFO, "Disconnect from client. ");
}
}catch(IOException Ex){
System.err.println("[Log] Client disconnected or IOException " + Ex.toString());
}
}
}