Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TCP Connections / Talking to EventGhost
#1
I'm trying to use Quick Macros to talk to EventGhost. Key codes have been inconsistently working, so I've gotten to the point where I've decided using TCP connections will probably be the best way. Anyway, I've got a plugin to receive events in EventGhost. I also have a python script that sends events.
I cannot, for the life of me, figure out how to get it working (by rewriting, etc).
I've attached the bit that does the sending.
Code:
Copy      Help
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #self.socket = sock
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.settimeout(2.0)
        try:
            sock.connect((self.host, self.port))
            sock.settimeout(1.0)
            # First wake up the server, for security reasons it does not
            # respond by it self it needs this string, why this odd word ?
            # well if someone is scanning ports "connect" would be very
            # obvious this one you'd never guess :-)

            sock.sendall("quintessence\n\r")

            # The server now returns a cookie, the protocol works like the
            # APOP protocol. The server gives you a cookie you add :<password>
            # calculate the md5 digest out of this and send it back
            # if the digests match you are in.
            # We do this so that no one can listen in on our password exchange
            # much safer then plain text.

            cookie = sock.recv(128)

            # Trim all enters and whitespaces off
            cookie = cookie.strip()

            # Combine the token <cookie>:<password>
            token = cookie + ":" + self.password

            # Calculate the digest
            digest = md5(token).hexdigest()

            # add the enters
            digest = digest + "\n"

            # Send it to the server
            sock.sendall(digest)

            # Get the answer
            answer = sock.recv(512)

            # If the password was correct and you are allowed to connect
            # to the server, you'll get "accept"
            if (answer.strip() != "accept"):
                sock.close()
                return False

            # now just pipe those commands to the server
            if (payload is not None) and (len(payload) > 0):
                for pld in payload:
                    sock.sendall(
                        "payload %s\n" % pld.encode(eg.systemEncoding)
                    )

            sock.sendall("payload withoutRelease\n")
            sock.sendall(eventString.encode(eg.systemEncoding) + "\n")

            return sock

        except:
            if eg.debugLevel:
                eg.PrintTraceback()
            sock.close()
            self.PrintError("NetworkSender failed")
            return None

Any hints?

- Derek
#2
QM TCP client class:
TCP/IP

To get MD5, use str function encrypt().
#3
I have this so far:
Code:
Copy      Help
out
#compile "__TcpIpClient"
TcpIpClient x

str server="localhost"
str getfile=""
str receivedData
str cookie
str token
str digest

out "---- connecting ----"

if(!x.Connect(server 1024)) out "failed"

out "---- validating ----"

if(!x.Send("quintessence\n\r")) out "failed"
if(!x.Receive(cookie 128)) out "failed"
out cookie
digest.encrypt(2|8 cookie)
out digest
digest.from(digest "\n")
out digest
if(!x.Send(digest)) out "failed"

out "---- sending request ----"

str request.format("Vol Up %s" getfile)
out "---- receiving ----"

if(!x.Receive(receivedData)) end "failed"

out "---- received: ----"
out receivedData

It hangs at the first "Send" and I'm unsure how to figure out why.

- Derek
#4
Maybe because in QM escape sequences for \n\r are different.

Code:
Copy      Help
if(!x.Send("quintessence[10][13]")) out "failed"
#5
Should have figured.
Fixed those throughout, but it still hangs.
In fact, it hangs on any time I try to do anything with the connection.

Now as part of the connection, I need to receive a cookie, hash it and send it back. Should I be trying to do that as part of "Connect"?

Alternatively to all of this, is there a reliable way to send events to EventGhost, or barring that to XBMC?

I'm using QM because it is the only program I can find that can differentiate between multiple keyboards, and I want my remote (that shows up as a keyboard) to address XBMC no matter what has focused.

- Derek
#6
Quote:Now as part of the connection, I need to receive a cookie, hash it and send it back. Should I be trying to do that as part of "Connect"?
No. Your code is correct, I don't know why it hangs.

Quote:Alternatively to all of this, is there a reliable way to send events to EventGhost, or barring that to XBMC?
I would try Directory Watcher plugin. QM then can create or rename or update a file, and it would generate event.
Or using command line -e, but it is slow.

I'm not familiar with XBMC.
#7
In this case Receive() hangs if not specified size of data to receive, or specified more.

This code works.

Macro Macro1568
Code:
Copy      Help
out
#compile "__TcpIpClient"
TcpIpClient x

str server="localhost"
str password="xxxxxxxx"
str request="test_qm"

str receivedData
str cookie
str digest

out "---- connecting ----"

if(!x.Connect(server 1024)) end "failed"

out "---- validating ----"

if(!x.Send("quintessence[10]")) end "failed"
if(!x.Receive(cookie 5)) end "failed"
cookie.trim; cookie+":"; cookie+password
out cookie
digest.encrypt(2|8 cookie)
out digest
digest+"[10]"
if(!x.Send(digest)) end "failed"
if(!x.Receive(receivedData 7)) end "failed"
out receivedData

out "---- sending request ----"

if(!x.Send("payload withoutRelease[10]")) end "failed"
if(!x.Send(_s.from(request "[10]"))) end "failed"

out "---- sent ----"

x.Send("close[10]")

Also I updated TcpIpClient.Receive. Now it is more reliable. This code works with the old version too.
#8
This code works fantastically.
Thanks so much for your help!


Forum Jump:


Users browsing this thread: 1 Guest(s)