Reputation: 7980
I've a singleton object responsible to start and stop an http server (using RESTlet framework).
When I create the object the boolean
var isStarted
is set to false
. When the serve is started isStarted
is set to true
.
Here's is my code for the singleton:
private WebService() {
this(Protocol.HTTP, DEFAULT_PORT);
}
private WebService(final Protocol protocol, final int port)
{
this.protocol = protocol;
this.port = port;
this.isStarted = false;
// Creating a new Restlet component and
this.component = new Component();
}
public synchronized static WebService getInstance()
{
if(instance == null)
{
return new WebService();
}
return instance;
}
public synchronized static WebService getInstance(final Protocol protocol, final int port)
{
if(instance != null)
{
instance = null;
}
return new WebService(protocol, port);
}
Right now I'm just using the getInstance()
, with no parameters.
Here is how I start and stop the server:
public synchronized boolean startServer(final Context context)
{
if ( isStarted ) {
Log.d(TAG, "Tried to start a running server");
throw new IllegalStateException("Tried to start a running server");
}
// Add a http connector to the component.
component.getServers().add(protocol, port);
component.getClients().add(Protocol.FILE);
// Static content
// Attach the component to the localhost
ipAuthenticator = new IPAuthenticator(org.restlet.Context.getCurrent());
ContactsRouter contactsRouter = new ContactsRouter(context, ipAuthenticator);
AuthenticationRouter authenticationRouter = new AuthenticationRouter(contactsRouter, ipAuthenticator);
component.getDefaultHost().attach(authenticationRouter);
// Start it! The HTTP server connector is also automatically started.
try {
component.start();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
isStarted = false;
return false;
}
isStarted = true;
Log.d(TAG, "Server Started! and isStarted is: " + isStarted);
return true;
}
public Boolean stopServer()
{
Log.d(TAG, "WebService: isStarted: " + isStarted);
if(isStarted)
{
try {
component.stop();
Log.d(TAG, "Server stopped");
return true;
} catch (Exception e) {
Log.e(TAG, "Exception - Could not stop the server!");
e.printStackTrace();
}
}
return false;
}
My problem is when I start and stop the server using my app user interface I can start it with no problem but when I try to stop the server (after it has been started) I can't because isStarted
is false
when it should be true
, as I just started the server previously.
Here is when I try to start/stop the server using my app Uuser Interface:
public HttpServer(ProgressBar progressBar, Context context, boolean isChecked)
{
this.progressBar = progressBar;
progressBar.setEnabled(false);
this.context = context;
webService = WebService.getInstance();
this.isChecked = isChecked;
}
(...)
@Override
protected void onPostExecute(Void result) {
// Hide the progress bar
progressBar.setVisibility(View.GONE);
// Here in this logcat I receive-> is sever started?: **false** but it should be true
Log.d(TAG, "isChecked = " + isChecked + " || Is the server started?: " + webService.isStarted());
if(isChecked == true)
{
Log.d(TAG, "Going to start the server");
webService.startServer(context);
}
else
{
Log.d(TAG, "Going to stop the server");
webService.stopServer();
}
}
Again, my question is why, after the server is started, is isChecked
false when it should be true?
Thanks!
Upvotes: 0
Views: 297
Reputation: 1440
You are not affecting the singleton object
public synchronized static WebService getInstance()
{
if(instance == null)
instance = new WebService();
return instance;
}
Upvotes: 4