Re: Client-Server to Web App
- From: "Paul Nichols (TeamB)" <paul@xxxxxxxx>
- Date: Sun, 14 May 2006 23:31:36 -0400
Damian Wood wrote:
"Paul Nichols (TeamB)" <paul@xxxxxxxx> wrote in message
news:4467efa9@xxxxxxxxxxxxxxxxxxxxxxxxx
Any of these strategies should work. Your requirements and/or limitations
are the key as to which technology you can or cannot use.
Thank you for your answer Paul. If you'll allow me to take a bit more of
your time I would like to ellaborate on the requirement.
Currently lets say we have 100 of these win32 clients all hitting the c++
server. Now translating the UI over to HTML of course will not be as rich
but it will suffice, so I have no problem there.
Since I do not know what the current client looks like, cannot say.
However, if you have not looked at AJAX Web clients, might be worth a
looksie. Many of these can look like Rich Clients, since the request,
response are asynchronous.
Let's say I implement an
ISAPI dll as a Web Service (or a .NET one for that matter) and this Web
Service acts as a go between for the messages. Since a Web Service is
stateless, it possible to keep a connection open to the C++ server between
requests to the web service from my web page UI?
That would be a bad design, IMHO. You do not need to keep the connection to
the C/C++ server open. If you do, you will use unnecessary memory on the
server itself. The connection to the C/C++ server side piece, should only
need connection when it is performing its services. If you need something
like instantaneous updates, you will need to incorporate a polling
mechanism, say with 1 to 5 second intervals. Refresh the state in a Session
managed object through an HTTPSession type object or through URL rewriting.
The downside is if the C/C++ server side piece is not thread safe. This can
really create a problematic scenario. If that is what you have to work
with, then you will need to create a thread safe Http or socket type
request that would host the data in a thread safe object. Below is a Java
example, but it will work with whatever technology.
/*By making this class Serializable you can save state on the server, or
send it across the network */
package com.yourcompany.data;
import java.util.Hashtable;
import java.util.ArrayList;
public class DataTable implements Serializable {
private String address; //used for server address TCP-IP
private int port; //used to provide the port# for socket call
private Hashtable data;
//skipping constructor, etc
public DataTable(){
try{
fillDataTable();
}
catch(Exception ex){
//Handle the exception
}
private void fillDataTable() throws Exception {
if(data==null){
data= new Hashtable();
}
int lineNbr=0;
//Make call to the C/C++ Server call
Socket clientSocket= new ClientSocket("Address", port);
clientSocket.open();
BufferedReader reader= new BufferedReader
(clientSocket.getInputStream());
while(reader!=null){
++lineNbr;
array.add(reader.readLine());
data.put(""+lineNbr,array);
}
reader.close();
}
public synchronized Hashtable getData() {
return data;
}
}
If you called this class from a standard Java Servlet, for instance, since
the servlet is thread safe, and the getData() method is thread safe (via
synchronized), then you have a thread safe call. The Hashtable returned to
the calling servlet, could be passed over the network, to a JSP,etc. You
would need to implement some listener to refresh this periodically via a
threaded call or Timer class (in Java). C# implementation will be close to
the Java class as per above.
It will require a little more work to do this with Delphi or with ISAPI, due
to making the class thread safe and implementing the Thread via TThread or
a TTimer. But same principles apply.
You could also put this in the Session Object with Java and C#.
Example
In Java Servlet:
package com.yourcompany.data.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.yourcompany.data.DataTable;
public class DataServlet extends HttpServlet
{
public synchronized void init (ServletConfig config)
{
super.init();
}
/** doPost method
*/
public void doGet(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException
//Next Page is the page you wish to send response to
String nextPage="myDisplayPage.jsp";
//DataTable is a call to your class
DataTable dt= new DataTable();
//data is a Hashtable used for data from dt instantiation of
// DataTable
Hashtable data=new HashMap(); //prevents null exceptions
//String errors will be used to display any errors to the user
String errrors=
"<p align='center'>
<font style='font-family:Times,Arial,Helvetica,Sans-Serif;
face-weight:bold; font-size:10pt;'>";
//Get the Data
try{
data=dt.getData();
}
catch(Exception ex){
errors=errors+ex.getMessage()+"</font></p>";
//Set the errors in request scope.
req.setAttribute("errors",errors);
}
//HttpSession makes your data session aware and will maintain state
HttpSession session=request.getSession(true);
//Adding an Attribute means that your session object has an
// attribute called data
session.addAttribute("data",dt.getData());
RequestDispacther rd= req.getRequestDispatcher();
rd.forward(req,res);
}
}
The JSP:
<@%page import="java.util.Hashmap"%>
<html>
<head>
<title>Your C++ Data</title>
</head>
<body>
<form action="dataServlet" method="post">
<input type="submit" name="getData" value="Get Data"/>
</form>
<%//Java Scriplet
//Get the Session Object. If the attribute is not
//null, display it
Hashtable data=(Hashtable)
request.getSession().getAttribute("data");
if(data!=null){ %>
<font style="font-size:12px; font-weight:bold;">
Your Data is shown below:</font>
<br/><br/>
<table cellpadding="2" cellspacing="2"
width="80%" border="1">
for (int i=0; i<data.size();i++){ %>
<tr>
<td width=20%">Record Number: "+data.key(""+i);
</td>
<td width="80%"> Data <%=i%>
<%=data.get(""+i).toString()%>
</td>
</tr>
<% } //end for %>
</table>
<% } //end if %>
</body>
</html>
Please note: I have not tested this code, so there may be bugs in it. Just
wrote it off the top of my head, which is why I chose Java (i am more
familiar with Java, since it is my primary language). You can do the same
in ASP.NET or Delphi.
One other note: I would use the timer within the JSP or ASP.NET page. Use
Java Script to implement a timer and change the code above to do an auto
document.forms[0].submit();
Example below:
<SCRIPT LANGUAGE = "JavaScript">
<!--
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
function InitializeTimer()
{
// Set the length of the timer, in seconds
secs = 5 //time out in 5 seconds refresh page
StopTheClock();
StartTheTimer();
}
function StopTheClock()
{
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function StartTheTimer()
{
if (secs==0)
{
StopTheClock();
// Here is where you call your servlet
document.forms[0].submit();
}
else
{
self.status = secs;
secs = secs - 1; //decrement by one
timerRunning = true; //set it to running
timerID = self.setTimeout("StartTheTimer()", delay);
}
}
//-->
</SCRIPT>
Make sure if you do this, to add this to the <body> tag in your JSP/ASP.NET
page:
<body onload="return InitializeTimer()">
Hope this helps ya!!
.
- Follow-Ups:
- Re: Client-Server to Web App
- From: Paul Nichols (TeamB)
- Re: Client-Server to Web App
- References:
- Client-Server to Web App
- From: Damian Wood
- Re: Client-Server to Web App
- From: Paul Nichols (TeamB)
- Re: Client-Server to Web App
- From: Damian Wood
- Client-Server to Web App
- Prev by Date: Re: new books about .NET and Delphi
- Next by Date: Re: interesting post that could be applied to Delphi
- Previous by thread: Re: Client-Server to Web App
- Next by thread: Re: Client-Server to Web App
- Index(es):
Relevant Pages
|
|