-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0ClientHandlerThread.java
More file actions
185 lines (170 loc) · 10.1 KB
/
Copy path0ClientHandlerThread.java
File metadata and controls
185 lines (170 loc) · 10.1 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cdvirtualserver;
import java.net.*;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.Date;
import java.util.Vector;
import javax.swing.*;
/**
*
* @author Chayan-Dhaddha
*/
public class ClientHandlerThread extends Thread{
Socket client;
String role,login_id,id,name;
public ClientHandlerThread(Socket client){
this.client=client;
this.start();
}
public void run(){
String query="";
try{
while(true){
ObjectInputStream in=new ObjectInputStream(this.client.getInputStream());
Resource.RequestCodes code=(Resource.RequestCodes)in.readObject();
if(code==Resource.RequestCodes.LOGIN_DETAIL){
this.role=in.readObject().toString();
this.login_id=in.readObject().toString();
String pwd=in.readObject().toString();
query="select * from "+this.role +" where login_id='"+this.login_id+"' and password='"+pwd+"'";
ResultSet rs=ConnectionFactory.getInstance().getResultSet(query);
ObjectOutputStream out=new ObjectOutputStream(this.client.getOutputStream());
if(rs.next()){
ClientInfo info=new ClientInfo();
info.id=rs.getString("id");
this.id=info.id;
info.client=this.client;
info.name=rs.getString("name");
this.name=info.name;
info.role=this.role;
CommResource.loggedInClient.add(info);
out.writeObject("Success");
String str="\n" + this.role + "(" + this.name + "): Logged in on " + this.client.getInetAddress().getHostAddress() + " at " + new java.util.Date()/*Calendar.getInstance().toString()*/;
CommResource.admin.panel_server.txt_area.append(str);
}else{
out.writeObject("Failed");
}
}
else if(code==Resource.RequestCodes.STUDENT_REGISTER){
this.login_id=in.readObject().toString();
String pwd=in.readObject().toString();
String name=in.readObject().toString();
String branch=in.readObject().toString();
String semester=in.readObject().toString();
String contact=in.readObject().toString();
query="insert into student set "+
"login_id='"+this.login_id+"',"+
"password='"+pwd+"',"+
"name='"+name+"',"+
"branch='"+branch+"',"+
"semester='"+semester+"',"+
"contact='"+contact+"'";
int n=ConnectionFactory.getInstance().setData(query);
ObjectOutputStream out= new ObjectOutputStream(this.client.getOutputStream());
if(n>0){
out.writeObject("Success");
}else{
out.writeObject("Failed");
}
}
else if(code==Resource.RequestCodes.INIT_UPCOMING_SESSION_MENTOR){
query="select initiated_by,topic,session_date,session_time,duration from session where status=0";
Vector<Vector> list=ConnectionFactory.getInstance().getData(query);
ObjectOutputStream out= new ObjectOutputStream(this.client.getOutputStream());
out.writeObject(Resource.ResponseCodes.INIT_MENTOR_RESPONSE);
out.writeObject(list);
}
else if(code==Resource.RequestCodes.INIT_UPCOMING_SESSION_STUDENT){
query="select initiated_by,topic,session_date,session_time,duration from session where status=0";
Vector<Vector> list=ConnectionFactory.getInstance().getData(query);
ObjectOutputStream out= new ObjectOutputStream(this.client.getOutputStream());
out.writeObject(Resource.ResponseCodes.INIT_STUDENT_RESPONSE);
out.writeObject(list);
}
else if(code==Resource.RequestCodes.SESSION_REGISTER){
String topic=in.readObject().toString();
String session_date=in.readObject().toString();
String session_time=in.readObject().toString();
String duration=in.readObject().toString();
String desc=in.readObject().toString();
String status="0";
String initiated_on=new Date().toString();
//DateFormat ddf = new SimpleDateFormat("yyyy/MM/dd");
//Date date=ddf.parse(session_date);
query="insert into session set "+
"initiated_by="+this.id+","+
"topic='"+topic+"',"+
"session_date='"+session_date+"',"+
"session_time='"+session_time+"',"+
"duration='"+duration+"',"+
"description='"+desc+"',"+
"initiated_on='"+initiated_on+"',"+
"status="+status;
int n=ConnectionFactory.getInstance().setData(query);
ObjectOutputStream out= new ObjectOutputStream(this.client.getOutputStream());
if(n>0){
out.writeObject(Resource.ResponseCodes.SESSION_REGISTER_SUCCESS);
}else{
out.writeObject(Resource.ResponseCodes.SESSION_REGISTER_FAILED);
}
query="select initiated_by,topic,session_date,session_time,duration from session where status=0";
Vector<Vector> list=ConnectionFactory.getInstance().getData(query);
for(int i=0;i<CommResource.loggedInClient.size();i++){
ObjectOutputStream tmp=new ObjectOutputStream(CommResource.loggedInClient.elementAt(i).client.getOutputStream());
tmp.writeObject(Resource.ResponseCodes.NEW_SESSION);
tmp.writeObject(list);
}
}
else if(code==Resource.RequestCodes.FILTER_PAST_SESSION){
Resource.RequestCodes req=(Resource.RequestCodes)in.readObject();
if(req==Resource.RequestCodes.FILTER_DATE){
String date_from=in.readObject().toString();
String date_to=in.readObject().toString();
query="select initiated_by,topic,session_date,session_time,duration from session where session_date>='"+date_from+"' and session_date<='"+date_to+"'";
Vector<Vector>list=ConnectionFactory.getInstance().getData(query);
ObjectOutputStream out=new ObjectOutputStream(this.client.getOutputStream());
out.writeObject(Resource.ResponseCodes.FILTER_SUCCESS);
out.writeObject(list);
}
else if(req==Resource.RequestCodes.FILTER_SEARCH){
String search=in.readObject().toString();
query="select initiated_by,topic,session_date,session_time,duration from session where topic='"+search+"'";
Vector<Vector>list=ConnectionFactory.getInstance().getData(query);
ObjectOutputStream out=new ObjectOutputStream(this.client.getOutputStream());
out.writeObject(Resource.ResponseCodes.FILTER_SUCCESS);
out.writeObject(list);
System.out.println("in filter_search");
}
else if(req==Resource.RequestCodes.FILTER_SESSION){
String session=in.readObject().toString();
query="select initiated_by,topic,session_date,session_time,duration from session where initiated_by='"+session+"'";
Vector<Vector>list=ConnectionFactory.getInstance().getData(query);
ObjectOutputStream out=new ObjectOutputStream(this.client.getOutputStream());
out.writeObject(Resource.ResponseCodes.FILTER_SUCCESS);
out.writeObject(list);
System.out.println("in filter session");
}
}
else if(code==Resource.RequestCodes.LOGOUT){
for(int i=0;i<CommResource.loggedInClient.size();i++){
if(CommResource.loggedInClient.elementAt(i).client.equals(this.client)){
CommResource.loggedInClient.remove(i);
CommResource.admin.panel_server.txt_area.append("\n" + this.name + "(" + this.role + ")" + " Logged out (" + new java.util.Date() + ")");
break;
}
}
break;
}
}
}catch(Exception ex){
JOptionPane.showMessageDialog(null,"Error on client handler thread : " + ex);
ex.printStackTrace();
}
}
}