Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to monitor tcp port or ping with CQC?
#11
Here is one that shows more how it would be used in a driver:

Code:
Class=[NonFinal]
    ClassPath MEng.TestPing;
    ParentClass MEng.Object;
EndClass;

Imports=
    MEng.System.Runtime.ConsoleOutStream;
    MEng.System.Runtime.SockPinger;
    MEng.System.Runtime.IPEndPoint;
EndImports;

Types=
    VectorOf[SockPinger]    PingerList;
EndTypes;

Members=
    PingerList          m_Pings;
    ConsoleOutStream    m_OutCon;
    Time                m_Time;
EndMembers;



Methods=[Public,Final]

    Constructor()
    Begin
    EndConstructor;

    Method ProcessOne([InOut] SockPinger Pinger)
    Begin
        Locals=
            Card4 RepMillis;
        EndLocals;

        m_Time.SetSpecial(SpecialTimes.CurTime);
        Switch(Pinger.GetState())
            Case PingerStates.WaitSend :
                If (m_Time.IsDiffGreater(Pinger.GetLastTime(), 60))
                    Pinger.SendRequest();
                    m_OutCon.FmtStr("Starting ping for address ");
                    m_OutCon.FmtStr(Pinger.GetRemAddr());
                    m_OutCon.NewLn();
                    m_OutCon.Flush();
                EndIf;
            EndCase;

            Case PingerStates.WaitReply :
                If (Pinger.WaitReply(10, RepMillis))
                    m_OutCon.FmtStr("Got a reply for address ");
                    m_OutCon.FmtStr(Pinger.GetRemAddr());
                    m_OutCon.NewLn();
                    m_OutCon.Flush();
                Else
                    If (m_Time.IsDiffGreater(Pinger.GetLastTime(), 10))
                        //
                        //  Give up on this one, but don't reset the time stamp.
                        //  That way the ten seconds we waited will now be part
                        //  of the wait for the 60 seconds to the next poll.
                        //
                        Pinger.CancelWait(False);
                        m_OutCon.FmtStr("Timed out on address ");
                        m_OutCon.FmtStr(Pinger.GetRemAddr());
                        m_OutCon.NewLn();
                        m_OutCon.Flush();
                    EndIf;
                EndIf;
            EndCase;

            Default :
            EndCase;

        EndSwitch;

    EndMethod;


    Method Start() Returns Int4
    Begin
        Locals=[Const]
            Card4       Count(2);
        EndLocals;

        Locals=
            IPEndPoint  TarEP;
            Card4       Index(0);
            SockPinger  DummyPinger;
        EndLocals;

        Try
            // Add some pinger objects to the list
            m_Pings.AddObject(DummyPinger);
            m_Pings.AddObject(DummyPinger);

            m_Pings[0].StartPing("www.google.com", IPAddrTypes.Unspec, TarEP);
            m_OutCon.FmtStr("Engaging Ping for address ");
            m_OutCon.Format(TarEP);
            m_OutCon.NewLn();

            m_Pings[1].StartPing("www.yahoo.com", IPAddrTypes.Unspec, TarEP);
            m_OutCon.FmtStr("Engaging Ping for address ");
            m_OutCon.Format(TarEP);
            m_OutCon.NewLn();
            m_OutCon.Flush();

            While(True)
                ProcessOne(m_Pings[Index]);

                Index++;
                If (Index = Count)
                    Index := 0;
                EndIf;

                // Sleep half a second between checks
                m_Time.Sleep(500);
            EndWhile;

        EndTry;

        Catch
            m_OutCon.FmtStr("\nEXCEPTION: ");
            m_OutCon.FmtStr($Exception.GetErrorText());
            m_OutCon.NewLn();
        EndCatch;

        m_OutCon.Flush();

        Return 0;
    EndMethod;
EndMethods;


But there's something wrong. The yahoo one will only ever reply once, though the standard Windows pinger gets multiple replies as expected. I'm not sure what's up with that. The Google one continues to reply as expected. The Yahoo one also only replies once in my previous, simpler one as well. So I must be doing something wrong that Google is ignoring but Yahoo isn't. But I'm not sure what it is.
Dean Roddey
Explorans limites defectum
Reply
#12
There's a 'last time' stamp associated with each pinger object. When the pinger is in the 'waiting for reply' state, then that stamp holds the time at which the request was sent. So it can be used to calculate how long you've been waiting for the reply.

When a reply comes in then the state goes back to 'idle' state, to indicate that there's no outstanding request, and in that state the stamp then represents the time at which the last reply was seen. This way you can use it to figure out how long since the last successful (or failed) ping, to figure out when it's time to do another one.

If you give up on waiting for a reply, you should cancel it, to force the state back to idle state (otherwise the kind of logic used in this example wouldn't work since it would stay in 'waiting for reply' state forever.) You can have the cancel either reset the stamp or not. I didn't do it in this example because I figure why wait 60 seconds plus the time of the timeout. Might as well let that 10 seconds we waited before giving up become part of the 60 second countdown to the next ping. But it would have worked fine either way.
Dean Roddey
Explorans limites defectum
Reply
#13
OK, my problem was that I wasn't clearing the checksum field before calculating the checksum (which includes the checksum field.) The first time it was zero since it hadn't been set yet. After that it would cause an invalid checksum to be calculated. So I'm declaring the ping stuff ready and moving on to the next thing.

BTW, that m_Time.IsDiffGreater() call I use in that example above is something i just added. It's just a really nice convenience that I don't know why I hadn't added before. But it won't work until you get the next drop (4.1.905).
Dean Roddey
Explorans limites defectum
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on CQC / WSE 2016 / Port 443 kblagron 3 5,508 11-15-2017, 09:06 PM
Last Post: Dean Roddey
  Serial Port Demons batwater 13 11,394 03-06-2017, 02:30 PM
Last Post: batwater
  cheap single port TCP/Serial? SomeWhatLost 15 7,993 08-22-2016, 08:12 PM
Last Post: Dean Roddey
  Baby Monitor? Sendero 5 2,718 02-15-2016, 05:49 PM
Last Post: jkmonroe
  8 port relay board with RS232 or Ethernet - Cheap znelbok 2 3,176 02-03-2015, 11:33 AM
Last Post: znelbok
  Energy Monitor by Outlet/Plug? jkmonroe 13 4,369 01-16-2014, 03:44 PM
Last Post: jkmonroe
  HP L2105tm Optical Touch Monitor LesAuber 0 1,361 04-15-2013, 11:13 AM
Last Post: LesAuber
  Com port redirecter help SomeWhatLost 5 2,787 02-03-2011, 03:00 PM
Last Post: Dean Roddey
  Lantronix serial port thingy reliability SomeWhatLost 15 5,966 04-08-2010, 12:28 PM
Last Post: SomeWhatLost
  Any easy way to Ping? anogee 4 2,678 07-21-2009, 05:07 PM
Last Post: broconne

Forum Jump:


Users browsing this thread: 1 Guest(s)