BowlerKernel
BowlerTCPServer.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.network;
16 
17 
18 import java.io.DataInputStream;
19 import java.io.DataOutputStream;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.net.Socket;
23 import java.net.SocketException;
24 
25 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
26 import com.neuronrobotics.sdk.common.Log;
27 
28 
29 
30 // TODO: Auto-generated Javadoc
35 
37  private int sleepTime = 5000;
38 
40  private PrintWriter out;
41 
43  private Socket socket;
44 
45 
52  public BowlerTCPServer(Socket socket) throws IOException{
53  this.socket = socket;
54  try {
55  socket.setSoTimeout(1000);
56 
57  } catch (SocketException e) {
58  // TODO Auto-generated catch block
59  e.printStackTrace();
60  }
62  setChunkSize(5210);
63  setDataIns(new DataInputStream(socket.getInputStream()));
64  setDataOuts(new DataOutputStream(socket.getOutputStream()));
65  out = new PrintWriter(socket.getOutputStream(), true);
66  setConnected(true);
67  connect();
68  }
69 
70 
71  /* (non-Javadoc)
72  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#connect()
73  */
74  @Override
75  public boolean connect() {
76 
77  if(isConnected())
78  return true;
79  Log.warning("Connecting..");
80 
81  return isConnected();
82  }
83 
89  @Override
90  public boolean isConnected() {
91 
92  return super.isConnected();
93  }
94  /* (non-Javadoc)
95  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#disconnect()
96  */
97  @Override
98  public void disconnect() {
99  if(!isConnected())
100  return;
101  Log.warning("Disconnecting Tcp Server..");
102  super.disconnect();
103  try {
104 
105  if(!socket.isClosed()){
106  socket.shutdownOutput(); // Sends the 'FIN' on the network
107  while (getDataIns().read() >= 0) ; // "read()" returns '-1' when the 'FIN' is reached
108  socket.close(); // Now we can close the Socket
109  }
110 
111  } catch (IOException e) {
112  // TODO Auto-generated catch block
113  e.printStackTrace();
114  }
115 
116  }
117 
118 
119 
120 // /* (non-Javadoc)
121 // * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#reconnect()
122 // */
123 // @Override
124 // public boolean reconnect() {
125 // Log.warning("TCP Server Reconnect, just disconnecting");
126 // disconnect();
127 // return false;
128 // }
129 
130  /* (non-Javadoc)
131  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#waitingForConnection()
132  */
133  @Override
134  public boolean waitingForConnection() {
135  return false;
136  }
137 
143  public boolean isClientConnected() {
144  if(out==null)
145  return true;
146  return !out.checkError();
147  }
148 
149 
150 
151 }
static void warning(String message)
Definition: Log.java:101