On Thu, Feb 12, 2009 at 12:26 PM, Matthew <[email protected]> wrote: > // Return true if the host is reachable from the server > public boolean pingHost(String host){ > return pingImp(host); > } > > // Return true if the port is open on the host (ping it before > checking) > public boolean checkPort(String host, int port){ > if (ping(host)){ > return checkPortImpl(host, port); > } > return false; > } > > // Return true if the username is valid (ping it and check port 23 > first) > public boolean checkLogin(String host, String username){ > if (ping(host)){ > if (checkPort(host, 23)){ > return checkLoginImpl(host, username); > } > } > return false; > }
First, what you're trying to achieve is probably impossible. Due to the Same Origin Policy (aka SOP), you can only connect to the host serving the page (I'm fuzzy on the details in the scenario that an HTML page served from http://foo.com/ refers to a script served from http://bar.com/--I don't know if XHRs are restricted to foo.com or bar.com, but it would be an easy test if it matters to you). Anyway, point being, most web pages are served from either port 80 (http) or port 443 (https), so the chances that you can do anything useful with port 23 are pretty slim, unless you're running a really non-standard setup. Second, what you're doing, assuming you ignore the SOP, is sort of a waste of time. Why ping, then check the port, then try to login? What happens if the ping succeeds and then the host disappears before you check the port? What happens if the host disappears or the service disappears between checking the port and trying to login? What about between login and the next request? The login request (and every request thereafter that must be authenticated) has to deal with all sorts of potential problems: - connectivity dies on the client - power failure - network cord unplugged - wifi disappears - bad hardware - bad driver - naughty dog/cat/toddler - etc. - connectivity dies on the server - see client - connectivity dies between client and server - backhoe - container ship - etc. - the server can be reached, but the expected service is unavailable - slashdot - DDOS - runaway process - sudo rm -rf / - etc. - the server's administrator disables the client's account - nonpayment - BOFH - etc. One or more of the above problems could happen at any time: before, during, or after any given request. You don't necessarily have to be able to determine the root cause and inform the user (and it may be impossible anyway--how do you distinguish between a backhoe, a container ship, and a BOFH?). You _do_ need to be able to do _something_ coherent when the network "doesn't work". > The implementation methods for all three of these functions make RPC > calls to a server which is performing the network activity. > > The key goal I'm trying to achieve is that the process only continues > after each step succeeds (e.g. the port checkPort only occurs after a > successful ping and so forth). > > Use case #1: the pingHost function will be used on its own to ping > multiple systems > Use case #2: the checkPort function will also be used on its own to > check status of various TCP/UDP ports (however it should only proceed > if ping function succeeds) > Use case #4; the checkLogin function will check for valid logins > (however, I don't want it to proceed unless the ping and checkport > tests succeed) > > It seems like I can't simply create an "OnSuccess chain" here, because > the path isn't linear – it varies depending on "who's" calling the > methods and what they're trying to do. On the other hand, I don't want > to have multiple versions of these implementations just so I can chain > off the OnSuccess methods. > > Can anyone make any general suggestions as to how I might organize or > restructure this so that I can call these subsequent functions > dependant on the results of earlier RPC calls? Use callbacks. If you've got some kind of answer to the problems I outlined above, you need to create your own callback mechanism: public interface WhatNow { void onSuccess(); void onFailure(); } public void pingHost(String host, WhatNow wn){ return pingImp(host, wn); } private void pingImp(String host, final WhatNow wn) { callPingService(host, new AsyncCallback<....>() { public void onFailure(Throwable caught) { wn.onFailure(); } public void onSuccess(.... result) { wn.onSuccess(); } }); } public void checkPort(String host, int port, final WhatNow wn){ ping(host, new WhatNow() { public void onSuccess() { checkPortImpl(host, port, wn); } public void onFailure() { wn.onFailure(); } }); } // etc. Ian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/Google-Web-Toolkit?hl=en -~----------~----~----~----~------~----~------~--~---
