Skip to content

Commit d6b0c39

Browse files
author
unknown
committed
a
1 parent 0536796 commit d6b0c39

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

src/conf/MANIFEST.MF

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Manifest-Version: 1.0
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
package homework.nott.webcontroller;
6+
7+
import homework.nott.hwdb.HWDBClient;
8+
import com.sun.grizzly.websockets.WebSocketEngine;
9+
import java.io.IOException;
10+
import java.sql.Date;
11+
import java.sql.Timestamp;
12+
import java.util.ArrayList;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
import javax.servlet.http.HttpSession;
16+
import org.hwdb.srpc.Connection;
17+
import org.hwdb.srpc.Message;
18+
import org.hwdb.srpc.SRPC;
19+
import org.hwdb.srpc.Service;
20+
21+
import org.springframework.stereotype.Controller;
22+
import org.springframework.web.bind.annotation.RequestMapping;
23+
import org.springframework.web.servlet.ModelAndView;
24+
25+
//pszgp, 26/03-11/04 2012
26+
27+
@Controller
28+
public class SocketController implements Runnable{
29+
30+
final int MAX_SESSION_INTERVAL = 1000000;
31+
static ModelAndView mvHWDB = new ModelAndView();
32+
private int MAX_ROWS_RESULT_SET = 1000;
33+
34+
@RequestMapping("/socket")
35+
public ModelAndView socket(HttpServletRequest request, HttpServletResponse response) {
36+
37+
//subscribe to hwdb when the session starts
38+
HttpSession session = request.getSession(true);
39+
String sessionId = session.getId();
40+
System.out.println(sessionId);
41+
System.out.println(session.getValueNames().length);
42+
if (session.getValueNames().length==0)
43+
{
44+
session.setAttribute("hwdb_session_id", sessionId);
45+
46+
try {
47+
String serviceName = "Handler";
48+
SRPC srpc = new SRPC();
49+
//HWDBClient.setService(srpc.offer(serviceName));
50+
service = srpc.offer(serviceName);
51+
//(new Thread(new HWDBClient())).start();
52+
new Thread(new SocketController()).start();
53+
Connection conn = srpc.connect("localhost", 987,"HWDB");
54+
int port = srpc.details().getPort();
55+
System.out.println(conn.call(String.format("SQL:subscribe LinksLast 127.0.0.1 %d %s", port, serviceName)));
56+
57+
} catch (Exception e) {
58+
System.exit(1);
59+
}
60+
}
61+
else{
62+
Object o = session.getAttribute("sessionAttrsNr");
63+
if (o != null)
64+
{
65+
int sessionAttrsNr = Integer.parseInt(o.toString());
66+
sessionAttrsNr++;
67+
session.setAttribute("sessionAttrsNr", sessionAttrsNr);
68+
}
69+
70+
}
71+
72+
session.setMaxInactiveInterval(MAX_SESSION_INTERVAL);
73+
74+
mvHWDB.addObject("sessionId", sessionId);
75+
mvHWDB.addObject("sessionAttrsNr", session.getValueNames().length);
76+
77+
//if session started, receive mesages from hwdb and send to client
78+
79+
return mvHWDB;
80+
}
81+
82+
static Service service;
83+
public void run() {
84+
try {
85+
Message query;
86+
while ((query = service.query()) != null) {
87+
System.out.println(query.getContent());
88+
String content = query.getContent();
89+
String[] rows = content.split("\n");
90+
int countRows = 0;
91+
int countFields = 0;
92+
if (rows.length>=3)
93+
{
94+
//get the field types
95+
String typesRow = rows[1];
96+
String[] types = typesRow.split("<\\|>");
97+
98+
ArrayList<String[]> valuesRows = new ArrayList();
99+
100+
countFields = types.length;
101+
102+
if (countFields > 1)//at least a timestamp field
103+
//get all the rows of the result set
104+
for (int j = 2; j < rows.length; j=j+3)
105+
{
106+
//get the field values of the current row
107+
String valuesRow = rows[j];//2];
108+
String[] values = valuesRow.split("<\\|>");
109+
if (values.length != types.length)
110+
continue;
111+
//System.out.println("number of columns: " + values.length);
112+
//System.exit(0);
113+
114+
String[] valuesWithTime = new String[countFields];
115+
for (int i=0;i<values.length;i++)
116+
{
117+
valuesWithTime[i] = values[i];
118+
if (i == 0)
119+
{
120+
String valueTimestamp = values[i];
121+
Timestamp t = getTimestampFromUnixString(valueTimestamp);
122+
if (t!=null)
123+
valueTimestamp = t.toString();
124+
valuesWithTime[i] = valueTimestamp;
125+
}
126+
}
127+
valuesRows.add(valuesWithTime);
128+
countRows++;
129+
}
130+
mvHWDB.addObject("types", types);
131+
mvHWDB.addObject("items", valuesRows);
132+
mvHWDB.addObject("countFields", countFields);
133+
mvHWDB.addObject("countRows", countRows);
134+
}
135+
136+
query.getConnection().response("OK");
137+
}
138+
} catch (IOException e) {
139+
System.exit(1);
140+
}
141+
}
142+
143+
Timestamp getTimestampFromUnixString(String hexa){
144+
try{
145+
//remove the @ characters from the first and last position of the string
146+
if (hexa.length() < 3)
147+
return null;
148+
if (hexa.contains("@"))
149+
hexa = hexa.replaceAll("@", "");
150+
long l=Long.parseLong(hexa, 16)/1000000;
151+
return new Timestamp(l);
152+
}
153+
catch(Exception e){
154+
e.printStackTrace();
155+
}
156+
return null;
157+
}
158+
}
159+

0 commit comments

Comments
 (0)