Author Topic: [Java] Sockets in a JavaOp Plugin  (Read 2480 times)

0 Members and 1 Guest are viewing this topic.

Offline Ryan Marcus

  • Cross Platform.
  • Full Member
  • ***
  • Posts: 170
  • I'm Bono.
    • View Profile
    • My Blog
[Java] Sockets in a JavaOp Plugin
« on: October 11, 2005, 11:06:24 am »
I am having some problems with Java sockets... Buffering incoming data.

Here is the code for my JavaOp plugin:

Code: [Select]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

import javax.swing.JComponent;

import callback_interfaces.PluginCallbackRegister;
import callback_interfaces.PublicExposedFunctions;
import callback_interfaces.StaticExposedFunctions;
import exceptions.PluginException;
import plugin_interfaces.ConnectionCallback;
import plugin_interfaces.EventCallback;
import plugin_interfaces.GenericPluginInterface;

public class PluginMain extends GenericPluginInterface implements EventCallback, ConnectionCallback
{

    private static ServerSocket serverSocket = null;

    private static Socket clientSocket = null;

    private static PrintWriter pOut = null;

    private static BufferedReader in = null;
    private static PublicExposedFunctions tOut = null;

    public void load(StaticExposedFunctions staticFuncs)
    {
       
    }

public void activate(PublicExposedFunctions out, PluginCallbackRegister register)
    {
    register.registerEventPlugin(this, null);
    register.registerConnectionPlugin(this, null);
    tOut = out;
    }
public void deactivate(PluginCallbackRegister register)
    {
        // TODO Auto-generated method stub

    }

    public String getName()
    {
        // TODO Auto-generated method stub
        return "SBCP";
    }

    public String getVersion()
    {
        // TODO Auto-generated method stub
        return "1.0";
    }

    public String getAuthorName()
    {
        // TODO Auto-generated method stub
        return "Ryan Marcus";
    }

    public String getAuthorWebsite()
    {
        // TODO Auto-generated method stub
        return "None";
    }

    public String getAuthorEmail()
    {
        // TODO Auto-generated method stub
        return "ryan@marcusfamily.info";
    }

    public String getLongDescription()
    {
        // TODO Auto-generated method stub
        return "Allows you to connect with SBCP";
    }

    public Properties getDefaultSettingValues()
    {
        // TODO Auto-generated method stub
        Properties p = new Properties();
        return p;
    }

    public Properties getSettingsDescription()
    {
        // TODO Auto-generated method stub
        Properties p = new Properties();
        return p;
    }

    public JComponent getComponent(String settingName, String value)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public Properties getGlobalDefaultSettingValues()
    {
        // TODO Auto-generated method stub
        Properties p = new Properties();
        return p;
    }

    public Properties getGlobalSettingsDescription()
    {
        // TODO Auto-generated method stub
        Properties p = new Properties();
        return p;
    }

    public JComponent getGlobalComponent(String settingName, String value)
    {
        // TODO Auto-generated method stub
        return null;
    }
   
   
    public void talk(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
       pOut.println("TALK" + user + " " + statstring);
    }

    public void emote(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("EMOT" + user + " " + statstring);
    }

    public void whisperFrom(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("WSFM" + user + " " + statstring);
    }

    public void whisperTo(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("WSCN" + user + " " + statstring);
    }

    public void userShow(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("USIN" + user + " " + statstring);
    }

    public void userJoin(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("USJN" + user + " " + statstring);
    }

    public void userLeave(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("USLV" + user + " " + statstring);
    }

    public void userFlags(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("USFG" + user + " " + statstring);
    }

    public void error(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("ERRO" + user + " " + statstring);
    }

    public void info(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("INFO" + user + " " + statstring);
    }

    public void broadcast(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("BRDC" + user + " " + statstring);
    }

    public void channel(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("CHAN" + user + " " + statstring);
    }
   
    public final boolean ABORT = false;
    public final boolean CONTINUE = true;
   
    /** The bot is about to connect to a server, but hasn't yet.  At this point, the connection can be stopped. */
    public boolean connecting(String host, int port, Object data) throws IOException, PluginException
    {
        return false;
    }
    /** The bot has just connected to the server. */
    public void connected(String host, int port, Object data) throws IOException, PluginException
    {
       
       
       
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

   
        try
        {
            clientSocket = serverSocket.accept();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     

       
         
         
         try
        {
            pOut = new PrintWriter(clientSocket.getOutputStream(), true);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
     
           
            try
            {
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
       
        pOut.println("Connected!");
       
        String inputLine = null;
        while ("i".equalsIgnoreCase("a"))
        {
            try
            {
                inputLine = in.readLine();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (inputLine.length() != 0)
            {
              try
            {
                tOut.sendText(inputLine);
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
        }
           
    }
   
    /** The bot is about to disconnect from the server.  This is only called for planned disconnects. */
    public boolean disconnecting(Object data)
    {
        return false;
    }
    /** The bot has disconnected from the server. */
    public void disconnected(Object data)
    {
    }


}

Ya, it sucks. So what?

How should I handle incoming data? How should I parse it? Are there already functions to split data at line breaks?
Thanks, Ryan Marcus

Quote
<OG-Trust> I BET YOU GOT A CAR!
<OG-Trust> A JAPANESE CAR!
Quote
deadly: Big blue fatass to the rescue!
496620796F75722072656164696E6720746869732C20796F75722061206E6572642E00