Socket connection

Connect to MMDAgent-EX via TCP/IP socket to monitor and/or control it.

This facility is only available on Desktop OS (Win/Mac/Linux)

The default Plugin Plugin_Remote offers communication with other process via TCP/IP socket. MMDAgent-EX can act as a server, or as a client. Connected peer process can:

  • send text lines to the MMDAgent-EX process to be issued as a message,
  • receive messages (or all the raw internal log texts) from the MMDAgent-EX process.

Enabling server function

The configuration is per-content basis. Write the following lines in your content .mdf file to enable server function while playing the content. The value of Plugin_Remote_Port is TCP port number it listens to. The default value is 39392. Maximum of 20 clients can be connected at once.

# configuration in .mdf to enable server function
Plugin_Remote_EnableServer=true
Plugin_Remote_Port=39392

By default only event / command messages will be sent from MMDAgent-EX. To enable sending all raw logs, including message, internal log and trace information, set Plugin_Remote_AllLog to true.

# configuration in .mdf to enable sending all raw log
Plugin_Remote_AllLog=true

Let me show you an example client script. The Python script below can connect to a MMDAgent-EX running as server on local host at port 39392, send a test message to it, and disconnect. The thrown message “MESSAGE|aaa|bbb” will be issued inside the MMDAgent-EX process.

# Example 1: post a message to MMDAgent-EX running as server
import socket
server = ("127.0.0.1", 39392)
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client.connect(server)
tcp_client.send(b"MESSAGE|aaa|bbb")
tcp_client.close()

Next is an another example, receiving messages from the MMDAgent-EX and outputing them to stdout. If you want to receive all raw log, set Plugin_Remote_AllLog to true in .mdf file as mentioned above.

# Example 2: capture messages from MMDAgent-EX running as server
import socket
server = ("127.0.0.1", 39392)
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client.connect(server)
while True:
   try:
      rcvmsg = tcp_client.recv(4096)
      print("[*] received: {}".format(rcvmsg))
   except Exception as e:
      print(e)
tcp_client.close()

More complicated example that let MMDAgent-EX show pressed key as text. It reads KEY|... messages in the sent message, and issue TEXTAREA_SET message with the pressed key string.

# Example 3: make server to show pressed key as text in the scene
import socket
import re

# return first message
def connectedMessage():
   return "TEXTAREA_ADD|num|6,2|2,0.3,0,1|0,0,0,0.7|1,1,1,1|0,6,-4"

# process log per line and return message to be sent to MMDAgent-EX if needed
def process(msg):
   matched = re.match('.*KEY\|(.*)$', msg)
   if (matched):
      return "TEXTAREA_SET|num|{}".format(matched.group(1))

# main
server = ("127.0.0.1", 39392)
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client.connect(server)
tcp_client.send(connectedMessage().encode())
while True:
   try:
      rcvmsg = tcp_client.recv(4096)
      print("[*] received: {}".format(rcvmsg))
      ret = process(rcvmsg.decode().rstrip())
      if (ret):
         tcp_client.send(ret.encode())
   except Exception as e:
      print(e)
tcp_client.close()

Enabling socket client function

You can also make MMDAgent-EX to act as a network client, connecting to a remote server. Write the following lines in the .mdf file of your content to enable client function. Be sure that you should not set both Plugin_Remote_EnableServer and Plugin_Remote_EnableClient in a .mdf file.

# configuration in .mdf to enable client function
Plugin_Remote_EnableClient=true
Plugin_Remote_Hostname=localhost
Plugin_Remote_Port=39392

When these settings are given, MMDAgnet-EX will try to make connection to the specified host and port, at the beginning of your content. When connection was successfull, MMDAgent-EX sends messages to the remote server, and also receive messages from the server and issue them to the internal message queue. If you want MMDAgent-EX to send all raw logs, set Plugin_Remote_AllLog to true.

# configuration in .mdf to enable sending all raw log
Plugin_Remote_AllLog=true

Here is an example of server script which can accept connection from MMDAgent-EX with client function. After connection, it will send the client MMDAgent-EX a test message and disconnect.

# Example 1: wait connection from MMDAgent-EX running as client, post message and disconnect
import socket
server = ("127.0.0.1", 39392)
listen_num = 5
tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server.bind(server)
tcp_server.listen(listen_num)
while True:
   client, address = tcp_server.accept()
   print("[*] connected: {}".format(address))
   client.send(b"MESSAGE|aaa|bbb")
   client.close()

An another exmple. The server script below also accept connection from MMDAgent-EX running with client mode, and then it keeps receiving messages from the client MMDAgent-EX and output them to stdout.

# Example 2: wait connection from MMDAgent-EX running as client, capture messages from it
import socket
server = ("127.0.0.1", 39392)
listen_num = 5
tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server.bind(server)
tcp_server.listen(listen_num)
while True:
   client, address = tcp_server.accept()
   print("[*] connected: {}".format(address))
   while True:
      try:
         rcvmsg = client.recv(4096)
         print("[*] received: {}".format(rcvmsg))
      except Exception as e:
         print(e)
   client.close()