-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentsDataResponder.cs
117 lines (88 loc) · 3.98 KB
/
StudentsDataResponder.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Web;
namespace StudentsServerWeb
{
class StudentsDataResponder:AbstractResponder
{
private SqlHelper _helper;
private const string REQ_SEND_CLASS_LIST = "sndclslt";
private const string REQ_SEND_STUD_LIST = "sndstdlt";
private const string CMD_START_CLASSLIST = "stclassl";
private const string CMD_END_LIST = "end_list";
private const string CMD_START_STUDLIST = "ststudlt";
private const string CMD_SEND_CLASS_ID = "sndclsid";
public StudentsDataResponder(SqlHelper helper) {
this._helper = helper;
}
public override void Respond(TcpClient client, NetworkStream stream, byte[] message)
{
string msgIncoming = Encoding.ASCII.GetString(message, 0, message.Length);
System.Diagnostics.Debug.WriteLine("RECEIVED//" + msgIncoming + "//");
switch (msgIncoming)
{
case REQ_SEND_CLASS_LIST:
List<Class> classList = _helper.GetClasses();
SendData(classList, stream);
break;
case REQ_SEND_STUD_LIST:
System.Diagnostics.Debug.WriteLine("Requesting class ID");
string msgToSend = CMD_SEND_CLASS_ID;
byte[] bytesToSend = Encoding.ASCII.GetBytes(msgToSend);
stream.Write(bytesToSend, 0, bytesToSend.Length);
message = new byte[5];
stream.Read(message, 0, message.Length);
msgIncoming = Encoding.ASCII.GetString(message, 0, message.Length).Trim();
System.Diagnostics.Debug.WriteLine("RECEIVED//" + msgIncoming + "//");
List<Student> studentsList = _helper.GetStudentsInClass(new Class(Convert.ToInt32(msgIncoming), ""));
SendData(studentsList, stream);
break;
}
}
private void SendData(List<Student> studentsList, NetworkStream stream)
{
string line = CMD_START_STUDLIST;
System.Diagnostics.Debug.WriteLine("SENDING//" + line + "//");
byte[] bytesResponse = Encoding.ASCII.GetBytes(line);
stream.Write(bytesResponse, 0, bytesResponse.Length);
foreach(Student s in studentsList){
line = s.id + ";" + s.name + ";" + s.grade + ";" + s.height;
SendLine(stream, line);
}
line = CMD_END_LIST;
SendLine(stream, line);
}
private void SendData(List<Class> classList, NetworkStream stream)
{
string line = CMD_START_CLASSLIST;
System.Diagnostics.Debug.WriteLine("SENDING//" + line + "//");
byte[] bytesResponse = Encoding.ASCII.GetBytes(line);
stream.Write(bytesResponse, 0, bytesResponse.Length);
foreach(Class c in classList){
line = c.id + ";" + c.name;
SendLine(stream, line);
}
line = CMD_END_LIST;
SendLine(stream, line);
}
private void SendLine(NetworkStream stream, string line)
{
System.Diagnostics.Debug.WriteLine("SENDING//" + line + "//");
byte[] bytesResponse = Encoding.ASCII.GetBytes(line);
byte[] lengthOfNextLine = GetLength(bytesResponse);
stream.Write(lengthOfNextLine, 0, lengthOfNextLine.Length);
stream.Write(bytesResponse, 0, bytesResponse.Length);
}
private byte[] GetLength(byte[] bytesResponse)
{
int length = bytesResponse.Length;
byte[] result = new byte[1];
System.Diagnostics.Debug.WriteLine("length: " + length);
result[0] = Convert.ToByte(length);
return result;
}
}
}