ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/agents/PeerHandler.java
(Generate patch)

Comparing experimental/agents/PeerHandler.java (file contents):
Revision 1.1 by ajm, Fri May 4 02:04:35 2001 UTC vs.
Revision 1.2 by ajm, Sun May 6 19:01:48 2001 UTC

# Line 32 | Line 32 | class PeerHandler extends Handler {
32      public PeerHandler(Socket socket) throws IOException {
33          super(socket);
34          // setup reader & writer
35 <        _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
35 >        _objectOut = new ObjectOutputStream(_socket.getOutputStream());
36 >        _objectIn = new ObjectInputStream(_socket.getInputStream());
37          _logger.write(toString(), Logger.SYSINIT, "created");
38      }
39  
# Line 41 | Line 42 | class PeerHandler extends Handler {
42      public void run() {
43          _logger.write(toString(), Logger.SYSINIT, "started");
44          try {    
45 <
45 >            
46              while(_running) {
47                  // VERY basic protocol, only allow sending of agents ;-)
48 <                getInBoundMessage("SENDAGENT");
49 <                receiveAgent();
48 >                try {
49 >                    String message = (String) _objectIn.readObject();
50 >                    if (message.equals("SENDAGENT")) {
51 >                        receiveAgent();
52 >                    } else {
53 >                        _logger.write(toString(), Logger.ERROR, "protocol error expected: SENDAGENT got: " + message);
54 >                    }
55 >                } catch (ClassNotFoundException e) {
56 >                    _logger.write(toString(), Logger.ERROR, "object protocol error expected: a String got: a " + e.getMessage());
57 >                }
58              }
59              
60          } catch (IOException e) {
# Line 67 | Line 76 | class PeerHandler extends Handler {
76              // stream types (ie, objectstream and printwriter) can be used together
77              // needs investigating
78              
79 <            PrintWriter socketOut = new PrintWriter(_socket.getOutputStream(), true);
80 <            socketOut.println("SENDAGENT");
81 <            socketOut.close();
73 <            
79 >            _objectOut.writeObject("SENDAGENT");
80 >            _objectOut.flush();
81 >                        
82              // send agent bytecode
83              
84 <            // TODO
84 >            // TODO - very dodgy and crude at the moment.
85 >            AgentClassLoader cl = new AgentClassLoader(ClassLoader.getSystemClassLoader());
86              
87 +            // send the super class
88 +            byte[] byteCode = cl.getBytecode(agent.getClass().getSuperclass().getName());
89 +            _objectOut.writeInt(byteCode.length);
90 +            _objectOut.flush();
91 +            _objectOut.write(byteCode);
92 +            _objectOut.flush();
93 +            
94 +            // send the agent
95 +            byteCode = cl.getBytecode(agent.getClass().getName());
96 +            _objectOut.writeInt(byteCode.length);
97 +            _objectOut.flush();
98 +            _objectOut.write(byteCode);
99 +            _objectOut.flush();
100 +            
101              // send agent state (serialized object)
79            ObjectOutputStream objectOutStream = new ObjectOutputStream(_socket.getOutputStream());
80            AgentStation.getInstance().removeAgent(agent);
81            objectOutStream.writeObject(agent);
82            objectOutStream.flush();
83            objectOutStream.close();
102              
103 +            AgentStation.getInstance().removeAgent(agent);
104 +            _objectOut.writeObject(agent);
105 +            _objectOut.flush();
106 +                        
107              _logger.write(toString(), Logger.SYSMSG, "agent sent");
108              result = true;
109          } catch (IOException e) {
# Line 124 | Line 146 | class PeerHandler extends Handler {
146      private void receiveAgent() throws IOException {
147          _logger.write(toString(), Logger.SYSMSG, "starting agent transfer - inbound");
148          try {
149 +            // TODO - very crude
150              // receive agent bytecode
151 +            AgentClassLoader cl = new AgentClassLoader(ClassLoader.getSystemClassLoader());
152              
153 <            // TODO
153 >            // first the super class
154 >            int givenLength = _objectIn.readInt();
155 >            byte[] byteCode = new byte[givenLength];
156 >            _objectIn.readFully(byteCode);
157 >            cl.defineAgentClass(byteCode);
158 >            
159 >            // then the agent
160 >            
161 >            givenLength = _objectIn.readInt();
162 >            byteCode = new byte[givenLength];
163 >            _objectIn.readFully(byteCode);
164 >            cl.defineAgentClass(byteCode);
165 >            
166          
167              // receive agent state (serialized object)
168 <            ObjectInputStream objectInStream = new ObjectInputStream(_socket.getInputStream());
133 <            Agent agent = (Agent) objectInStream.readObject();
168 >            Agent agent = (Agent) _objectIn.readObject();
169              AgentStation.getInstance().addAgent(agent);
170                      
171              _logger.write(toString(), Logger.DEBUG, "Agent state received");
# Line 145 | Line 180 | class PeerHandler extends Handler {
180              Thread thread = new Thread(agent);
181              thread.start();
182              
148            objectInStream.close();
183              _logger.write(toString(), Logger.SYSMSG, "agent received");
184              
185          } catch (ClassNotFoundException e) {
# Line 153 | Line 187 | class PeerHandler extends Handler {
187          }
188      }
189      
156    /**
157     * Get the next inbound string from the socket and expect it
158     * to be the given text.
159     *
160     * @param expected the expected text
161     *
162     * @throws IOException on I/O error or protocol error
163     */
164     private String getInBoundMessage(String expected) throws IOException {
165        // grab the input
166        String inBound = getInBoundMessage();
167        // check if it's what we're expecting
168        if(!inBound.equals(expected)) {
169            throw new IOException("protocol error - expected:" + expected + " got:" + inBound);
170        }
171        // it should be ok then
172        return inBound;
173    }
174    
175    /**
176     * Get the next inbound string from the socket.
177     *
178     * @throws IOException on I/O error or protocol error
179     */
180    private String getInBoundMessage() throws IOException {
181        // grab the input
182        String inBound = _socketIn.readLine();
183        // check for null's, likely disconnection
184        if(inBound == null) {
185            throw new IOException("got null from peer, maybe it died");
186        }
187        // it's a valid message it seems
188        return inBound;
189    }
190
190   //---ACCESSOR/MUTATOR METHODS---
191  
192   //---ATTRIBUTES---
# Line 218 | Line 217 | class PeerHandler extends Handler {
217      /**
218       * Used for the input stream of this socket
219       */
220 <    private BufferedReader _socketIn;
221 <    
220 >    private ObjectInputStream _objectIn;
221 >
222 >    /**
223 >     * Used for the output stream of this socket
224 >     */
225 >    private ObjectOutputStream _objectOut;
226 >        
227   //---STATIC ATTRIBUTES---
228  
229   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines