• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

how to change/modify/wrap method at runtime?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm new to Java and still getting my feet wet. Looking at old topics and the articles/FAQs here have helped, so thank you! If something I write here doesn't make sense I probably got my wires crossed somewhere, so please let me know .

I've been searching around this morning for ways to modify a method at runtime. It seems like there are a lot of different techniques for this over the last 10 or so years so I'd like to know what's considered old hat and what is current practice.

Here's why I want to do this. I'm writing a telnet library for a game I'm working on (a text mud). The server has a method that sends output from the mud to the client (call it send). Simplified it might look like this:



There are options that could change how the server sends data to the client. One example is the option MCCP (mud client compression protocol). When MCCP turns on, the server starts compressing all output sent. What I'd like to do is change the method to something like:



Now I guess I could include all the possible modifications in the send method, and check if they're active at runtime. But what would be some more dynamic ways to change things around, so I wouldn't have to put it in the send method beforehand?





 
Marshal
Posts: 75665
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That looks a more difficult question that we usually get here, so I shall move it to another forum.

Don't actually know. You don't want to change the method at Runtime, because that would entail reflection and all sorts of things can go wrong when you start reflecting.
Can you pass the protocol to the method as a parameter?Can you retrieve the Protocol from some sort of properties file or Map?
 
Campbell Ritchie
Marshal
Posts: 75665
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
…or can you use a switch (which works on Strings in Java7)
 
George Oliver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell. I think you're right that I don't need to change the actual method. I don't think I want to pass the protocol as a parameter, or write a switch, because that seems like it's hardwiring things more than I'd like, but using a Map looks promising.

Since I can remove and put methods into the map, an option could add a method into the map when it turns on. Then in the send method I could pass the data through each method in the map, and then send the transformed data at the end.

I guess the only problem will be determining some sort of precedence for the methods. Maybe then instead of a Map I could use some kind of List.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

George Oliver wrote:I've been searching around this morning for ways to modify a method at runtime...


Sounds to me like you've leapt to how you're going to do this before you've worked out what needs to be done. And don't worry, we're all guilty of it - me included.

The TCP (or OSI) stack already allows for protocols to be wrapped inside other protocols, so I wonder if you're not overthinking this, particularly if the ones you're using are already properly registered. It might be something as simple as adding "mccp://" to the front of the URL (but I honestly don't know).

I also wonder if this isn't a question for a MUD forum (if there is such a beast) because it sounds to me like the sort of thing that might have already been solved, and it's simply a case of finding the right library.

HIH

Winston
 
George Oliver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's funny, I just read the How/What article here last night, it's a good read.

Though MCCP has protocol in its name it's more of a telnet option, like ECHO, so you need to handle it yourself. Other mud-specific options (MSSP, MSDP) work in a similar way.

There are indeed mud forums! And multiple Java muds and telnet libraries. I have been studying them, but I do have reasons for re-inventing the wheel here (https://docs.google.com/document/d/1rdNUGN2VD5o84HZaQO3T74CmZu41cvlC2leEdr2rb5w/edit?usp=sharing ), or at least I'm good at deluding myself .
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

George Oliver wrote:That's funny, I just read the How/What article here last night, it's a good read.


Glad you liked it (assuming you're referring to WhatNotHow).

Since I really don't know anything about MUD (a bit about Telnet) about the only thing I can suggest is: start small.

You probably already know the drill: get your project working with ONE protocol. Then add another, then another... etc.

'Fraid, if you're rolling your own, there just ain't no easy way.

Winston
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic