Reputation: 173
This is the Java Code To receive the Data from C Server using Sockets and store the data in Google Data Store and Deploy the app on Google app Engine.
package pack.exp;
@SuppressWarnings("serial")
public class CToJavaToCloudServlet extends HttpServlet
{
static List<String> obj1;
public static void main(String[] args) throws UnknownHostException, IOException
{
Socket s = new Socket("10.9.11.55", 6870);
InputStream stream = s.getInputStream();
byte[] data = new byte[13];
int read;
String can_Id= null;
while((read = stream.read(data)) != -1)
{
can_Id= String.format("%02X%02X%02X%02X", data[0], data[1], data[2],
data[3]);
String can_Data= String.format("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11]);
obj1= new ArrayList<String>();
obj1.add(can_Data.substring(0, 2));
obj1.add(can_Data.substring(3, 5));
obj1.add(can_Data.substring(6, 8));
obj1.add(can_Data.substring(9, 11));
obj1.add(can_Data.substring(12, 14));
obj1.add(can_Data.substring(15, 17));
obj1.add(can_Data.substring(18, 20));
obj1.add(can_Data.substring(21, 23));
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException
{
Key k1 = KeyFactory.createKey("C","0C F0 0400");
String statusEEC1= obj1.get(0);
String driversDemandEnginePercentTorque= obj1.get(1);
String actualEnginePercentTorque= obj1.get(2);
String engineSpeed= obj1.get(3) + obj1.get(4);
String sourceAddressOfControllingDeviceForEngineControl= obj1.get(5);
String engineStarterMode= obj1.get(6);
String engineDemandPercentTorque= obj1.get(7);
Entity can1 = new Entity(k1);
can1.setProperty("Status EEC", statusEEC1);
can1.setProperty("Drivers Demand Engine Percent Torque",
driversDemandEnginePercentTorque);
can1.setProperty("Actual Engine Percent Torque",
actualEnginePercentTorque);
can1.setProperty("Engine Speed", engineSpeed);
can1.setProperty("Source Address Of Controlling Device For Engine Control",
sourceAddressOfControllingDeviceForEngineControl);
can1.setProperty("Engine Starter Mode", engineStarterMode);
can1.setProperty("Engine Demand Percent Torque",
engineDemandPercntTorque);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(can1);
Entity can11 = null;
try
{
can11= datastore.get(k1);
}
catch (EntityNotFoundException e)
{
e.printStackTrace();
}
String statusEEC11= (String) can11.getProperty("Status EEC");
String driversDemandEnginePercentTorque1= (String)
can11.getProperty("Drivers Demand Engine Percent Torque");
String actualEnginePercentTorque1= (String) can11.getProperty("Actual
Engine Percent Torque");
String engineSpeed1= (String) can11.getProperty("Engine Speed");
String sourceAddressOfControllingDeviceForEngineControl1= (String)
can11.getProperty("Source Address Of Controlling Device For Engine
Control");
String engineStarterMode1= (String) can11.getProperty("Engine Starter
Mode");
String engineDemandPercentTorque1= (String) can11.getProperty("Engine
Demand Percent Torque");
resp.setContentType("text/plain");
resp.getWriter().println("Can id 0C F0 0400------>");
resp.getWriter().println("");
resp.getWriter().println("Status EEC--- " + statusEEC11);
resp.getWriter().println("Drivers Demand Engine Percent Torque---" +
driversDemandEnginePercentTorque1);
resp.getWriter().println("Actual Engine Percent Torque---" +
actualEnginePercentTorque1);
resp.getWriter().println("Engine Speed---" + engineSpeed);
resp.getWriter().println("Source Address Of Controlling Device For Engine
Control---" + sourceAddressOfControllingDeviceForEngineControl1);
resp.getWriter().println("Engine Starter Mode---" + engineStarterMode1);
resp.getWriter().println("Engine Demand Percent Torque---" +
engineDemandPercentTorque1);
resp.getWriter().println("");
resp.getWriter().println("");
resp.getWriter().println("");
resp.getWriter().println("");
}
}
On deploying the app to google cloud engine I am getting the error. The website cannot display the page HTTP 500 Most likely causes: •The website is under maintenance. •The website has a programming error.
Can anybody help me out with this problem.
Upvotes: 0
Views: 1174
Reputation: 10727
The javax.servlet
and javax.servlet.http
packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines lifecycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet
and doPost
, for handling HTTP-specific services.
Since you are implementing HttpServlet
interface, you have to implement doGet
and/or doPost
.
Servlet init methods allows a servlet to perform one-time initialization prior to servicing requests. One common mistake when implementing init method is in this form:
So your main
method is not called at all. Move the code from main into init
. Another problem with your code is that you are opening a socket expecting data. This should be done in doGet
and/or doPost
methods since they are responsible for receiving data.
Upvotes: 1