📄 server.java
字号:
import hla.rti.*;import se.pitch.prti.*;import java.net.URL;import java.io.*;import java.util.*;class Server extends FederateAmbassador_Impl{ RTIambassador _rtiAmbassador; int _messageId; int _parameterIdText; boolean done = false; int serviceCount = 10; String msg = new String(); boolean canSend = false; public static void main(String[] args) { new Server().run(args); } public String doSort (String str) { // convert a string str into an array of 10 numbers here // and sort an array of numbers, then return a new sorted string // Hint: use StringTokenizer to extract numbers from a string. // Any simple sorting algorithm (e.g., Bubble sort) will do. // -------------------------------------------------------------- // -------------------------------------------------------------- } public void sendData(String message) { // Sends processed data to clients try { SuppliedParameters parameters = RTI.suppliedParametersFactory().create(1); byte[] value = message.getBytes(); parameters.add(_parameterIdText, value); _rtiAmbassador.sendInteraction(_messageId, parameters, null); } catch (Exception e) { // concurrent access e.printStackTrace(); } } public void run(String[] args) { try { String rtiHost = (args.length > 0) ? args[0] : "localhost"; _rtiAmbassador = RTI.getRTIambassador(rtiHost, 8989); try { _rtiAmbassador.createFederationExecution( "ChatRoom", new URL("file:chat.fed")); } catch (FederationExecutionAlreadyExists ignored) { } _rtiAmbassador.joinFederationExecution("Server", "ChatRoom", this); _messageId = _rtiAmbassador.getInteractionClassHandle( "Communication"); _parameterIdText = _rtiAmbassador.getParameterHandle( "Message", _messageId); _rtiAmbassador.subscribeInteractionClass(_messageId); _rtiAmbassador.publishInteractionClass(_messageId); while (done == false) { // Send data if available if (canSend == true) { // can only send data once per each client request try { // create parameter list with 1 parameter SuppliedParameters parameters = RTI.suppliedParametersFactory().create(1); // convert a string to be sent into a byte array byte[] value = msg.getBytes(); // add a value to the parameter parameters.add(_parameterIdText, value); // broadcast message to all clients _rtiAmbassador.sendInteraction(_messageId, parameters, null); // do not send until a new request arrives canSend = false; } catch (Exception e) { // trap concurrent access to receive and send e.printStackTrace(); } } // if } // Exit federation execution _rtiAmbassador.resignFederationExecution( ResignAction.DELETE_OBJECTS_AND_RELEASE_ATTRIBUTES); try { _rtiAmbassador.destroyFederationExecution("ChatRoom"); } catch (FederatesCurrentlyJoined ignored) { } } catch (Exception e) { e.printStackTrace(); } } public void receiveInteraction ( int interactionClass, ReceivedInteraction theInteraction, byte[] userSuppliedTag) throws InteractionClassNotKnown, InteractionParameterNotKnown, FederateInternalError { try { if (interactionClass == _messageId) { String message = ""; for (int i = 0; i < theInteraction.size(); ++i) { if (theInteraction.getParameterHandle(i) == _parameterIdText) { message = new String(theInteraction.getValueReference(i)); System.out.println("Client Request: " + message); msg = doSort(message); } } // Add a code to display a sorted list here -------------------- // ------------------------------------------------------------- canSend = true; } } catch (ArrayIndexOutOfBounds e) { System.out.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -