弄Rpc有一段时间了,从定义IDL规则,自动生成存根代码给应用使用,相继做了c++,java,python,actionscript四个版本,且都都能互相调用,协议是自定义的二进制格式。 公司的项目要求我从事android的开发,编写一些小的应用,要与平台服务器通信。 与服务器的通信采用xml交换,之前做的二进制编码便不能使用。 不想很弱智的去重复的编解码xml消息格式,去编写socket通信代码,工作量和繁琐是自己不喜欢的,那只能让自己的rpc去支持xml格式。 这种支持我称之为半人工的,因为xml格式的tag定义必须在idl中出现 idl定义 1 struct Ts_heartbeat_p_User_t{ 2 string id; 3 }; 4 //心跳 5 6 struct Ts_gps_p_GPS_t{ 7 float lon; 8 float lat; 9 float speed; 10 float direction; 11 int time; 12 }; 13 14 struct Ts_gps_p_Properties_t{ 15 string senderid; 16 string target_type; 17 string users; 18 string groups; 19 }; 20 21 struct Ts_verify_r_User_t{ 22 string id; 23 }; 24 25 struct Ts_verify_r_Group_t{ 26 string id; 27 string name; 28 string type; 29 }; 30 31 sequence<Ts_verify_r_Group_t> Ts_verify_r_Groups_t; 32 33 struct Ts_verify_r_Result_t{ 34 int code; 35 string msg; 36 Ts_verify_r_User_t user; 37 Ts_verify_r_Groups_t groups; 38 }; 39 40 41 struct Ts_verify_p_User_t{ 42 string token; 43 }; 44 45 interface Ts{ 46 void heartbeat(Ts_heartbeat_p_User_t p); 47 void gps(Ts_gps_p_GPS_t p,Ts_gps_p_Properties_t props); 48 Ts_verify_r_Result_t verify(Ts_verify_p_User_t p); 49 }; 50 idl定义了一个Ts的接口,内部有三个方法:heartbeat(),gps(),verify() 这些方法在服务器端已经实现,其他的struct类型定义了接口调用使用的参数和返回值类型 ,注意他们的格式: struct Ts_verify_p_User_t{ string token; }; 这个类型将被解释成 <User token="..."/>
sequence<Ts_verify_r_Group_t> Ts_verify_r_Groups_t; 将被解释成 <Groups> <Group id="" name="" type=""/> ... </Groups> 努力不是白费的,python+ply实现了idl的语法解析,并生成java框架代码,并编写通信代码完成Rpc构造。 必须要注意Android不允许任何阻塞UI线程的操作,所以要实现通信必须在自己的线程中进行,我提供的Rpc调用模式只能是 oneway(单向调用) ,async(异步调用) 实例: 1 package test; 2 3 4 import sns_mobile.*; 5 6 import java.util.*; 7 import tce.*; 8 9 import java.nio.*; 10 import java.io.*; 11 12 public class SnsTestMain { 13 TsProxy tsprx = null; 14 String token="dBmPPfxxohFmXjx4SUv46BffzmeYitbjmzT2mRHLttGaau4kiTvKEOpW2BJE0IGLEBFZ4lE/KrMKl7qE6pNmaBUfbixMrPEG/1Xfsp36P4lmZHhQfkVgyGWa8yi5g/swbMpg8h2ybC2dQShR18VFtQkXGpEUg8VfV1Fb0UllN6j/umVaBBQX7lgqcf3zkIaGcOpvS7FB6snC/PWU1crCf2gHrgnOmZgIaKmBF14dkJsSoozRGKiIpiCjJ7mRZ05og67ZmFfOaH2EnaCo3G1pmQcez7MOqCtIvNpGo5yivmZ6GyMA2eSIlFmD2Z7ysCrxzAt92UUCNwb9mkKZrUthjA=="; 15 16 double locs[]={436914.309600,112259.854800,436914.309600,112259.847600,436914.298800,112259.847600,436914.298800,112259.844000,436914.291600,112259.844000,436914.288000,112259.847600,436914.298800,112259.847600,436914.298800,112259.844000,436914.306000,112259.836800,436914.309600,112259.836800,436914.316800,112259.826000,436914.324000,112259.811600,436914.334800,112259.793600,436914.345600,112259.775600,436914.352800,112259.775600,436914.360000,112259.775600,436914.370800,112259.764800,436914.378000,112259.757600,436914.381600,112259.739600,436914.381600,112259.754000,436914.381600,112259.808000,436914.352800,112259.883600,436914.316800,112260.024000,436914.280800,112260.186000,436914.244800,112260.376800,436914.234000,112260.600000,436914.201600,112260.834000,436914.165600,112261.086000,436914.118800,112261.359600,436914.075600,112261.654800,436914.036000,112261.942800,436914.000000,112262.230800,436913.910000,112262.562000,436913.841600,112262.907600,436913.794800,112263.285600,436913.733600,112263.660000,436913.676000,112264.038000,436913.614800,112264.401600,436913.553600,112264.776000,436913.517600,112265.128800,436913.445600,112265.463600,436913.373600,112265.805600,436913.334000,112266.180000,436913.290800,112266.540000,436913.244000,112266.910800,436913.244000,112266.910800,436913.139600,112267.695600,436913.139600,112267.695600,436913.139600,112267.695600,436913.056800,112268.440800,436913.002800,112268.793600,436912.923600,112269.081600,436912.920000,112269.276000,436912.938000,112269.376800,436912.995600,112269.369600,436913.013600,112269.366000,436913.020800,112269.394800,436913.013600,112269.423600,436913.010000,112269.441600,436913.020800,112269.459600,436913.010000,112269.495600,436913.010000,112269.513600,436913.002800,112269.531600,436912.992000,112269.546000,436912.977600,112269.564000,436912.974000,112269.567600,436912.966800,112269.582000,436912.956000,112269.585600,436912.948800,112269.592800,436912.941600,112269.600000,436912.938000,112269.610800,436912.930800,112269.618000,436912.923600,112269.621600,436912.923600,112269.636000,436912.923600,112269.639600,436912.923600,112269.639600,436912.923600,112269.639600,436912.920000,112269.646800,436912.920000,112269.654000,436912.920000,112269.657600,436912.912800,112269.664800,436912.920000,112269.664800,436912.912800,112269.672000,436912.912800,112269.675600,436912.912800,112269.682800,436912.912800,112269.693600,436912.912800,112269.693600,436912.905600,112269.700800,436912.905600,112269.700800,436912.905600,112269.700800,436912.902000,112269.711600,436912.902000,112269.711600,436912.902000,112269.726000,436912.894800,112269.736800,436912.894800,112269.747600,436912.887600,112269.772800,436912.884000,112269.801600,436912.884000,112269.801600,436912.858800,112269.852000,436912.851600,112269.888000,436912.833600,112269.924000,436912.815600,112269.963600,436912.797600,112270.032000,436912.768800,112270.104000,436912.768800,112270.104000,436912.707600,112270.500000,436912.660800,112270.719600,436912.617600,112270.942800,436912.560000,112271.205600,436912.527600,112271.508000,436912.455600,112271.824800,436912.372800,112272.148800,436912.311600,112272.498000,436912.236000,112272.850800,436912.149600,112273.218000,436912.059600,112273.588800,436911.966000,112273.959600,436911.861600,112274.344800,436911.753600,112274.715600,436911.642000,112275.100800,436911.642000,112275.100800,436911.346800,112275.838800,436911.202800,112276.173600,436911.051600,112276.476000,436910.896800,112276.785600,436910.860800,112277.055600,436910.796000,112277.271600,436910.727600,112277.469600,436910.670000,112277.620800,436910.634000,112277.721600,436910.644800,112277.746800,436910.619600,112277.757600,436910.616000,112277.764800,436910.616000,112277.764800,436910.619600,112277.782800,436910.616000,112277.782800,436910.616000,112277.782800,436910.608800,112277.775600,436910.608800,112277.772000,436910.608800,112277.772000,436910.608800,112277.772000,436910.601600,112277.772000,436910.598000,112277.764800,436910.583600,112277.757600,436910.583600,112277.754000,436910.580000,112277.746800,436910.580000,112277.739600,436910.580000,112277.739600,436910.572800,112277.728800,436910.565600,112277.721600,436910.562000,112277.736000,436910.554800,112277.736000,436910.547600,112277.736000,436910.536800,112277.721600,436910.529600,112277.721600,436910.518800,112277.721600,436910.508000,112277.703600,436910.500800,112277.710800,436910.500800,112277.721600,436910.500800,112277.721600,436910.490000,112277.721600,436910.490000,112277.728800,436910.490000,112277.728800,436910.482800,112277.728800,436910.482800,112277.721600,436910.475600,112277.718000,436910.464800,112277.718000,436910.446800,112277.710800,436910.436000,112277.703600,436910.418000,112277.703600,436910.403600,112277.703600,436910.385600,112277.692800,436910.382000,112277.700000,436910.374800,112277.692800,436910.364000,112277.685600,436910.356800,112277.685600,436910.356800,112277.685600,436910.349600,112277.685600,436910.349600,112277.685600,436910.346000,112277.692800,436910.346000,112277.692800,436910.338800,112277.692800,436910.338800,112277.692800,436910.338800,112277.692800,436910.328000,112277.685600,436910.328000,112277.685600,436910.320800,112277.685600,436910.313600,112277.685600,436910.310000,112277.682000,436910.310000,112277.682000,436910.310000,112277.682000,436910.302800,112277.674800,436910.302800,112277.667600,436910.295600,112277.667600,436910.292000,112277.664000,436910.292000,112277.664000,436910.292000,112277.667600,436910.277600,112277.667600,436910.266800,112277.667600,436910.259600,112277.667600,436910.248800,112277.674800,436910.241600,112277.674800,436910.238000,112277.674800,436910.223600,112277.667600,436910.220000,112277.667600,436910.220000,112277.667600,436910.212800,112277.667600,436910.220000,112277.674800,436910.220000,112277.674800,436910.223600,112277.682000,436910.223600,112277.685600,436910.220000,112277.700000,436910.202000,112277.703600,436910.176800,112277.739600,436910.140800,112277.790000,436910.122800,112277.862000,436910.079600,112277.901600,436910.043600,112277.973600,436910.007600,112278.042000,436909.953600,112278.132000,436909.914000,112278.207600,436909.647600,112278.402000,436909.528800,112278.924000,436909.410000,112279.230000,436909.276800,112279.518000,436909.143600,112279.770000,436909.071600,112279.968000,436908.996000,112280.166000,436908.898800,112280.385600,436908.808800,112280.634000,436908.754800,112280.904000,436908.682800,112281.220800,436908.610800,112281.490800,436908.513600,112281.807600,436908.351600,112282.239600,436908.207600,112282.653600,436908.063600,112283.056800,436907.898000,112283.445600,436907.746800,112283.802000,436907.746800,112283.802000,436907.343600,112284.496800,436907.188800,112284.813600,436907.052000,112285.116000,436906.947600,112285.422000,436906.857600,112285.720800,436906.720800,112286.055600,436906.720800,112286.055600,436906.440000,112286.674800,436906.440000,112286.674800,436906.288800,112287.042000,436906.288800,112287.042000,436906.000800,112287.934800,436905.864000,112288.330800,436905.712800,112288.701600,436905.558000,112289.061600,436905.414000,112289.410800,436905.288000,112289.745600,436905.162000,112290.084000,436905.036000,112290.408000,436905.039600,112290.699600,436905.057600,112291.005600,436905.093600,112291.200000,436905.324000,112291.326000,436905.478800,112291.473600,436905.756000,112291.506000,436906.054800,112291.506000,436906.353600,112291.524000,436906.677600,112291.542000,436907.008800,112291.563600,436907.358000,112291.588800,436907.728800,112291.596000,436908.114000,112291.596000,436908.520800,112291.596000,436908.960000,112291.560000,436908.960000,112291.560000,436909.863600,112291.480800,436909.863600,112291.480800,436910.320800,112291.437600,436910.320800,112291.437600,436911.246000,112291.380000,436911.246000,112291.380000,436911.246000,112291.380000,436912.149600,112291.362000,436912.578000,112291.380000,436912.948800,112291.398000,436913.283600,112291.416000,436913.578800,112291.419600,436913.820000,112291.426800,436914.010800,112291.426800,436914.172800,112291.437600,436914.324000,112291.444800,436914.435600,112291.437600,436914.514800,112291.434000,436914.576000,112291.437600,436914.622800,112291.434000,436914.648000,112291.434000,436914.658800,112291.444800,436914.666000,112291.444800,436914.669600,112291.452000,436914.669600,112291.455600,436914.676800,112291.452000,436914.676800,112291.452000,436914.687600,112291.452000,436914.694800,112291.452000,436914.694800,112291.452000,436914.694800,112291.444800,436914.702000,112291.452000,436914.702000,112291.444800,436914.702000,112291.452000,436914.694800,112291.452000,436914.694800,112291.452000,436914.687600,112291.455600,436914.687600,112291.470000,436914.687600,112291.473600,436914.684000,112291.488000,436914.676800,112291.491600,436914.676800,112291.506000,436914.669600,112291.516800,436914.669600,112291.524000,436914.669600,112291.524000,436914.666000,112291.527600,436914.666000,112291.534800,436914.658800,112291.534800,436914.651600,112291.542000,436914.648000,112291.545600,436914.640800,112291.545600,436914.640800,112291.552800,436914.633600,112291.545600,436914.630000,112291.542000,436914.630000,112291.545600,436914.666000,112291.545600,436914.756000,112291.545600,436914.900000,112291.542000,436914.900000,112291.542000,436915.342800,112291.578000,436915.342800,112291.578000,436915.342800,112291.578000,436915.929600,112291.632000,436916.271600,112291.650000,436916.628000,112291.668000,436916.998800,112291.686000,436917.402000,112291.707600,436917.816000,112291.740000,436918.230000,112291.768800,436918.654800,112291.794000,436919.094000,112291.833600,436919.544000,112291.887600,436919.986800,112291.966800,436920.400800,112292.046000,436920.771600,112292.139600,436921.131600,112292.218800,436921.488000,112292.308800,436921.848000,112292.398800,436922.200800,112292.481600,436922.532000,112292.560800,436922.830800,112292.632800,436923.118800,112292.715600,436923.406800,112292.805600,436923.712800,112292.902800,436924.054800,112292.992800,436924.432800,112293.082800,436924.821600,112293.190800,436925.235600,112293.316800,436925.235600,112293.316800,436926.142800,112293.568800,436926.636000,112293.702000,436927.125600,112293.864000,436927.611600,112294.018800,436928.112000,112294.170000,436928.619600,112294.332000,436928.619600,112294.332000,436929.616800,112294.666800,436930.113600,112294.857600,436930.588800,112295.037600,436931.031600,112295.199600,436931.424000,112295.361600,436931.787600,112295.505600,436932.090000,112295.638800,436932.334800,112295.754000,436932.540000,112295.847600,436932.705600,112295.934000,436932.820800,112295.988000,436932.892800,112296.027600,436932.918000,112296.042000,436932.921600,112296.042000,436932.921600,112296.045600,436932.928800,112296.042000,436932.928800,112296.045600,436932.936000,112296.042000,436932.936000,112296.045600,436932.928800,112296.045600,436932.928800,112296.060000,436932.921600,112296.070800,436932.921600,112296.070800,436932.928800,112296.063600,436932.939600,112296.052800,436932.936000,112296.063600,436932.928800,112296.078000,436932.928800,112296.070800,436932.936000,112296.070800,436932.936000,112296.070800,436932.936000,112296.078000,436932.939600,112296.070800,436932.946800,112296.063600,436932.954000,112296.060000,436932.954000,112296.060000,436932.946800,112296.070800,436932.954000,112296.078000,436932.946800,112296.078000,436932.939600,112296.096000,436932.939600,112296.096000,436932.939600,112296.081600,436932.939600,112296.078000,436932.939600,112296.078000,436932.939600,112296.070800,436932.939600,112296.063600,436932.939600,112296.078000,436932.939600,112296.078000,436932.928800,112296.106800,436932.928800,112296.106800,436932.918000,112296.117600,436932.918000,112296.117600,436932.918000,112296.117600,436932.921600,112296.124800,436932.910800,112296.142800,436932.903600,112296.135600,436932.903600,112296.132000,436932.900000,112296.153600,436932.892800,112296.160800,436932.892800,112296.168000,436932.892800,112296.168000,436932.885600,112296.171600,436932.882000,112296.186000,436932.892800,112296.196800,436932.946800,112296.214800,436933.047600,112296.243600,436933.206000,112296.297600,436933.404000,112296.384000,436933.630800,112296.492000,436933.890000,112296.621600,436934.188800,112296.765600,436934.188800,112296.765600,436934.847600,112297.096800,436935.196800,112297.251600,436935.538800,112297.413600,436935.880800,112297.593600,436936.248000,112297.752000,436936.618800,112297.953600,436937.007600,112298.176800,436937.400000,112298.385600,436937.400000,112298.385600,436938.249600,112298.824800,436938.732000,112299.040800,436939.218000,112299.282000,436939.707600,112299.519600,436940.211600,112299.760800,436940.722800,112300.005600,436941.226800,112300.254000,436941.730800,112300.516800,436942.242000,112300.779600,436942.749600,112301.056800,436943.235600,112301.337600,436943.703600,112301.622000,436944.150000,112301.884800,436944.567600,112302.162000,436944.978000,112302.406800,436945.359600,112302.658800,436945.359600,112302.658800,436946.061600,112303.108800,436946.374800,112303.296000,436946.374800,112303.296000,436946.374800,112303.296000,436946.878800,112303.605600,436947.105600,112303.767600,436947.364800,112303.929600,436947.642000,112304.106000,436947.948000,112304.296800,436948.275600,112304.494800,436948.635600,112304.700000,436948.995600,112304.898000,436949.370000,112305.106800,436949.766000,112305.333600,436950.183600,112305.592800,436950.612000,112305.862800,436951.047600,112306.140000,436951.479600,112306.410000,436951.929600,112306.672800,436952.376000,112306.942800,436952.811600,112307.223600,436952.811600,112307.223600,436953.693600,112307.778000,436954.125600,112308.030000,436954.536000,112308.282000,436954.924800,112308.519600,436955.302800,112308.760800,436955.680800,112308.994800,436956.033600,112309.218000,436956.357600,112309.426800,436956.642000,112309.606800,436956.876000,112309.750800,436957.077600,112309.876800,436957.077600,112309.876800,436957.383600,112310.064000,436957.488000,112310.128800,436957.570800,112310.172000,436957.635600,112310.200800,436957.689600,112310.236800,436957.732800,112310.254800,436957.758000,112310.272800,436957.768800,112310.265600,436957.776000,112310.265600,436957.768800,112310.262000,436957.776000,112310.262000,436957.768800,112310.265600,436957.768800,112310.262000,436957.776000,112310.265600,436957.797600,112310.283600,436957.866000,112310.326800,436957.948800,112310.373600,436958.067600,112310.445600,436958.211600,112310.532000,436958.373600,112310.640000,436958.560800,112310.748000,436958.766000,112310.866800,436959.000000,112311.003600,436959.270000,112311.162000,436959.540000,112311.316800,436959.828000,112311.489600,436960.144800,112311.684000,436960.494000,112311.900000,436960.857600,112312.126800,436961.253600,112312.360800,436961.656800,112312.623600,436962.070800,112312.893600,436962.502800,112313.178000,436962.952800,112313.469600,436963.402800,112313.746800,436963.860000,112314.024000,436964.320800,112314.294000,436964.320800,112314.294000,436965.220800,112314.826800,436965.688800,112315.071600,436966.185600,112315.287600,436966.686000,112315.503600,436967.175600,112315.708800,436967.676000,112315.899600,436968.162000,112316.094000,436968.612000,112316.248800,436969.011600,112316.400000,436969.378800,112316.526000,436969.702800,112316.637600,436969.998000,112316.752800,436970.250000,112316.860800,436970.469600,112316.940000,436970.656800,112317.004800,436970.818800,112317.058800,436970.973600,112317.112800,436971.142800,112317.174000,436971.315600,112317.231600,436971.510000,112317.303600,436971.726000,112317.372000,436971.978000,112317.447600,436972.266000,112317.537600,436972.590000,112317.645600,436972.932000,112317.753600,436973.292000,112317.861600,436973.680800,112317.969600,436974.069600,112318.066800,436974.465600,112318.156800,436974.850800,112318.239600,436975.246800,112318.326000,436975.246800,112318.326000,436976.092800,112318.488000,436976.517600,112318.570800,436976.982000,112318.678800,436977.450000,112318.794000,436977.910800,112318.923600,436978.368000,112319.046000,436978.821600,112319.164800,436979.289600,112319.290800,436979.782800,112319.424000,436980.297600,112319.535600,436980.819600,112319.643600,436981.356000,112319.758800,436981.896000,112319.874000,436982.439600,112320.003600,436983.004800,112320.136800,436983.570000,112320.270000,436984.149600,112320.417600,436984.732800,112320.568800,436985.298000,112320.741600,436985.859600,112320.921600,436986.424800,112321.108800,436986.982800,112321.317600,436987.540800,112321.540800,436988.088000,112321.764000,436988.620800,112322.008800,436989.142800,112322.260800,436989.654000,112322.523600,436990.143600,112322.790000,436990.618800,112323.052800,436991.094000,112323.286800,436991.580000,112323.502800,436992.066000,112323.690000,436992.534000,112323.837600,436992.951600,112323.978000,436993.318800,112324.089600,436993.624800,112324.179600,436993.887600,112324.251600,436993.887600,112324.251600,436994.298000,112324.374000,436994.445600,112324.420800,436994.568000,112324.464000,436994.640000,112324.482000,436994.650800,112324.485600,436994.650800,112324.485600,436994.643600,112324.482000,436994.643600,112324.482000,436994.643600,112324.474800,436994.643600,112324.482000,436994.643600,112324.482000,436994.640000,112324.482000,436994.640000,112324.482000,436994.640000,112324.474800,436994.640000,112324.474800,436994.640000,112324.467600,436994.640000,112324.464000,436994.640000,112324.456800,436994.640000,112324.449600,436994.640000,112324.446000,436994.640000,112324.446000,436994.643600,112324.438800,436994.640000,112324.438800,436994.640000,112324.438800,436994.640000,112324.438800,436994.643600,112324.431600,436994.650800,112324.431600,436994.650800,112324.428000,436994.650800,112324.420800,436994.650800,112324.413600,436994.658000,112324.410000,436994.658000,112324.402800,436994.661600,112324.395600,436994.661600,112324.395600,436994.668800,112324.395600,436994.676000,112324.392000,436994.679600,112324.384800,436994.686800,112324.377600,436994.694000,112324.377600,436994.697600,112324.377600,436994.704800,112324.377600,436994.712000,112324.377600,436994.712000,112324.377600,436994.715600,112324.377600,436994.715600,112324.384800,436994.722800,112324.384800,436994.722800,112324.384800,436994.722800,112324.384800,436994.730000,112324.384800,436994.730000,112324.384800,436994.733600,112324.392000,436994.740800,112324.392000,436994.740800,112324.395600,436994.740800,112324.395600,436994.766000,112324.402800,436994.838000,112324.420800,436994.949600,112324.456800,436995.090000,112324.492800,436995.244800,112324.521600,436995.432000,112324.539600,436995.612000,112324.539600,436995.784800,112324.536000,436995.936000,112324.500000,436996.054800,112324.428000,436996.170000,112324.287600,436996.299600,112324.089600,436996.378800,112323.906000,436996.468800,112323.690000,436996.551600,112323.441600,436996.648800,112323.186000,436996.756800,112322.916000,436996.854000,112322.638800,436996.972800,112322.379600,436997.055600,112322.134800,436997.055600,112322.134800,436997.232000,112321.648800,436997.314800,112321.386000,436997.415600,112321.116000,436997.548800,112320.813600,436997.682000,112320.507600,436997.800800,112320.198000,436997.908800,112319.892000,436997.991600,112319.586000,436998.060000,112319.316000,436998.114000,112319.049600,436998.186000,112318.858800,436998.268800,112318.725600,436998.286800,112318.624800,436998.297600,112318.560000,436998.304800,112318.534800,436998.312000,112318.524000,436998.294000,112318.506000,436998.276000,112318.498800,436998.261600,112318.498800,436998.258000,112318.491600,436998.258000,112318.491600,436998.243600,112318.480800,436998.243600,112318.473600,436998.232800,112318.470000,436998.225600,112318.462800,436998.222000,112318.452000,436998.207600,112318.444800,436998.214800,112318.434000,436998.222000,112318.408800,436998.225600,112318.354800,436998.243600,112318.290000,436998.240000,112318.236000,436998.207600,112318.182000,436998.214800,112318.156800,436998.214800,112318.149600,436998.225600,112318.156800,436998.232800,112318.156800,436998.232800,112318.156800,436998.232800,112318.156800,436998.243600,112318.164000,436998.243600,112318.167600,436998.240000,112318.167600,436998.240000,112318.167600,436998.225600,112318.167600,436998.225600,112318.174800,436998.214800,112318.174800,436998.207600,112318.167600,436998.196800,112318.174800,436998.186000,112318.174800,436998.178800,112318.174800,436998.178800,112318.174800,436998.168000,112318.182000,436998.160800,112318.182000,436998.153600,112318.182000,436998.150000,112318.185600,436998.150000,112318.185600,436998.142800,112318.192800,436998.142800,112318.192800,436998.135600,112318.192800,436998.135600,112318.185600,436998.132000,112318.185600,436998.132000,112318.164000,436998.135600,112318.102800,436998.135600,112318.020000,436998.117600,112317.922800,436998.070800,112317.807600,436998.045600,112317.678000,436998.045600,112317.678000,436998.016800,112317.444000,436998.006000,112317.310800,436997.998800,112317.202800,436998.052800,112317.040800,436998.214800,112316.796000,436998.261600,112316.619600,436998.322800,112316.482800,436998.322800,112316.482800,436998.322800,112316.482800,436998.441600,112316.205600,436998.502800,112316.032800,436998.574800,112315.834800,436998.657600,112315.600800,436998.747600,112315.338000,436998.855600,112315.042800,436998.945600,112314.718800,436999.053600,112314.405600,436999.176000,112314.099600,436999.302000,112313.782800,436999.428000,112313.469600,436999.528800,112313.178000,436999.644000,112312.911600,436999.734000,112312.666800,436999.824000,112312.422000,436999.906800,112312.188000,437000.004000,112311.964800,437000.086800,112311.756000,437000.158800,112311.568800,437000.158800,112311.568800,437000.284800,112311.252000,437000.284800,112311.252000,437000.284800,112311.252000,437000.364000,112311.003600,437000.364000,112311.003600,437000.364000,112311.003600,437000.392800,112310.841600,437000.367600,112310.740800,437000.392800,112310.658000,437000.428800,112310.578800,437000.446800,112310.506800,437000.457600,112310.442000,437000.454000,112310.352000,437000.446800,112310.236800,437000.464800,112310.121600,437000.500800,112310.002800,437000.547600,112309.866000,437000.616000,112309.722000,437000.698800,112309.578000,437000.778000,112309.473600,437000.824800,112309.390800,437000.868000,112309.336800,437000.914800,112309.290000,437000.950800,112309.236000,437000.979600,112309.164000,437001.022800,112309.066800,437001.058800,112308.969600,437001.058800,112308.969600,437001.120000,112308.771600,437001.130800,112308.670800,437001.174000,112308.573600,437001.213600,112308.472800,437001.264000,112308.357600,437001.321600,112308.213600,437001.382800,112308.069600,437001.465600,112307.922000,437001.562800,112307.770800,437001.642000,112307.626800,437001.699600,112307.464800,437001.771600,112307.284800,437001.832800,112307.079600,437001.879600,112306.881600,437001.879600,112306.665600,437001.933600,112306.500000,437001.966000,112306.359600,437002.005600,112306.230000,437002.048800,112306.078800,437002.092000,112305.927600,437002.146000,112305.783600,437002.185600,112305.636000,437002.239600,112305.484800,437002.300800,112305.333600,437002.365600,112305.196800,437002.419600,112305.060000,437002.419600,112305.060000,437002.491600,112304.826000,437002.534800,112304.721600,437002.570800,112304.620800,437002.596000,112304.520000,437002.606800,112304.430000,437002.624800,112304.343600,437002.635600,112304.278800,437002.632000,112304.232000,437002.635600,112304.196000,437002.635600,112304.170800,437002.635600,112304.152800,437002.614000,112304.142000,437002.588800,112304.134800,437002.563600,112304.124000,437002.542000,112304.116800,437002.542000,112304.109600,437002.527600,112304.106000,437002.527600,112304.088000,437002.534800,112304.062800,437002.545600,112304.019600,437002.570800,112303.980000,437002.596000,112303.929600,437002.632000,112303.872000,437002.668000,112303.785600,437002.707600,112303.713600,437002.750800,112303.638000,437002.804800,112303.551600,437002.833600,112303.461600,437002.858800,112303.371600,437002.866000,112303.296000,437002.894800,112303.216800,437002.941600,112303.170000,437003.028000,112303.144800,437003.103600,112303.152000,437003.190000,112303.170000,437003.290800,112303.173600,437003.406000,112303.191600,437003.571600,112303.206000,437003.740800,112303.263600,437003.928000,112303.314000,437004.126000,112303.360800,437004.342000,112303.414800,437004.342000,112303.414800,437004.810000,112303.558800,437004.810000,112303.558800,437005.029600,112303.602000,437005.029600,112303.602000,437005.342800,112303.767600,437005.512000,112303.836000,437005.677600,112303.890000,437005.893600,112303.864800,437006.109600,112303.900800,437006.314800,112303.954800,437006.505600,112304.016000,437006.685600,112304.080800,437006.865600,112304.134800,437007.070800,112304.206800,437007.286800,112304.304000,437007.567600,112304.448000,437007.909600,112304.613600,437008.230000,112304.757600,437008.518000,112304.880000,437008.806000,112304.934000,437009.076000,112304.944800,437009.310000,112304.880000,437009.547600,112304.818800,437009.716800,112304.746800,437009.868000,112304.631600,437010.004800,112304.433600,437010.130800,112304.235600,437010.202800,112304.080800,437010.282000,112303.918800,437010.336000,112303.767600,437010.390000,112303.638000,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800}; 17 public int ii=100; 18 19 20 Ts_verify_r_Result_t user_ok = null; 21 22 Terminal termthis = null; 23 RpcCommAdapter adapter = null; 24 Thread thread; 25 SnsTestMain(){ 26 27 28 thread = new Thread(new Runnable(){ 29 public void run(){ 30 try{ 31 while(true){ 32 Thread.sleep(2000); 33 if(tsprx!=null){ 34 heartbeat(); 35 send_gps(); 36 } 37 38 } 39 }catch(Exception e){ 40 System.out.println(e.toString()); 41 System.out.println("thread exiting "); 42 } 43 } 44 45 }); 46 //thread.start(); 47 48 initServant(); 49 } 50 51 void initServant(){ 52 adapter = RpcCommunicator.instance().createAdapter("termimal", RpcConsts.MSG_ENCODE_XML); 53 termthis = new Terminal(){ 54 @Override 55 public void hello(RpcContext ctx){ 56 57 } 58 }; 59 adapter.addServant(termthis); 60 } 61 62 void heartbeat(){ 63 Ts_heartbeat_p_User_t user = new Ts_heartbeat_p_User_t(); 64 user.id = user_ok.user.id; 65 tsprx.heartbeat_oneway(user); 66 } 67 68 69 void send_gps(){ 70 Ts_gps_p_GPS_t gps = new Ts_gps_p_GPS_t(); 71 gps.lon= Float.valueOf((float)121.2004); 72 gps.lat= Float.valueOf((float)31.1234); 73 gps.speed = (float)0.0; 74 gps.direction = (float)35.5; 75 gps.time = new Date().getSeconds(); 76 Ts_gps_p_Properties_t props = new Ts_gps_p_Properties_t(); 77 props.senderid = user_ok.user.id; 78 props.target_type="2"; 79 props.users="1001,1002"; 80 props.groups="2001,2001"; 81 tsprx.gps_oneway(gps, props); 82 83 } 84 85 public Tc_redirect_r_Result_t test_redirect(){ 86 87 TcProxy tc = TcProxy.createWithXML("172.26.181.193", 8881); //8889 88 Tc_redirect_p_User_t user = new Tc_redirect_p_User_t(); 89 user.token = token; 90 Tc_redirect_r_Result_t r = null; 91 try{ 92 tc.redirect_async(user, new Tc_AsyncCallBack(){ 93 @Override 94 public void redirect(Tc_redirect_r_Result_t result,RpcProxyBase proxy){ 95 System.out.println(String.format("%s %d", result.tc_srv_ip,result.tc_srv_port)); 96 //到这里重定向调度okay 97 test_verify(result.tc_srv_ip,result.tc_srv_port); 98 } 99 @Override 100 protected void onError(String name,String msg,RpcProxyBase proxy){ 101 System.out.println(String.format("error: %s msg:%s",name,msg)); 102 } 103 }); 104 //tc.destroy(); 105 }catch(Exception e){ 106 System.out.println(e.toString()); 107 return null; 108 } 109 return r; 110 } 111 112 public Tc_redirect_r_Result_t test_verify(String host,int port){ 113 tsprx = TsProxy.createWithXML(host, port); 114 Ts_verify_p_User_t user = new Ts_verify_p_User_t(); 115 user.token = token; 116 try{ 117 tsprx.verify_async(user, new Ts_AsyncCallBack(){ 118 @Override 119 public void verify(Ts_verify_r_Result_t result,RpcProxyBase proxy){ 120 System.out.println(String.format("verify back() msg:%s code:%d gourps.length:%d", result.msg,result.code,result.groups.size())); 121 user_ok = result; 122 tsprx.conn.attachAdapter(adapter); 123 send_gps(); 124 } 125 }); 126 //ts.destroy(); 127 }catch(Exception e){ 128 System.out.println(e.toString()); 129 return null; 130 } 131 return null; 132 } 133 134 135 public static void test(){ 136 SnsTestMain sns = new SnsTestMain(); 137 sns.test_redirect(); 138 } 139 140 public static void mainxxx(String[] args) { 141 SnsTestMain.test(); 142 143 } 144 145 } 146 IDL 语法树结构 python+ ply lexical.py 1 import sys 2 3 language = 'py' 4 arch = '32' 5 6 class SyntexTreeNode: 7 def __init__(self,name): 8 self.name = None 9 10 class Container: 11 def __init__(self,name=''): 12 # SyntexTreeNode.__init__(self,name) 13 self.children={} 14 self.list=[] 15 16 def addChild(self,c): 17 #self.children.append(c) 18 self.children[c.getName()] = c 19 self.list.append(c) 20 21 def createStruct(self,st): 22 pass 23 24 def createInterface(self,ifc): 25 pass 26 27 def createSequence(self,seq): 28 pass 29 30 def createDictionary(self,dict): 31 pass 32 33 def createEnumeration(self,enm): 34 pass 35 36 def createUnit(self,unit): 37 pass 38 39 40 41 42 43 class Contained: 44 def __init__(self,container=None): 45 self.container =container 46 47 class TypeId: 48 def __init__(self,type,id): #变量定义 type id; 49 self.type = type # int name , <int> is type, <name> is id 50 self.id = id 51 52 53 class TypeBase: 54 def __init__(self,name): 55 self.name = name 56 self.idx = 0 #索引,用于数据序列化时类型识别 57 #self.type = name 58 59 def getName(self): 60 return self.name 61 62 def getTypeDefaultValue(self): 63 if language == 'py': 64 return '%s()'%self.name 65 if language =='as': 66 return 'new %s()'%self.name 67 if language =='cpp': 68 return '%s()'%self.name 69 if language =='java': 70 return 'new %s()'%self.name 71 72 def getMappingTypeName(self): 73 r = self.name 74 75 return r 76 77 class DataMember(Contained): 78 def __init__(self,d,container): # d - type_id 79 Contained.__init__(self,container) 80 self.d = d 81 self.name = d.id 82 self.type = d.type 83 84 class Sequence(Contained,TypeBase): 85 def __init__(self,name,type): 86 TypeBase.__init__(self,name) 87 self.type = type # sequence<type> name; 88 self.valuetype = type 89 90 def getTypeDefaultValue(self): 91 if language == 'py': 92 return '[]' 93 if language == 'as': 94 return 'new Array()' 95 if language =='cpp': 96 return 'std::vector< %s >()'%self.type.getMappingTypeName() 97 if language =='java': 98 return 'new Vector<%s>()'%self.type.getMappingTypeName() 99 100 def getMappingTypeName(self): 101 r = '' 102 if language == 'as': 103 r = 'Array' 104 105 if language =='cpp': 106 return 'std::vector< %s >'%self.type.getMappingTypeName() 107 if language =='java': 108 return 'Vector<%s>'%self.type.getMappingTypeName() 109 110 return r 111 112 class Dictionary(Contained,TypeBase): 113 def __init__(self,name,first,second): 114 TypeBase.__init__(self,name) 115 self.first = first 116 self.second = second 117 118 def getTypeDefaultValue(self): 119 if language =='py': 120 return '{}' 121 if language == 'as': 122 return 'new HashMap()' 123 124 if language == 'cpp': 125 #return 'boost::shared_ptr< std::map<%s,%s> >( new std::map<%s,%s>()) '%(self.first.name,self.second.name,self.first.name,self.second.name) 126 # return '%s::hash_type()'%self.name 127 return 'std::map< %s,%s >()'%(self.first.getMappingTypeName(),self.second.getMappingTypeName()) 128 if language =='java': 129 return 'new Hashtable<%s,%s>()'%(self.first.getMappingTypeName(), 130 self.second.getMappingTypeName() 131 ) 132 133 134 def getMappingTypeName(self): 135 r = '' 136 if language == 'as': 137 r = 'HashMap' 138 if language =='cpp': 139 # return 'boost::shared_ptr< std::map<%s,%s> >'%(self.first.name,self.second.name) 140 # return '%s::hash_type'%(self.name) 141 r = 'std::map< %s,%s >'%(self.first.getMappingTypeName(),self.second.getMappingTypeName() ) 142 143 if language == 'java': 144 r = 'Hashtable< %s,%s >'%(self.first.getMappingTypeName(),self.second.getMappingTypeName() ) 145 146 return r 147 148 class Enumeration(Contained,TypeBase): 149 def __init__(self,name): 150 TypeBase.__init__(name) 151 152 class OperateMember(Contained): 153 def __init__(self,name,type,params): 154 self.name = name 155 self.type = type # callreturn [ type foo(params) ] 156 self.params = params 157 pass 158 159 class Struct(Container,Contained,TypeBase): 160 def __init__(self,name): 161 Container.__init__(self) 162 Contained.__init__(self) 163 TypeBase.__init__(self,name) 164 165 def createDataMember(self,dm): 166 if self.children.has_key(dm.id): 167 return False 168 e = DataMember(dm,self) 169 self.children[dm.id] = e 170 self.list.append(e) 171 return True 172 173 174 175 class Interface(Container,Contained,TypeBase): 176 def __init__(self,name): 177 Container.__init__(self) 178 Contained.__init__(self) 179 TypeBase.__init__(self,name) 180 181 182 183 def createOperateMember(self,opm): 184 185 if self.children.has_key(opm.name): 186 return False 187 self.children[opm.name] = opm 188 self.list.append(opm) 189 190 return True 191 192 class Unit(Container): 193 def __init__(self): 194 Container.__init__(self) 195 pass 196 197 198 class Builtin(TypeBase): 199 def __init__(self,type): 200 TypeBase.__init__(self,type) 201 self.type = type 202 203 tables =[ 204 'byte', 205 'bool', 206 'short', 207 'int', 208 'long', 209 'float', 210 'double', 211 'string', 212 ] 213 214 @staticmethod 215 def isBuiltinType(type): 216 return Builtin.tables.count(type) 217 218 def getTypeDefaultValue(self): 219 r = 'None' 220 type = self.type 221 if type in ('byte','short','int','long'): 222 r = '0' 223 if language == 'java': 224 if type == 'byte': 225 #r= '%s.valueOf( (byte)0)'%self.getMappingTypeName() 226 r = '0' 227 else: 228 r = '%s.valueOf(0)'%self.getMappingTypeName() 229 230 elif type in ('float','double'): 231 r = '0.0' 232 if language == 'java': 233 r = '%s.valueOf(0)'%self.getMappingTypeName() 234 235 elif type in ('bool'): 236 r = 'False' 237 if language in ('as','cpp'): 238 r = 'false' 239 # if language == 'cpp': 240 # r = 'false' 241 if language == 'java': 242 r = '%s.valueOf(false)'%self.getMappingTypeName() 243 244 elif type in ('string'): 245 r = "''" 246 if language in ('as','cpp','java'): 247 r ="\"\"" 248 # if language =='cpp': 249 # r = "\"\"" 250 251 return r 252 253 def getMappingTypeName(self): 254 r = '-^|^*'*5 255 256 if language == 'as': 257 type = self.type 258 if type in ('byte',) : #'bool'): 259 r ='uint' 260 if type in ('bool',): 261 r = 'Boolean' 262 if type in ('short','int'): 263 r = 'int' 264 elif type in ('float','long','double'): 265 r = 'Number' 266 elif type in ('string'): 267 r = "String" 268 elif type in ('void'): 269 r ='void' 270 271 if language == 'cpp': 272 type = self.type 273 274 if type in ('byte',) : #'bool'): 275 r ='unsigned char' 276 if type in ('bool',): 277 r = 'bool' 278 if type in ('short',): 279 r ='short' 280 if type in ('int',): 281 r = 'int' 282 elif type in ('float',): 283 r = type 284 elif type in ('long',): 285 r = type 286 if arch =='32': 287 r = 'long long' 288 elif type in ('double',): 289 r = type 290 elif type in ('string'): 291 r = "std::string" 292 elif type in ('void'): 293 r ='void' 294 295 if language == 'java': 296 type = self.type 297 298 if type in ('byte',) : #'bool'): 299 r ='Byte' 300 if type in ('bool',): 301 r = 'Boolean' 302 if type in ('short',): 303 r ='Short' 304 if type in ('int',): 305 r = 'Integer' 306 elif type in ('float',): 307 r = 'Float' 308 elif type in ('long',): 309 r = 'Long' 310 311 elif type in ('double',): 312 r = 'Double' 313 elif type in ('string'): 314 r = "String" 315 elif type in ('void'): 316 r ='void' 317 318 return r 319 320 @staticmethod 321 def id(name): 322 pass 323 324 @staticmethod 325 def str(id_): 326 pass 327 328 types_def={} 329 kwds=['struct', 330 'interface', 331 'sequence', 332 'dictionary', 333 'exception', 334 'void' 335 ] 336 337 def getTypeDef(type): 338 t = types_def.get(type,None) 339 return t 340 341 #检测变量名称是否合法 342 def checkVariantName(name,all=True): 343 if kwds.count(name): 344 return False 345 if all: 346 if getTypeDef(name): 347 return False 348 return True 349 350 def initBuiltinTypes(): 351 for t in Builtin.tables: 352 types_def[t] = Builtin(t) 353 354 types_def['void'] = Builtin('void') 355 356 initBuiltinTypes() 357 358 unit=None 继续贴 语法代码 grammar.py 1 #--coding:utf-8-- 2 3 4 #scott shanghai china 5 # qq:24509826 msn: socketref@hotmail.com 6 # 7 8 import sys 9 10 11 12 if ".." not in sys.path: sys.path.insert(0,"..") 13 14 import os,sys,os.path,struct,time,traceback,string 15 16 import ply.lex as lex 17 import ply.yacc as yacc 18 19 from lexparser import * 20 import lexparser as myparser 21 22 ''' 23 默认是SLR,我们也可以通过参数指定为 LALR( 24 25 idl 26 interface定义 函数参数保留名称: d,idx,m 27 ''' 28 29 30 tokens=( 31 'IDENTIFIER','STRUCT','NUMBER','INTERFACE', 32 'SEQUENCE','DICTIONARY','EXCEPTION','COMMENTLINE', 33 # 'VOID', 34 35 ) 36 37 def t_COMMENTLINE(t): 38 '//.*\n' 39 40 def t_SEQUENCE(t): 41 'sequence' 42 return t 43 44 def t_DICTIONARY(t): 45 'dictionary' 46 return t 47 48 49 def t_STRUCT(t): 50 'struct' 51 return t 52 53 54 def t_INTERFACE(t): 55 'interface' 56 return t 57 58 #def t_VOID(t): 59 # 'void' 60 # return t 61 62 def t_IDENTIFIER(t): 63 '[A-Za-z_][A-Za-z0-9_]*' 64 return t 65 66 t_NUMBER=r'\d+([uU]|[lL]|[uU][lL]|[lL][uU])?' 67 68 t_ignore =" \t" 69 70 def t_newline(t): 71 r'\n+' 72 t.lexer.lineno += 1 #int(t.value) #.count("\n") 73 74 75 def t_error(t): 76 print("Illegal character '%s'" % t.value[0]) 77 t.lexer.skip(1) 78 79 literals = [ '{','}',';','(',')','<','>',',' ] 80 81 # Build the lexer 82 lexer = lex.lex() 83 84 #literals = [ ':',',','(',')' ] 85 86 87 88 #lexer.input(data) 89 90 #while 1: 91 # tok = lexer.token() 92 # if not tok: break 93 #print tok.type,tok.value 94 #sys.exit() 95 96 97 def p_start(t): 98 '''start : definations 99 ''' 100 101 defs = t[1] 102 unit = Unit() 103 104 defs.reverse() 105 106 # print defs 107 for d in defs: 108 unit.addChild( d ) 109 110 111 t[0] = unit 112 113 #t[0] = t[1] 114 #print t[0] 115 116 def p_definations(t): 117 '''definations : defination ''' 118 119 t[0] = [t[1],] 120 121 def p_definations_2(t): 122 '''definations : defination definations ''' 123 t[2].append(t[1]) 124 t[0]= t[2] 125 126 def p_defination(t): 127 ''' defination : struct_def ';' 128 | interface_def ';' 129 | sequence_def ';' 130 | dictionary_def ';' 131 | 132 ''' 133 t[0]=t[1] 134 135 136 def p_sequence_def(t): 137 ''' 138 sequence_def : SEQUENCE '<' type '>' IDENTIFIER 139 ''' 140 # print 'sequence..',t[3] 141 name = t[5] 142 143 if not checkVariantName(name,False): 144 print 'error: sequence<%s>.%s illegal!'%(t[3],name) 145 sys.exit() 146 147 if getTypeDef(name): 148 print 'error: line %s sequence (%s) has existed!'%(t.lineno(1),name) 149 sys.exit() #sequence的类型名存在 150 151 type_ = t[3] 152 # if not getTypeDef(type_): 153 # print 'error: sequence %s <%s> not defined!'%(name,type_) 154 # sys.exit() 155 # print 'xxx.',name,type_ 156 157 seq = Sequence(name,type_) 158 types_def[name] = seq 159 t[0] = seq 160 161 def p_dictionary_def(t): 162 ''' 163 dictionary_def : DICTIONARY '<' type ',' type '>' IDENTIFIER 164 ''' 165 first = t[3] 166 second = t[5] 167 name = t[7] 168 # print first,second,name 169 170 if not checkVariantName(name,False): 171 print 'error: dictionary < %s > illegal!'%(name) 172 sys.exit() 173 174 if getTypeDef(name): 175 print 'error: line %s dictionary type (%s) has existed!'%(t.lineno(1),name) 176 sys.exit() #sequence的类型名存在 177 178 179 # if not getTypeDef(first): 180 # print 'error: dictionary %s.first <%s> not defined!'%(name,first) 181 # sys.exit() 182 # 183 # if not getTypeDef(second): 184 # print 'error: dictionary %s.second <%s> not defined!'%(name,second) 185 # sys.exit() 186 187 dict = Dictionary(name,first,second) 188 types_def[name] = dict 189 t[0] = dict 190 191 def p_interface_def(t): 192 ''' 193 interface_def : INTERFACE IDENTIFIER '{' operatemembers '}' 194 ''' 195 # print t[1],t[2] 196 id = t[2] 197 type_ = getTypeDef(id) 198 if type_: 199 print 'error: interface name(%s) has existed!'%id 200 sys.exit() 201 202 ifc = Interface(id) 203 opms = t[4] 204 opms.reverse() 205 #检测函数名称是否有重复 206 # print opms 207 for opm in opms: 208 if not ifc.createOperateMember(opm): 209 210 print 'error: line %s createOperateMember failed! interface:: %s.%s'%( t.lineno(3),t[2],opm.name) 211 sys.exit() 212 # print type(ifc.children) 213 types_def[id] = ifc 214 t[0] = ifc # reduce to syntax tree ![](http://www.cppblog.com/Images/dot.gif) 215 216 217 218 def p_operatemembers(t): 219 ''' 220 operatemembers : operatemember 221 ''' 222 #print 'operatemember num:',len(t) # 1 means no operatemebmer 223 # if len(t) > 1: 224 # t[0] = t[1] 225 # else: 226 # t[0] = [] 227 t[0] = [t[1],] 228 229 230 def p_operatemembers_2(t): 231 ''' 232 operatemembers : operatemember operatemembers 233 ''' 234 if type(t[2]) != type([]): 235 t[0] = [t[1],t[2]] 236 else: 237 t[2].append(t[1]) 238 t[0] = t[2] 239 240 def p_operatemember(t): 241 ''' 242 operatemember : callreturn IDENTIFIER '(' operateparams ')' ';' 243 244 ''' 245 params = t[4] 246 params.reverse() # 这里必须进行倒置一下 247 for p in params: 248 if p.id in ('d','p','m','r','idx','size','cr','container','o','prx','m2'): 249 p.id+='_' 250 opm = OperateMember(t[2],t[1],params) 251 t[0] = opm 252 # print 'x1.',opm 253 254 def p_operateparams(t): 255 ''' 256 operateparams : type_id 257 | 258 ''' 259 if len(t) > 1: 260 t[0] = [ t[1],] 261 else: 262 t[0] =[ ] 263 264 265 def p_operateparams_2(t): 266 ''' 267 operateparams : type_id ',' operateparams 268 ''' 269 270 t[3].append(t[1]) 271 t[0] = t[3] 272 273 def p_callreturn(t): 274 ''' 275 callreturn : type 276 ''' 277 t[0] = t[1] 278 279 280 def p_struct_def(t): 281 ''' struct_def : STRUCT IDENTIFIER '{' datamembers '}' ''' 282 id = t[2] 283 284 type = getTypeDef(id) 285 if type: 286 print 'error struct name:%s existed!'%(id) 287 sys.exit() 288 289 st = Struct(id) 290 # print t[4] 291 t[4].reverse() 292 for dm in t[4]: 293 if not st.createDataMember(dm): 294 print 'error: datamember<%s> name<%s> has existed!'%(id,dm.id) 295 sys.exit() 296 297 types_def[id] = st #注册数据类型到全局类型表 298 #print types_def 299 t[0] = st 300 301 302 303 def p_datamembers(t): 304 ''' 305 datamembers : datamember 306 ''' 307 #print t[1] 308 #t[0] = t[1] 309 t[0] = [t[1],] 310 #print 'a1..' 311 #print t[0] 312 313 def p_datamembers_2(t): 314 ''' 315 datamembers : datamember datamembers 316 ''' 317 if type(t[2]) != type([]): 318 t[0] = [t[1],t[2]] 319 else: 320 t[2].append(t[1]) 321 t[0] = t[2] 322 323 324 def p_datamember(t): 325 ''' 326 datamember : type_id ';' 327 ''' 328 #print 'datamenber..' 329 t[0] = t[1] 330 331 def p_type_id(t): 332 ''' 333 type_id : type IDENTIFIER 334 ''' 335 #print t[1],t[2] 336 id = t[2] 337 if not checkVariantName(id): 338 print 'error: type_id.id<%s> illegal!'%(id) 339 sys.exit() 340 341 t[0] = TypeId(t[1],t[2]) 342 343 344 345 346 def p_type(t): 347 '''type : IDENTIFIER 348 ''' 349 type = getTypeDef(t[1]) 350 351 if type: 352 t[0] = type 353 else: 354 print 'error: line %s'%(t.lineno),' type:%s not existed!'%t[1] 355 sys.exit() 356 357 358 def p_error(t): 359 print("Syntax error at '%s'" % t) 360 #print t.lineno,t.lexpos 361 362 yacc.yacc(debug=True,method="SLR") 363 364 data=''' 365 366 sequence < int > IntList; 367 368 struct student { 369 int x; 370 int y; 371 int z; 372 double fractor; 373 IntList ids; 374 }; 375 376 interface booksystem{ 377 int test1(int age); 378 int test2(int age); 379 }; 380 381 struct dog{ 382 int name; 383 string dog; 384 int name2; 385 student st1; 386 }; 387 388 389 390 ''' 391 392 data=''' 393 394 struct animal{ 395 int x; 396 }; 397 398 sequence < animal > IntList; 399 400 struct student { 401 int x; 402 int y; 403 int z; 404 double fractor; 405 IntList ids; 406 }; 407 408 interface booksystem{ 409 int test1(int age); 410 int test2(int age); 411 }; 412 413 sequence<IntList> XX; 414 415 dictionary<int,int> int2_t; 416 417 dictionary<int2_t,string> intstr_t; 418 419 420 ''' 421 422 def filterComments(data): 423 return data 424 lines = data.split("\n") 425 result=[] 426 for line in lines: 427 x = line.strip() 428 if x and x[0] =='#': 429 continue 430 result.append(line) 431 data = string.join(result,'\n') 432 return data 433 434 def syntax_result(data): 435 return yacc.parse( filterComments(data)) 436
以上为词法和语法解析idl的代码,生成了语法树了,这部分如果idl不做扩展,这部分是无需修改 接下来就是各种语言版本的代码mapping了,也就是根据grammar tree的生成对于的框架代码,代码过长,这里就不帖了
|