~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to proctools/org.eclipse.linuxtools.sequoyah.device/src/org/eclipse/linuxtools/sequoyah/device/tools/memorymap/MemoryMapProcessor.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************************
 
2
 * Copyright (c) 2009 Motorola Inc. All rights reserved.
 
3
 * This program and the accompanying materials are made available under the terms
 
4
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 
5
 * available at http://www.eclipse.org/legal/epl-v10.html
 
6
 *
 
7
 * Initial Contributor:
 
8
 * Otavio Ferranti (Motorola)
 
9
 *
 
10
 * Contributors:
 
11
 * {Name} (company) - description of contribution.
 
12
 ********************************************************************************/
 
13
 
 
14
package org.eclipse.linuxtools.sequoyah.device.tools.memorymap;
 
15
 
 
16
import java.io.IOException;
 
17
import java.util.ArrayList;
 
18
import java.util.Scanner;
 
19
import java.util.regex.MatchResult;
 
20
 
 
21
import org.eclipse.linuxtools.sequoyah.device.LinuxToolsPlugin;
 
22
import org.eclipse.linuxtools.sequoyah.device.network.IConnectionProvider;
 
23
import org.eclipse.linuxtools.sequoyah.device.network.IConstants.CommandCode;
 
24
import org.eclipse.linuxtools.sequoyah.device.network.IConstants.EventCode;
 
25
import org.eclipse.linuxtools.sequoyah.device.tools.AbstractNotifier;
 
26
import org.eclipse.linuxtools.sequoyah.device.tools.IListener;
 
27
import org.eclipse.linuxtools.sequoyah.device.tools.INotifier;
 
28
import org.eclipse.sequoyah.device.common.utilities.logger.ILogger;
 
29
 
 
30
/**
 
31
 * @author Otavio Ferranti
 
32
 */
 
33
public class MemoryMapProcessor extends AbstractNotifier implements IListener {
 
34
 
 
35
        final private String CMD_FETCH_IOMEM = "/proc/iomem"; //$NON-NLS-1$
 
36
        final private String PARSE_PATTERN = "\\s*(\\w{8})-(\\w{8})\\s*:\\s*(.*)"; //$NON-NLS-1$
 
37
 
 
38
        final private String MSG_EXECUTING_COMMAND =
 
39
                Messages.MemoryMapProcessor_Msg_Executing_The_Command;
 
40
 
 
41
        final private String MSG_GOT_RESULT =
 
42
                Messages.MemoryMapProcessor_Msg_Got_The_Result;
 
43
        
 
44
        final private int MAX_COLUMNS = 4;
 
45
        private IConnectionProvider connectionProvider = null;
 
46
 
 
47
        private ILogger logger = null;
 
48
        
 
49
        /**
 
50
         * The constructor;
 
51
         * @param connectionProvider
 
52
         */
 
53
        public MemoryMapProcessor(IConnectionProvider connectionProvider) {
 
54
                setConnectionProvider(connectionProvider);
 
55
                logger = LinuxToolsPlugin.getLogger();
 
56
        }
 
57
        
 
58
        /**
 
59
         * @throws IOException
 
60
         */
 
61
        public void gatherData() throws IOException {
 
62
                connectionProvider.setResponseLength(8192);
 
63
                connectionProvider.sendCommand(CommandCode.FETCH_FILE, this.CMD_FETCH_IOMEM);
 
64
                logger.debug(MSG_EXECUTING_COMMAND + "\n" + this.CMD_FETCH_IOMEM); //$NON-NLS-1$
 
65
        }
 
66
        
 
67
        /* (non-Javadoc)
 
68
         * @see org.eclipse.linuxtools.sequoyah.device.network.IListener#notify(org.eclipse.linuxtools.sequoyah.device.network.INotifier, org.eclipse.linuxtools.sequoyah.device.network.IConstants.EventCode, java.lang.Object)
 
69
         */
 
70
        public void notify(INotifier notifier,
 
71
                                                EventCode event,
 
72
                                                Object result) {
 
73
                if (notifier == this.connectionProvider &&
 
74
                                event == EventCode.EVT_PROVIDER_SENDCOMMAND_FINISHED) {
 
75
                        this.connectionProvider.setResponseLength(1024);
 
76
                        Object[][] parsedResult = parseIomem((StringBuffer) result);
 
77
                        this.notifyListeners(EventCode.EVT_PROCESSOR_GATHERDATA_FINISHED,
 
78
                                        parsedResult);
 
79
                }
 
80
        }
 
81
        
 
82
        /**
 
83
         * @param connectionProvider
 
84
         */
 
85
        public void setConnectionProvider (IConnectionProvider connectionProvider) {
 
86
                if (null != this.connectionProvider) {
 
87
                        this.connectionProvider.removeListener(this);
 
88
                }
 
89
                this.connectionProvider = connectionProvider;
 
90
                if (null != this.connectionProvider) {
 
91
                        this.connectionProvider.addListener(this);
 
92
                }
 
93
        }
 
94
 
 
95
        /**
 
96
         * @param data
 
97
         * @return
 
98
         */
 
99
        private Object[][] parseIomem(StringBuffer data) {
 
100
                logger.debug(MSG_GOT_RESULT + "\n" + data.toString());
 
101
                
 
102
                Scanner s1 = new Scanner(data.toString());
 
103
                
 
104
                ArrayList<String[]> list = new ArrayList<String[]>();
 
105
                
 
106
                int j = 0;
 
107
                
 
108
                while (s1.hasNextLine()) {
 
109
                        Scanner s2 = new Scanner(s1.nextLine());
 
110
                        s2.findInLine(PARSE_PATTERN);
 
111
 
 
112
                        String[] entry = null;
 
113
                        try {
 
114
                                MatchResult result = s2.match();
 
115
                                entry = new String[MAX_COLUMNS];
 
116
                            for (int i = 1; i <= result.groupCount() && i <= MAX_COLUMNS - 1; i++) {
 
117
                                entry[i-1] = result.group(i);
 
118
                            }
 
119
                            entry[MAX_COLUMNS - 1] = new Integer(j).toString();
 
120
                            j++;
 
121
                        } catch (IllegalStateException ise) {
 
122
                                //TODO: Nothing ?
 
123
                        }
 
124
 
 
125
                    s2.close();
 
126
                    if (null != entry) {
 
127
                        list.add(entry);
 
128
                    }
 
129
                }
 
130
            s1.close();
 
131
            
 
132
            String[][] retVal = new String[list.size()][MAX_COLUMNS];
 
133
            for (int i = 0; i < retVal.length; i++) {
 
134
                retVal[i] = list.get(i);
 
135
            }
 
136
                return retVal;
 
137
        }
 
138
}