Author Topic: [Java] Using sockets in a JavaOp plugin  (Read 3586 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] Using sockets in a JavaOp plugin
« on: December 06, 2005, 06:55:44 pm »
Alright, after you read this it should be pretty obvious what it does:

Code: [Select]
[pre]
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!");
       
       
           
    }
   
    /** 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)
    {
    }

   
    private static String inputLine = null;
   
    while ("i".equalsIgnoreCase("e"))
    {
        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();
        }
        }
    }
   

}
}
[/pre]

The only problem Eclipse is telling me about is that private static String inputLine = null; should not end in a ;. but it should be a {?


Help!
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

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] Using sockets in a JavaOp plugin
« Reply #1 on: December 06, 2005, 07:39:17 pm »
It's because your code is broken.  You have a while() loop outside of a method:
Code: [Select]
    public void disconnected(Object data)
    {
    }
    // function ends

    // member declaration
    private static String inputLine = null;
    // compiler: oh, there's code here.  That shouldn't have been an end-of-statement, it should have been
    // an opening brace.  Error!
    while ("i".equalsIgnoreCase("e"))
    {
        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();
        }
        }
    }
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Ryan Marcus

  • Cross Platform.
  • Full Member
  • ***
  • Posts: 170
  • I'm Bono.
    • View Profile
    • My Blog
Re: [Java] Using sockets in a JavaOp plugin
« Reply #2 on: December 06, 2005, 08:56:50 pm »
Ah! I see. Then where do you suggest I put that code? Its still in the class, so I thought it might be OK...
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

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] Using sockets in a JavaOp plugin
« Reply #3 on: December 06, 2005, 09:16:30 pm »
Well it needs to be in a function, otherwise having code in arbitrary places won't do anything....
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [Java] Using sockets in a JavaOp plugin
« Reply #4 on: December 06, 2005, 09:29:08 pm »
Code: [Select]
while ("i".equalsIgnoreCase("e"))
You know i is never e, right? =p
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Ryan Marcus

  • Cross Platform.
  • Full Member
  • ***
  • Posts: 170
  • I'm Bono.
    • View Profile
    • My Blog
Re: [Java] Using sockets in a JavaOp plugin
« Reply #5 on: December 06, 2005, 09:37:12 pm »
That's the idea. It loops forever, because I can't figure out how to make code execute when a socket receives data.
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

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [Java] Using sockets in a JavaOp plugin
« Reply #6 on: December 06, 2005, 09:42:49 pm »
That's the idea. It loops forever, because I can't figure out how to make code execute when a socket receives data.

Is while(true) illegal in Java? :\

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] Using sockets in a JavaOp plugin
« Reply #7 on: December 06, 2005, 10:00:37 pm »
That's the idea. It loops forever, because I can't figure out how to make code execute when a socket receives data.

Is while(true) illegal in Java? :\
No, and receive() should block until data arrives.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [Java] Using sockets in a JavaOp plugin
« Reply #8 on: December 06, 2005, 10:46:43 pm »
Resolved, thanks to Joe!

I taught Ryan how to use a Timer. =)
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] Using sockets in a JavaOp plugin
« Reply #9 on: December 07, 2005, 01:30:30 am »
You know, Timers aren't the proper way to implement multithreading when there is a Thread class.  :P
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.