Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #12638

Re: Strange Socket problem

From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: Strange Socket problem
Date 2012-03-03 18:02 -0800
Organization A noiseless patient Spider
Message-ID <jiuifd$ktc$4@dont-email.me> (permalink)
References <jiojsa$5ol$1@dont-email.me> <4f520467$0$26705$65785112@news.neostrada.pl>

Show all headers | View raw


On 3/3/2012 3:45 AM, x wrote:
> Knute Johnson pisze:
>> I'm having a problem in some production code that I can't figure out.
>
> Hi Knute,
>
> I was trying to analyze your code, and currently it might be too
> complicated. The class controls thread starting, stopping, network
> operations, parsing the received lines, notyfying other components, and
> so on... Too many concerns.
>
> If I were you, I would start with refactoring it into manageable chunks.
> For example, in the most general terms, your class has to read lines
> from a server and process them somehow. Why don't you write a small
> class, which does EXACTLY that, and push the remaining issues to other
> classes ?
>
> If you do that, you will be able to unit test each of the parts
> separately. Currently it's rather hard...
>
> See this class for example:
>
>
> public class Worker implements Runnable {
>
> private final String serverAddress;
> private volatile boolean running;
>
> //we will get lines from this fellow...
> private LineSource lineSource;
>
> //...and pass them to this guy
> private final LineProcessor lineProcessor;
>
> public Worker(String serverAddress, LineProcessor lineProcessor) {
> this.running = true;
> this.serverAddress = serverAddress;
> this.lineProcessor = lineProcessor;
> }
>
> @Override
> public void run() {
> // does not connect to server, just creates the object and saves // the
> server address
> lineSource = new LineSource(serverAddress);
>
> while (running) {
> String line = null;
> try {
> line = lineSource.getLine();
> } catch (Exception e) {
> // TODO LOG IT. MUST... HAVE... LOGS...
> }
>
> if (line != null) {
> try {
> lineProcessor.processLine(line);
> } catch (Exception e) {
> // TODO LOG IT. MUST... HAVE... LOGS...
> }
> }
> }
>
> // ok, we're finished, just close the lineSource
> try {
> lineSource.close();
> } catch (Exception e) {
> // TODO LOG IT. MUST... HAVE... LOGS...
> }
> }
>
> public void stop() {
> running = false;
> }
> }
>
> It is just a starting point, still too messy to be used in production.
> And I did't really look into the subject of blocking operations.
>
> I realize that my solution to your problem is an indirect one, but hey,
> it just took five minutes to write this code.
>
> Have a nice day,
> Marcin Jaskolski

Thanks Marcin.  All of this has already been tested many times as 
individual pieces.  It has been running in this condition for almost a 
year now in three sites.

-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar


Thread

Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-01 11:49 -0800
  Re: Strange Socket problem Steven Simpson <ss@domain.invalid> - 2012-03-01 20:59 +0000
    Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-01 13:30 -0800
      Re: Strange Socket problem Steven Simpson <ss@domain.invalid> - 2012-03-01 22:54 +0000
        Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-01 16:32 -0800
          Re: Strange Socket problem Steven Simpson <ss@domain.invalid> - 2012-03-03 09:08 +0000
            Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-03 17:48 -0800
  Re: Strange Socket problem markspace <-@.> - 2012-03-01 14:08 -0800
    Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-01 16:48 -0800
      Re: Strange Socket problem markspace <-@.> - 2012-03-02 08:33 -0800
        Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-02 15:29 -0800
          Re: Strange Socket problem markspace <-@.> - 2012-03-03 09:33 -0800
            Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-03 17:55 -0800
              Re: Strange Socket problem markspace <-@.> - 2012-03-03 20:06 -0800
                Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-03 21:02 -0800
                Re: Strange Socket problem "John B. Matthews" <nospam@nospam.invalid> - 2012-03-05 00:00 -0500
                Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-05 09:44 -0800
  Re: Strange Socket problem Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-01 22:15 +0000
    Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-01 16:43 -0800
      Re: Strange Socket problem Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-03 02:01 +0000
  Reiteration: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-01 16:51 -0800
    Re: Reiteration: Strange Socket problem Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-03 02:02 +0000
  Re: Strange Socket problem x <x@x.x> - 2012-03-02 21:18 +0100
    Re: Strange Socket problem Lew <noone@lewscanon.com> - 2012-03-02 15:06 -0800
      Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-02 15:38 -0800
        Re: Strange Socket problem Lew <noone@lewscanon.com> - 2012-03-02 16:00 -0800
          Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-02 17:18 -0800
            Re: Strange Socket problem Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-03 02:13 +0000
              Re: Strange Socket problem Steven Simpson <ss@domain.invalid> - 2012-03-03 08:41 +0000
                Re: Strange Socket problem Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-03 18:26 +0000
        Re: Strange Socket problem x <x@x.x> - 2012-03-03 11:32 +0100
          Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-03 17:59 -0800
          Re: Strange Socket problem Lew <noone@lewscanon.com> - 2012-03-03 23:21 -0800
      Re: Strange Socket problem x <x@x.x> - 2012-03-03 10:59 +0100
        Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-03 18:08 -0800
    Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-02 15:33 -0800
  Re: Strange Socket problem Paka Small <paka-en@tumia.org> - 2012-03-03 03:32 -0800
    Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-03 18:00 -0800
  Re: Strange Socket problem x <x@x.x> - 2012-03-03 12:45 +0100
    Re: Strange Socket problem Knute Johnson <nospam@knutejohnson.com> - 2012-03-03 18:02 -0800

csiph-web